LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-04-2017, 05:05 PM   #1
MQMan
Member
 
Registered: Jan 2004
Location: Los Angeles
Distribution: Slack64 14.1
Posts: 581

Rep: Reputation: 38
Start systemd service as a user, but run ExecStartPost as root


Hi,

I'm using systemd on a CentOS 7 system to run a user based process. This is working perfectly.

However, I now need to add an additional step that runs after the main process start, but this has to run as root. Just adding the ExecStartPost entry fails with Permission Denied as this step is currently run as the same user as the main process.

How can I get this second step to run as root.

Cheers.
 
Old 04-04-2017, 06:36 PM   #2
shwaybotx
LQ Newbie
 
Registered: Mar 2017
Location: Amarillo, T Amateur photographer, published author. Interests: astronomy, political science & freedom of press. Been: 37 countries. Lived: 3 continents.X
Distribution: linux mint mate kfce 18.1
Posts: 7

Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by MQMan View Post
Permission Denied as this step is currently run as the same user as the main process.
I am very interested in this. I spent half my morning today trying to figure out the exact same thing. I finally figured out the best way to do what I was trying to do was use crontab. In other words, I gave up and went a different way. I'm still interested in the outcome, however.
 
Old 04-04-2017, 07:42 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Obviously, in the general sense, what you would be asking for here is privilege escalation. A non-root process wants to "run something as root." ("Well, don't they all ..." )

One possibility is to create an entirely root-level sequence that, say, uses some kind of sudo -u userid trick to invoke a particular process under the auspices of a particular user. The owning sequence, however, is still understood to be "rootly," and it therefore could initiate the other processing.

I haven't thought this hare-brained idea completely through, o'course ...
 
Old 04-04-2017, 11:15 PM   #4
MQMan
Member
 
Registered: Jan 2004
Location: Los Angeles
Distribution: Slack64 14.1
Posts: 581

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by sundialsvcs View Post
A non-root process wants to "run something as root."
Not quite. systemd *is* (or should be) running as root to fire off the user process. It's just that the ExecStartPost inherits the user attribute.

Another possibility would be if there's a way to "chain" another service off the user one, as a dependency, then that would work also.

Quote:
Originally Posted by sundialsvcs View Post
One possibility is to create an entirely root-level sequence that, say, uses some kind of sudo -u userid trick to invoke a particular process under the auspices of a particular user.
I thought that's what's happening now. The systemd process runs as root and starts a process as a particular user. Plus, I also thought that it wasn't possible to use sudo under systemd. But I'm happy to be corrected.

Cheers.

Last edited by MQMan; 04-04-2017 at 11:17 PM.
 
1 members found this post helpful.
Old 04-05-2017, 02:34 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
add the command to sudoers NOPASSWD?
 
Old 04-05-2017, 12:10 PM   #6
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,141

Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
Create another service file and have it run after the user service?

Code:
After = your-user.service
 
Old 04-05-2017, 02:03 PM   #7
MQMan
Member
 
Registered: Jan 2004
Location: Los Angeles
Distribution: Slack64 14.1
Posts: 581

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by jmgibson1981 View Post
Create another service file and have it run after the user service?

Code:
After = your-user.service
Been playing with this all morning, together with lots of Google-fu, and just the After= on the 2nd service didn't do the trick, sorry.

But, the following does appear to do what I want:

On the original service, I added the following:
Code:
Before=2nd.service
Wants=2nd.service
And created that 2nd service with:
Code:
[Unit]
Description=Follow up script that needs to be root
PartOf=original.service

[Service]
Type=oneshot
ExecStart=/usr/local/bin/some.sh
RemainAfterExit=yes
Cheers.
 
Old 05-06-2019, 12:35 PM   #8
mobusdorphin
LQ Newbie
 
Registered: May 2019
Posts: 1

Rep: Reputation: Disabled
Since this is the top result for my duckduckgo search for starting a process as a user, but execstart[pre|post] as root, I wanted to add this answer

From man 5 systemd.service

Code:
Table 1. Special executable prefixes

...

If the executable path is prefixed with "+" then the process is executed with full privileges. 
In this mode privilege restrictions configured with User=, Group=, CapabilityBoundingSet= or the 
various file system namespacing options (such as PrivateDevices=, PrivateTmp=) are not applied to the 
invoked command line (but still affect any other ExecStart=, ExecStop=, ... lines).
In summary

Code:
[Service]
ExecStartPre=+/usr/bin/privilegedprocess
ExecStart=/home/jamal/userprocess
ExecStartPost=+/usr/bin/anotherrootprocess

User=jamal
Group=jamal
This will start /home/user/userprocess as user jamal, but the ExecStartPre and ExecStartPost will run as root. This should be much simpler and cleaner than creating a subservice that is PartOf the main service.

Last edited by mobusdorphin; 05-06-2019 at 12:42 PM.
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't access full journalctl from script via systemd service even though user is in systemd-journal group iwtbf Linux - Newbie 0 02-19-2016 02:44 PM
[SOLVED] systemd - trying to enable querylog each time BIND is restarted w/ExecStartPost psycroptic Linux - Server 1 11-03-2013 11:56 AM
Service won't start unless run as root user gauge73 Linux - General 3 05-07-2005 10:37 PM
Is it possible to start a service but not as a root user appas Linux - Software 1 10-15-2004 11:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 06:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration