Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
    
You can automate starting services such as LAMP on boot up; there should be no need to use a sudo-bash hack. –  izx Jun 20 '12 at 5:28
    
I thought about that, but I don't necessarily want my localhost running all the time. Just when I actually need it. –  Scotty C. Jun 20 '12 at 5:38
add comment

2 Answers

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|improve this answer
    
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. Jun 20 '12 at 6:00
    
Obviously the above scripts span multiple lines, but the comments here don't show it. –  Scotty C. Jun 20 '12 at 6:05
    
Did you reboot? –  jasmines Jun 20 '12 at 6:41
    
@jasmines, I think you should at least give a warning for the process. –  Anwar Shah Jun 20 '12 at 7:07
    
@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. –  Scotty C. Jun 22 '12 at 21:19
add comment

You can open a terminal, then "sudo gnome-terminal", enter the password and just copy, paste the commands to install LAMPP. Or you can write a script, "run.sh" for example, and run "sudo sh run.sh". Hope that helps.

share|improve this answer
1  
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. –  Mahesh Jun 20 '12 at 7:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.