Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

How do I run a .sh script with root privileges from Ubuntu's cmd? Some scripts require to enter the password. I watn to avoid this.

share|improve this question
    
It's called 'bash', presumably, not cmd. –  wzzrd Aug 3 '09 at 14:39

5 Answers 5

If security is a concern, I wouldn't do this as has been mentioned.

Better would be to use visudo to edit the sudoers file, and edit all the commands that the script uses that need to be root. So, the whole script doesn't need to be root, but maybe shutdown does (contrived example), so:

fooUser ALL=(ALL) NOPASSWD:/sbin/reboot,/sbin/shutdown

This means that fooUser will be able to run the reboot and shutdown commands from ALL terminals and when they are acting as any (ALL) user.

And then, in the scipt:

sudo /sbin/shutdown
share|improve this answer

Do you mean a command inside the script is asking for a password, or that you just want to run it once with a password at launch, or that you have a script that's automated to run and is pausing for a password?

I normally launch with sudo from the command prompt and it uses root privileges, but otherwise you may have to muck with the sudoer's file in /etc to apply "nopasswd" to the command or script you're using.

share|improve this answer

You can use NOPASSWD in /etc/sudoers to not prompt for a password for some/all commands/users/hosts when using sudo.

See the manpage.

share|improve this answer

Edit the sudoers file using the visudo command as follows:

%cdrom    ALL = NOPASSWD: /usr/bin/k3b

This is not a good practice for shell scripts unless you are running an application like tripwire that will detect changes to the script.

share|improve this answer

Asside all the other advice in the answers to your question ... you could also open a bash shell (Terminal) and then type sudo su - , enter your password wich will give you a shell with root privilegs from where you can run scripts as root without re-entering the password.

As other's have advised: be carefull

user@prompt>sudo su -
password: xXxXxXx
root@prompt>
share|improve this answer
    
That's what I've got: training@training-vm:~$ sudo su - root@training-vm:~# /home/training/myscript.sh root@127.0.0.1's password: I have to reenter the password now –  lak-b Aug 3 '09 at 17:27

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.