-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.

Wayne_Yux
  • 4,942

1 Answers1

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.