Running a program as non root from a script
I have a question closely related to this thread: Best practice to run Linux service as a different user
but I need the solution to work开发者_运维知识库 in "every" Linux distribution.
I would like to run a program as a non root user from a script. This way, when init.d starts up the services at boot time as root, the script launches the process as the non-root user I specify. Of course the solution shouldn't prompt for a password.
I think this is the normal/correct procedure when deploying applications.
How could I do that?
Thanks a lot
A good way would be to drop privileges from your actual program. Then just pass that user as a parameter. Inside you can handle it in a very standard way (setuid()
)
Otherwise su -c 'your command' different_user
will work just fine on any linux. (as long as different_user exists)
There are two ways:
sudo
command - you need to add the original user to/etc/sudoers
with such entry that the program can be run without (NOPASSWD
)seteuid()
system call (if you can modify the program)
If you are root
, you can also use su
(see @cnicutar's answer for details)
精彩评论