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.
Related videos on Youtube
Author by
Scotty C.
Updated on September 18, 2022Comments
-
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 almost 12 yearsYou can automate starting services such as LAMP on boot up; there should be no need to use a sudo-bash hack.
-
Admin almost 12 yearsI thought about that, but I don't necessarily want my localhost running all the time. Just when I actually need it.
-
-
Scotty C. almost 12 yearsOkay, 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. almost 12 yearsObviously the above scripts span multiple lines, but the comments here don't show it.
-
jasmines almost 12 yearsDid you reboot?
-
Anwar almost 12 years@jasmines, I think you should at least give a warning for the process.
-
Mahesh almost 12 yearsOP 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. 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 over 5 years