Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This is what I plan to do:

#!/bin/bash

sudo -i <<'EOF'
iptables-save | awk '/^[*]/ { print $1 } 
                 /^:[A-Z]+ [^-]/ { print $1 " ACCEPT" ; }
                 /COMMIT/ { print $0; }' | iptables-restore
EOF

I will save the above and call it cleariptables.sh. I will set it as executable.

It will be placed in a folder called bin in the following path: /home/cymbal/bin/

(where cymbal is the username)

I will create a shortcut to cleariptables.sh on the desktop. The contents of the shortcut will be:

[Desktop Entry]
Version=1.0
Type=Application
Terminal=true
Icon[en_US]=nm-device-wired
Name[en_US]=ClearIPtables
Exec=gksudo /home/cymbal/bin/cleariptables.sh
Comment[en_US]=Flush iptables filters
Name=ClearIPtables
Comment=Flush iptables script
Icon=nm-device-wired

Whenever I click on the desktop shortcut, I will be prompted to enter the password and the script will run.

Question: Clicking on the desktop shortcut prompts me for the password. As there is a sudo -i in the script, will I be prompted again for the password?

share|improve this question
    
If you're interested in viewing the output or any errors in the script, this question might be helpful too: How can I make a script that opens terminal windows and executes commands in them? –  lk- May 25 at 21:14

2 Answers 2

up vote 1 down vote accepted

You can use NOPASSWD directive in your sudoers file.

Add this line to your /etc/sudoers file

your_login ALL = (root) NOPASSWD: /path/to/your/script

When editing this file, you should use the visudo command instead of a regular text editor. This will check for syntax errors before saving your changes.

share|improve this answer
    
Thanks for the tip. What does your_login represent? Is it my username? –  user66229 May 25 at 22:48
1  
Yes it is cymbal –  Slyx May 26 at 2:44
    
Thanks for the clarification. Now, if I add your recommended line to my /etc/sudoers file, will my username become root from that point onwards? I do not want that to happen. During Debian installation, I expressly chose not to allow log in as root. –  user66229 May 26 at 3:48
1  
No ! Only the given command is run as root. It's not a root login ! –  Slyx May 26 at 3:52
1  
But any files by your script, will have the root as owner :-) –  Slyx May 26 at 3:56

No, you should not be prompted for a password again. The script will be running as root due to the gksudo. In my experience, sudo never asks for password if you are already root (although I couldn't find this explicitly documented).

share|improve this answer
    
You are right. I was not prompted for a password again. –  user66229 May 26 at 16:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.