Persistence by Abusing systemd Services
A short showcase of user-level persistence through systemd user services on a Linux host.
Persistence by Abusing systemd Services
User-level systemd services live in:
1 | ~/.config/systemd/user/ |
If the directory does not exist yet, create it first:
1 | mkdir -p ~/.config/systemd/user |
Create a user service file:
1 | vi ~/.config/systemd/user/persistence.service |
1 | [Unit] |
Reload the user systemd manager and enable the service:
1 | systemctl --user daemon-reload |
enable does not run the service immediately. It creates the user-level autostart link, so the service starts the next time the user’s default.target is reached. In most setups, that means the next login for that user.
To start it immediately as well:
1 | systemctl --user start persistence.service |
This provides persistence across logins and reboots where the user logs in again. If the service should start after boot without an interactive login, lingering must be enabled for that account:
1 | loginctl enable-linger "$USER" |