Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Sudo in Bash

  1. #1
    Join Date
    Feb 2009
    Beans
    25

    Sudo in Bash

    I am trying to write a simple bash file. One of the lines has sudo at the beginning which of course requires the user to input their sudo password. Is there a way to run the script without having to type in the sudo password?

  2. #2
    Join Date
    Dec 2005
    Location
    Western Australia
    Beans
    11,480
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Sudo in Bash

    There is a way to add the script to the "sudoers" file so it won't prompt for a password to run the script. I am not sure of the syntax required for this.

    The other method is to change the owner of the script so that root owns it, and then set the "setuid" flag on it. This means that the script will always run as the file's owner, i.e. root.

    If you enable Advanced Permissions in Nautilus for the root user (gksudo gconf-editor; then apps > nautilus), then open a root file browser and get Properties for the script, you can change the owner and add the setuid ability.
    I try to treat the cause, not the symptom. I avoid the terminal in instructions, unless it's easier or necessary. My instructions will work within the Ubuntu system, instead of breaking or subverting it. Those are the three guarantees to the helpee.

  3. #3
    Join Date
    Apr 2007
    Location
    Belgium
    Beans
    1,528

    Re: Sudo in Bash

    Depending on the situation (for example, if it's a script that has to run at boot or a cron script), you can remove the sudo because it is executed as root already.
    Disclaimer: I am currently suffering from severe CSD (Compulsive Sarcasm Disorder).
    My Site | Linux User #452328 | Running Arch Linux on Sony Vaio VGN-SZ61XN/C since October 2008

  4. #4
    Join Date
    Feb 2009
    Beans
    25

    Re: Sudo in Bash

    Quote Originally Posted by 3rdalbum View Post
    If you enable Advanced Permissions in Nautilus for the root user (gksudo gconf-editor; then apps > nautilus), then open a root file browser and get Properties for the script, you can change the owner and add the setuid ability.
    Are you able to explain this in a little more detail, I don't quite follow what I have to do.

  5. #5
    Join Date
    Apr 2006
    Location
    Ubuntuland
    Beans
    2,124
    Distro
    Ubuntu 13.10 Saucy Salamander

    Lightbulb Re: Sudo in Bash

    Quote Originally Posted by 3rdalbum View Post
    There is a way to add the script to the "sudoers" file so it won't prompt for a password to run the script. I am not sure of the syntax required for this.
    The "sudoers" files is a who file, not a what file. Meaning that it specifies who has privileges.

    You don't usually need to change sudoers, you just add users to the admin group (which is already specified in sudoers).
    24 beers in a case, 24 hours in a day. Coincidence? I think not!

    Trusty Tahr 64 bit, AMD Phenom II 955 Quad Core 3.2GHz, GeForce 9600 GT
    16G PC2-6400 RAM, 128 GB SSD, Twin 1TB SATA 7200 RPM RAID0

  6. #6
    Join Date
    Dec 2008
    Beans
    59
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Sudo in Bash

    Quote Originally Posted by daved2424 View Post
    Are you able to explain this in a little more detail, I don't quite follow what I have to do.
    I can give it a shot.

    1. Use Alt-F2 to open the Run dialog box.
    2. Type in "gksudo gconf-editor" (without the quotes) and press Enter.
    3. Enter your password (if necessary)
    4. Double click apps, scroll down and double-click nautilus, and then click preferences.
    5. In the right pane, scroll down until you see show_advanced_permissions. Put a checkmark in its box.


    Now, any time you want to change the setuid of a file, run "gksudo nautilus" (using ALT-F2 again).

    ----

    I personally think it would be easier for you to do it all in a terminal, since you obviously know bash. Just cd to the directory that has the file, and then run the following two commands

    Code:
    sudo chown root file
    sudo chmod ug+s file
    ----

    Either way you choose, if you ever want to edit the file again, you'll have to open it as root. Run "gksudo gedit /path/to/file", either in a terminal, or with ALT-F2. (replace gedit with your favorite editor. In a terminal, you could also use sudo instead of gksudo.)
    Last edited by trlkly; March 1st, 2009 at 07:58 PM. Reason: didn't close list

  7. #7
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Sudo in Bash

    Quote Originally Posted by 3rdalbum View Post
    The other method is to change the owner of the script so that root owns it, and then set the "setuid" flag on it. This means that the script will always run as the file's owner, i.e. root.
    For security reasons, the setuid bit is not honored for scripts, only binary executables...

  8. #8
    Join Date
    Feb 2009
    Beans
    25

    Re: Sudo in Bash

    That's great everyone. Thank you so much for your help.

  9. #9
    Join Date
    Dec 2008
    Beans
    59
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Sudo in Bash

    Quote Originally Posted by geirha View Post
    For security reasons, the setuid bit is not honored for scripts, only binary executables...
    Seriously? As in, Ubuntu thinks it knows what's best for me and won't let me do what I want to do? That's a Microsoft move, if ever I heard one.

  10. #10
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Sudo in Bash

    Quote Originally Posted by trlkly View Post
    Seriously? As in, Ubuntu thinks it knows what's best for me and won't let me do what I want to do? That's a Microsoft move, if ever I heard one.
    Not really Ubuntu. Linux in general. And also many other unix implementations disable setuid for scripts due to security holes.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •