How to run script as another user without password?
visudo and add this:
user1 ALL=(user2) NOPASSWD: /home/user2/bin/test.sh
The command paths must be absolute! Then call
sudo -u user2 /home/user2/bin/test.sh from a
user1 shell. Done.
Run script as another user on Linux
The answer is change from su to sudo.
su is primarily for switching users, while
sudo is for executing commands as other users. The
-u flag lets you specify which user to execute the command as:
sudo -u wayne '/home/wayne/script2.sh'
Sorry user is not allowed to execute
Provide password for running a script inside another script as different user
Yes you have option for that. But that password you have to provide at the time of executing the script or hard coded it in the script itself. Try with the below example:-
echo 'passwordofB' | sudo -u B -S ./script2.sh
Also you can do it like:-
sudo -u A ./script.sh passwordofB #as a command line parameter
echo $1 | sudo -u B -S ./script2.sh
You are executing another script
sudo -u B ./script2.sh from script
./script.sh right? So change that line with
echo $1 | sudo -u B -S ./script2.sh and run your first script as
sudo -u A ./script.sh passwordofB where
passwordofB is the password for user 'B'
How to make passwordless switch to another user in a shell script
You don't use "su" with the sudoers file, you need to use "sudo". So, you'd want a command line like:
sudo su - hduser
which would do want you want, provided you had the appropriate lines in the sudoers file. A line like this:
hadoopmaster ALL=(ALL:ALL) NOPASSWD: su - hduser
should do the trick.