I would like to run a script from the main ubuntu shell as a different user that has no password.
I have full sudo privileges, so I tried this:
sudo su -c "Your command right here" -s /bin/sh otheruser
Then I have to enter my password, but I am not sure if that script is now really running under that user.
How can I confirm that the script is really running under that user now?
You can do that with
su
orsudo
, no need for both.The relevant parts of
man sudo
:su
can only switch user without providing a password if you are root. See Caleb's answerYou can modify the
/etc/pam.d/su
file to allowsu
without password. See this answer.If you modified your auth file to the following, any user that was part of group
somegroup
couldsu
tootheruser
without a password.Then test from terminal
If you want to use su instead of sudo, I believe you can use something like this:
-
will simulate a login of the specified user-c
tells it that you want to run a commandps. Unfortunately I'm not able to install ruby using rvm with this method, but that's probably not related.
The answers above are really useful to me but to answer the actual question...
Use:
The output should include your script and the actual user executing it. People on BSD-like systems, e.g. MAC can find similar information with:
Make sure you switch to root user with command
sudo su
and then use the commandFor example:
su www-data -s bin/magento cache:clean
I had the same problem. Just type in the command
screen -dmS testscreen
this will create a detached screen on your non-sudo user account and then you can log it and check if this screen is there byscreen -ls
.You can use
sudo
to do this. First, you need to modify the/etc/sudoers
file to add a rule that allows thehudson
user to become the other user for the script in question. Let's assume you have auser1
account that needs to run/usr/local/bin/my_script.sh
.Run
visudo
to modify thesudoers
file. Then, use the followingsudo
command to run the script from hudson.It's very important that you use
visudo
to modify thesudoers
file in order to check the file for errors before putting it in place. Also, I'd suggest a quick read throughman sudoers
in order to clearly understand what the above does before adding it to your configuration.I have created few users without password using
To confirm please check
I have written a shell script named
testing.sh
like:Now I am going to run this script as different users who do not have any password.
I use "ssh" pointing to the same local machine. This enables me to run the shell script or command passwordless.
First, I make sure the host-user saved his public key to the target user (in the same machine). Then I execute the ssh command I showed above.
The result, is a command/script run as the target user, without even entering a password.