All I need to do is to run a specific script as a particular user who does have the nologin/false
shell indicated in /etc/passwd
.
I would run the script as root and this should run as another user. Running:
~# su -c "/bin/touch /tmp/test" testuser
would work, but I need a valid shell for the testuser.
I know I can disable the password with passwd -d testuser
and leave the shell to /bin/bash
this way would secure a little bit but I need to have nologin/false
shell.
Basically what I need is what crontab
does when we set jobs to be running as a particular user, regardless this one has nologin/false
shell.
p.s I found this thread Executing a command as a nologin user, but I have no idea how to concatenate
the command su -s /bin/sh $user
to the script I need to run.
You can use the -s switch to su to run a particular shell
(Prepend
sudo
to the above iftestuser
is a passwordless user.)You can do this with
sudo -u
if you have it installed:By providing the script as the argument to execute to /bin/sh:
just realized :
su -s "/bin/bash" -c "/bin/touch /tmp/testuser" testuser
maybe there is a better way ?!
actually, the best way to do this is via runuser
see the man page for details
this tool is used to deal with the situation as the developer said in his bolg
Whenever an service is running as root and wants to change UID using the shell it should use runuser.
http://danwalsh.livejournal.com/55588.html
i've post this answer in many palaces, forgive me if you find this annoying
All you need is
sudo
:sudo -u <user> -g <group> -- <command>
Use sudo -u to specify the user. Also use the -i flag to set environment variables of the target user too.
Use su command with shell -s and -c command. As shown below