Configuring Systemd Service to run with root access
tell systemd to run the service with
sudo has nothing to with it.
Typically you instruct systemd to run a service as a specific user/group with a
Group= directive in the
[Service] section of the unit file.
Set those to root (or remove them, as running as root is the default).
systemd system services run as root by default, but there is still a difference between the default behavior and running a system service with
As documented in Environment variables in spawned processes, these variables are only set if
User= is set:
$USER, $LOGNAME, $HOME, $SHELL
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
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:
[Unit] Description=Rails Webserver After=syslog.target [Service] Type=simple User=ubuntu WorkingDirectory=/var/www/webserver ExecStart=/usr/bin/sudo /bin/bash -lc 'bundle exec rails server -e demo -p 80' Restart=always KillSignal=SIGQUIT [Install] WantedBy=multi-user.target