Run Bash Script as Root

6,178

You could add the script to your sudoers configuration file:

sudo visudo

Then find the following:

%sudo ALL=(ALL) ALL

add after:

 your_username ALL=(ALL) NOPASSWD: /path/to/your/script

CTRL+X and confirm.

Share:
6,178

Related videos on Youtube

Scotty C.
Author by

Scotty C.

Updated on September 18, 2022

Comments

  • Scotty C.
    Scotty C. over 1 year

    Is there some way I can run a Bash script as root without being prompted for a password every time? I'm attempting to automate the process of starting my LAMPP install, which requires me running a couple of sudo commands to start and stop services. Optimally, I'd like to build this into an executable file, so all I'd have to do is click an icon. ;)

    Obviously, I have the password, so that's not an issue. I just want to take my laziness one step farther and not have to enter it every time I start my localhost. After all, that's what programming is all about, right?

    • Admin
      Admin almost 12 years
      You can automate starting services such as LAMP on boot up; there should be no need to use a sudo-bash hack.
    • Admin
      Admin almost 12 years
      I thought about that, but I don't necessarily want my localhost running all the time. Just when I actually need it.
  • Scotty C.
    Scotty C. almost 12 years
    Okay, I did something wrong, but I don't know what it was. It's still prompting for a password on both of the commands. :S The sudoers file edit: scotty ALL=(ALL) NOPASSWD: /opt/lampp/startup.sh The few lines of my executable script: #!/bin/bash sudo /opt/lampp/lampp startapache sudo /opt/lampp/lampp startmysql
  • Scotty C.
    Scotty C. almost 12 years
    Obviously the above scripts span multiple lines, but the comments here don't show it.
  • jasmines
    jasmines almost 12 years
    Did you reboot?
  • Anwar
    Anwar almost 12 years
    @jasmines, I think you should at least give a warning for the process.
  • Mahesh
    Mahesh almost 12 years
    OP was asking for ways to run WITHOUT entering password. If you include modifying sudoers file to allow running just this script with NOPASSWORD, it can be an answer. In its present form, however, it is invalid.
  • Scotty C.
    Scotty C. almost 12 years
    @jasmines Hey, sorry it took so long to get back to this. Been up to my ears in work lately. Anyway, yes, I did reboot, and the executable file is still asking for my password.
  • Pablo Bianchi
    Pablo Bianchi over 5 years
    And here is why using setuid is not a good idea.