Code

How to Run a Shell Script as Superuser

If you've ever tried to run a shell script as superuser in Linux by using the syntax sudo ./example-shell-script, you'll know that doesn't really work – the commands within the script are executed as you, not the superuser, so permissions issues can easily arise. Instead, if you want to run the script as superuser, this is how to do it:

  1. Create your script
    vi example-shell-script
  2. Change the permissions
    chmod 755 example-shell-script
  3. Switch into superuser mode
    sudo su -
  4. Run the script
    ./example-shell-script
  5. Log out of superuser mode
    exit

That's it. You're done. Pretty easy, right?



1 Comment

lara
February 17, 2012, 4:35 am
thanks a lot! I was having trouble running a script as super user, by giving sudo su - , it worked :)

Leave a Comment

Name
Email
Website
Comment
Name and email are required. Your email will not be published.

This post was published on October 4th, 2009 by Robert James Reese in the following categories: Linux, VI Editor.

Before using any of the code or other content in this post, you must read and agree to our Terms & Conditions.

Copyright © 2014, Ink Plant. All rights reserved.