5

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?

Scotty C.
  • 151
  • You can automate starting services such as LAMP on boot up; there should be no need to use a sudo-bash hack. – ish Jun 20 '12 at 05: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 05:38

2 Answers2

7

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.

jasmines
  • 11,011
  • 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 06:00
  • Obviously the above scripts span multiple lines, but the comments here don't show it. – Scotty C. Jun 20 '12 at 06:05
  • Did you reboot? – jasmines Jun 20 '12 at 06:41
  • @jasmines, I think you should at least give a warning for the process. – Anwar Jun 20 '12 at 07: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
  • And here is why using setuid is not a good idea. – Pablo Bianchi Dec 26 '18 at 01:53
-1

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.

Huynh
  • 198
  • 2
    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 07:59