2

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?

1

2 Answers 2

6

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).

1
  • You are right. I was not prompted for a password again.
    – user66229
    Commented May 26, 2014 at 16:53
0

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.

6
  • Thanks for the tip. What does your_login represent? Is it my username?
    – user66229
    Commented May 25, 2014 at 22:48
  • 1
    Yes it is cymbal
    – Slyx
    Commented May 26, 2014 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
    Commented May 26, 2014 at 3:48
  • 1
    No ! Only the given command is run as root. It's not a root login !
    – Slyx
    Commented May 26, 2014 at 3:52
  • 1
    But any files by your script, will have the root as owner :-)
    – Slyx
    Commented May 26, 2014 at 3:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .