Writing scripts that needs root permission

September 2nd, 2007 mysurface Posted in Admin, Bash, sudo | Hits: 82374 | 17 Comments »

While executing commands that needs root permission, I rely on sudo. Sudo provides me a very convenient way to execute root privilege commands from my user account provided with my user account password.

Sudo and su are not the same, check out more from here.

When comes to scripting, I couldn’t use sudo directly. For examples I make up a script modifying /etc/resolv.conf with my DNS settings. In order to modify /etc/resolv.conf, I need root permission. But I couldn’t execute commands directly with sudo in the script.

Below showing the script that DOES NOT work.

# the lines below doesn't work!!!
sudo echo "nameserver" > /etc/resolv.conf
sudo echo "nameserver" >> /etc/resolv.conf
cat /etc/resolv.conf

I can write all our commands that need root permission in the script, then externally execute the script with sudo. Let say my script is set_dns.sh.

sudo ./set_dns.sh

The line above works! But what if I want to run it as normal user and it prompts me for sudo password? I figure out a way, I can do that with the script below.

if [ "$1" == 'done' ]
    #the commands needs root permission list as below
    echo "nameserver" > /etc/resolv.conf
    echo "nameserver" >> /etc/resolv.conf
    cat /etc/resolv.conf
    sudo $0 done

I make it as a template, I just have to replace the portion between ‘then’ and ‘else’.


17 Responses to “Writing scripts that needs root permission”

  1. But you can’t use parameters with your script!

    If you use this it will work:

    if [ “$1” == ‘done’ ]; then
    # …
    sudo $0 done $*

  2. blackbelt_jones Says:

    Interesting, and cool! I’ve been enabling sudo to run without a password by adding this line to the /etc/sudoers file:

    bbjones ALL=NOPASSWD: ALL

    and that makes adding to su to scripts and desktop shortcuts pretty straightforward. Of course, some users aren’t going to want to run sudo without a password for security reasons.

  3. me I use temp files and than sudo cp, I find it easier to manage

    By example this simple script to set minimum fan speed on macbooks:

    echo “Input desired minimum speed (in RPM):”
    read speed
    echo $speed > ./.tmp
    sudo cp ./.tmp /sys/devices/platform/applesmc.768/fan1_min -f
    rm ./.tmp

  4. I adore your website.. excellent colours & theme. Did an individual design this site oneself or maybe have you actually rely on someone else to do it for you personally? Plz answer while I!|m planning to design and style my very own blog site as well as want to learn where by u became this specific through. thanks

  5. Very good blog post. I definitely love this site.
    Keep it up!

  6. Well I searched for the article title and found this,
    never thought i’d find my answer

  7. Very exciting short article

  8. Extremely fascinating article

  9. The better communication at work writing is definately one of the best i have read

  10. Inside the top 10 of my favourite content pieces, thanks!

  11. Lovely webpage, it loads very fast and appears really cool.

  12. At the least it is more informative than one
    of these reality Television stars, kim this?
    Joey what?

  13. The electronics technician jobs in texas short article is definately
    the most effective i’ve read today.

  14. I arrived from twitter well done on an outstanding
    social media promotion

  15. This has to be my second favorite piece in the week, i’m not
    able to’t tell you number one, it may offend you!

  16. One of the superb pieces i have read in the week.

  17. Really… such a beneficial website
    madden nfl 17 http://www.adv.mydatadiary.com/index.php?item/1837

Leave a Reply