How to run script as another user without password?
Call 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'
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 ./script2.sh
Also you can do it like:-
sudo -u A ./script.sh passwordofB #as a command line parameter
now inside script.sh
:-
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.
Related Topics
Uninstall Node.Js Using Linux Command Line
How to Find All Files Containing Specific Text on Linux
Linux Blocking Vs. Non Blocking Serial Read
How to Permanently Export a Variable in Linux
Use of Floating Point in the Linux Kernel
Curl to Access a Page That Requires a Login from a Different Page
Connection Refused to Mongodb Errno 111
Externalizing Tomcat Webapp Config from .War File
Understanding User File Ownership in Docker: How to Avoid Changing Permissions of Linked Volumes
Curl Command to Repeat Url Request
How to Change the Output Color of Echo in Linux
Deploying Yesod to Heroku, Can't Build Statically
Sorting Multiple Keys With Unix Sort
Difference Between Using 'Sh' and 'Source'