-1

how do I can achieve sudo privilege via the shell script without entering the password? Example:

[user@user Desktop]$ ./script.sh 
[user@user Desktop]# 

which command should I try on the script.sh? I do not want to add in the sudoers file. Please help me. Thanks for your valuable time.

1
  • short story: requesting sudo privilege always requires either password or modifying sudoers file Jul 13, 2016 at 11:14

1 Answer 1

0

you would have to alter the sudoers file (or add a new file to /etc/sudoers.d/) to allow user "user" to execute this script without password:

user  ALL=(ALL:ALL) NOPASSWD: /home/user/Desktop/script.sh

if you do not want to execute the whole script.sh as root, you need to mention all commands within the script.sh that you will be prepending with sudo. it's somehow more readable if you use the alias feature:

Cmnd_Alias SCRIPT_CMDS = /sbin/poweroff, /sbin/reboot, /sbin/halt
user ALL=(ALL:ALL) NOPASSWD: SCRIPT_CMDS

EDIT

If you build some script that outputs the password at stdout, you can use the askpass parameter:

sudo -A /path/to/password-output-script /some/command/to/be/run/as/root

else this might be of interest to you (from sudo man page):

-S, --stdin
Write the prompt to the standard error and read the password from the standard input instead of using the terminal device. The password must be followed by a newline character.

4
  • I need to run some commands with sudo privilege. If the script works then I can easly enter the commands without entering the password. But I don't want to enter in the sudoers file. Is there any other way?? Jul 13, 2016 at 11:36
  • does this fit your usecase? askubuntu.com/a/585145/555486 otherwise I think, I missed the point of your question. Jul 13, 2016 at 11:44
  • I mean, I need to get sudo privilege via the shell script without enter the password manually. But I do not want to enter on sudoers file. Something like echo "password" | sudo -s Jul 13, 2016 at 12:03
  • If you build some script that outputs the password at stdout, you can use the askpass parameter: sudo -A /path/to/password-output-script /some/command/to/be/run/as/root Jul 13, 2016 at 12:10

Not the answer you're looking for? Browse other questions tagged .