How to Run Script as Another User Without Password

How to run script as another user without password?

Call visudo and add this:

user1 ALL=(user2) NOPASSWD: /home/user2/bin/

The command paths must be absolute! Then call sudo -u user2 /home/user2/bin/ 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/'

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

Also you can do it like:-

 sudo -u A ./ passwordofB #as a command line parameter

now inside

echo $1 | sudo -u B -S ./

You are executing another script sudo -u B ./ from script ./ right? So change that line with echo $1 | sudo -u B -S ./ and run your first script as sudo -u A ./ 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.

Related Topics

Leave a reply