1

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

Community
  • 1
  • 1
luix5984
  • 39
  • 1
  • 4

2 Answers2

3

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)

viraptor
  • 33,322
  • 10
  • 107
  • 191
1

There are two ways:

  1. sudo command - you need to add the original user to /etc/sudoers with such entry that the program can be run without (NOPASSWD)
  2. seteuid() system call (if you can modify the program)

If you are root, you can also use su (see @cnicutar's answer for details)

Kimvais
  • 38,306
  • 16
  • 108
  • 142