systemctl --user: manage your own services without sudo
- tool
- systemd
- command
- systemctl
- flag
- --user
- default
- --system: talk to the system service manager
systemctl --user talks to your own per-user service manager instead of the system one. Every systemctl command works with it (start, stop, enable, status, daemon-reload, list-units), but it manages units from your home directory, runs them as you, and needs no sudo. Without the option, systemctl uses --system, which the manual calls "the implied default".
Syntax
mkdir -p ~/.config/systemd/user
cp myapp.service ~/.config/systemd/user/
systemctl --user daemon-reload
systemctl --user enable --now myapp.service
systemctl --user status myapp.service
journalctl --user -u myapp.service -fA user unit looks like a system unit, but its [Install] section usually targets default.target, because user managers have no multi-user.target:
[Unit]
Description=My app (user service)
[Service]
ExecStart=%h/bin/myapp
Restart=on-failure
[Install]
WantedBy=default.target--user, --system and --global
| Option | Service manager | Unit files read from (main locations) |
|---|---|---|
| --system (default) | The system manager (PID 1) | /etc/systemd/system, /usr/lib/systemd/system |
| --user | Your user manager | ~/.config/systemd/user, /etc/systemd/user, /usr/lib/systemd/user |
| --global | None (only with enable and disable) | Enables a unit in the global user configuration for all future logins of all users |
| --user --machine=alice@.host | alice's user manager | alice's user unit paths |
The full user search path in systemd.unit(5) also includes $XDG_CONFIG_DIRS, $XDG_RUNTIME_DIR and $XDG_DATA_HOME locations; ~/.config/systemd/user/ is the one to use for hand-written units.
Keep user services running: lingering
A user manager is started when you log in and stopped when your last session ends, so user services die with it. loginctl enable-linger changes that: in the words of the loginctl manual, a user manager is spawned for the user at boot and kept around after logouts.
loginctl enable-linger # for the calling user
sudo loginctl enable-linger alice # for another user
loginctl disable-lingerLogs of user services
journalctl --user shows messages from your user services, and with it -u NAME is treated as a user unit. The journalctl manual notes that --user only works if persistent logging is enabled through Storage= in journald.conf.
Pitfalls
- A unit created in
~/.config/systemd/user/is invisible to plainsystemctl;sudo systemctl start myappreports Unit myapp.service not found. Add--userand dropsudo. - After editing a user unit run
systemctl --user daemon-reload, not the system one. - User services cannot bind ports below 1024 or write system directories unless the system grants that; use a system unit with
User=for those (see create a systemd service).
FAQ
Why does my user service stop when I log out?
The per-user service manager normally runs only while you have a session. Run loginctl enable-linger once for your user: a user manager is then started at boot and kept after logout, so services enabled with systemctl --user keep running.
Does sudo systemctl --user work?
It talks to root's user manager, not yours, because the command runs as root. Run systemctl --user as the user who owns the service; an administrator can reach another user's manager with systemctl --user --machine=alice@.host.
Where do I put the unit file for systemctl --user?
In ~/.config/systemd/user/ for your own units. Units for every user go in /etc/systemd/user/, and systemctl --global enable turns them on for all future logins.
Sources
- freedesktop.orgwww.freedesktop.org/software/systemd/man/latest/systemctl.html#--user
- freedesktop.orgwww.freedesktop.org/software/systemd/man/latest/systemd.unit.html
- freedesktop.orgwww.freedesktop.org/software/systemd/man/latest/loginctl.html
- freedesktop.orgwww.freedesktop.org/software/systemd/man/latest/journalctl.html