Configuring Systemd Service to run with root access

Solution 1:

tell systemd to run the service with sudo?

sudo has nothing to with it.

Typically you instruct systemd to run a service as a specific user/group with a User= and Group= directive in the [Service] section of the unit file.

Set those to root (or remove them, as running as root is the default).

Solution 2:

To clear, systemd system services run as root by default, but there is still a difference between the default behavior and running a system service with User=root.

As documented in Environment variables in spawned processes, these variables are only set if User= is set:


I tested to confirm this finding. So if you want to run a systemd service as root that needs one of the above variables, you need to set User=root.

Solution 3:

a temporary solution, but got it to work in a pinch:

/usr/bin/sudo /bin/bash -lc 'bundle exec rails server -e demo -p 80'

Can run with a user who has sudo privileges in a systemd unit file like so:

Description=Rails Webserver

ExecStart=/usr/bin/sudo /bin/bash -lc 'bundle exec rails server -e demo -p 80'