How to make ubuntu service run as specific user

How to make ubuntu service run as specific user?

I created an init.d script and make it start at boot, but sooner I found the service is running as root after reboot.

I google and google, people suggest run as a seperate user, so what's the steps?


Solution 1:

Upstart v1.4+ states ...

setuid $user

Changes to the user before running the job's process.

  • Change $user to an actual username
  • Note that all processes (pre-start, post-stop, et cetera) will be run as the user specified.
  • Note that if you specify an invalid username in the setuid stanza, Upstart will log an error if it is in Debug Mode.