You are not logged in.

#1 2018-10-08 19:19:18

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,734
Website

Systemd service run as user but run a pre and post as root [SOLVED]

Goal: write a systemd service to run the ExecStart= line as an unprivileged user BUT have ExecStopPost= and ExecStartPre= lines run as root.

Is this possible?  For example, the following will start the program as the unprivileged user:

[Unit]
Description=my service
After=remote-fs.target

[Service]
User=myuser
Group=mygroup
ExecStart=/usr/bin/myscript
Restart=on-failure

[Install]
WantedBy=multi-user.target

But what I really want is to have root run a 2nd program both before and after (ExecStartPre and ExecStartPost or an equivalent).  I can achieve the goal by omitting the User= and Group= lines from the service and by modifying /usr/bin/myscript to run certain lines within as myuser:mygroup but, I'd like to do it with entirely with systemd if possible.  I would be OK with a solution involving multiple .service units but am unsure how to make them conditional on this one.

Thoughts are appreciated.

Last edited by graysky (2018-10-08 20:11:06)

Offline

#2 2018-10-08 19:45:02

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: Systemd service run as user but run a pre and post as root [SOLVED]

Another option may be using sudo and some NOPASSWD entries in sudoers, but that dosn't fulfil the systemd only criteria either.

That's the best I've got smile


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#3 2018-10-08 19:52:12

loqs
Member
Registered: 2014-03-06
Posts: 19,113

Re: Systemd service run as user but run a pre and post as root [SOLVED]

Have you read the Special executable prefixes table in man 5 systemd.service?

Offline

#4 2018-10-08 20:10:54

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,734
Website

Re: Systemd service run as user but run a pre and post as root [SOLVED]

loqs wrote:

Have you read the Special executable prefixes table in man 5 systemd.service?

Great pointer!  Thank you, this fits the bill.

Offline

#5 2018-10-08 20:58:40

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Systemd service run as user but run a pre and post as root [SOLVED]

If that weren't the case, you could still implement privilege dropping by using

/usr/bin/runuser -u username command args

systemd's User and Group keywords make this easier and reduce boilerplate, but it isn't impossible to do without.


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

Board footer

Powered by FluxBB