Running service as a user
Is it possible to run an upstart service as another user? Similar to how cron can specify which user a job runs as. If not directly, is there a workaround?
Question information
- Language:
- English Edit question
- Status:
- Answered
- For:
- upstart Edit question
- Assignee:
- No assignee Edit question
- Last query:
- 2008-09-22
- Last reply:
- 2012-01-12
| aberlanas (angel-berlanas) said : | #1 |
This is usefull for you?
exec su myuser -c "exec mydaemon"
| Igor Galić (i.galic) said : | #2 |
If I wanted to start an httpd to only run with the minimum capabilities(7) needed, that's not possible with su or sudo.
The trouble those is that they don't respect pam_cap (or PAM in general), when executed as a privileged user:
[root@prvix]/etc# grep auth pam.d/system-auth
#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth required pam_cap.so debug
auth required pam_env.so
auth sufficient pam_unix.so try_first_pass nullok
auth required pam_deny.so
[root@prvix]/etc# cat security/
cap_net_
cap_net_
none *
[root@prvix]/etc# getent passwd daemon
daemon:
[root@prvix]/etc# getent passwd i.galic
i.galic:
[root@prvix]/etc# sudo -u daemon getpcaps $$
Capabilities for `22898': =ep
[root@prvix]/etc# sudo -u i.galic getpcaps $$
Capabilities for `22898': =ep
[root@prvix]/etc# su - i.galic
i.galic@prvix ~ % getpcaps $$
Capabilities for `12303': =
i.galic@prvix ~ % su - i.galic
Password:
i.galic@prvix ~ % getpcaps $$
Capabilities for `12353': = cap_net_
i.galic@prvix ~ %
pam_cap.so and /etc/security/
Am I making sense here? Is this anywhere in the scope of upstart? Could it be?
| Randall Leeds (randall-leeds) said : | #3 |
If I understand the issue correctly, I just addressed something similar myself.
The solution is to have libcap2-bin add the pam_cap.so as optional before the root user su in /etc/pam.d/su
My file used to show:
# This allows root to su without passwords (normal operation)
auth sufficient pam_rootok.so
I changed it to:
# This allows root to su without passwords (normal operation)
auth optional pam_cap.so
auth sufficient pam_rootok.so
By default, libcap2-bin only installs the pam_cap module in /etc/pam.
In my /etc/security/
cap_net_
And finally, add the capability to your service's binary or interpreter. Setting the capability to "effective and permitted" (+ep) is an EXTREMELY BAD IDEA, especially with an interpreter (a service run with python or erlang must set capabilities on the interpreter binary), since all users will get this permission. However, using "effective and inherited" only works a charm to propogate the rules from /etc/security/
In my case, this meant executing the following:
$ setcap cap_net_
Can you help with this problem?
Provide an answer of your own, or ask Eoghan Gaffney for more information if necessary.

