Understanding and Managing Persistent User Sessions with Linger on Ubuntu
Introduction to Linger
In Ubuntu (and other Linux systems using systemd), linger allows non-root users to run persistent services that:
- Start automatically at system boot.
- Continue running after the user logs out.
Without linger, user-specific services terminate when the user logs out. Linger is ideal for long-running processes like web servers, databases, or bots under a user account.
Why Use Linger?
- Run services without active login: No need to keep a user logged in.
- Start on boot: Services launch automatically during system startup.
- User-level isolation: Avoid running everything as
root; use dedicated users for security. - Resource management: User services respect system resource limits.
Enabling Linger for a User
Use loginctl to enable linger. Replace username with the target user:
sudo loginctl enable-linger username
Example:
sudo loginctl enable-linger iman
Disabling Linger
To revoke persistent sessions:
sudo loginctl disable-linger username
Example:
sudo loginctl disable-linger iman
Verifying Linger Status
Check if linger is active for a user:
loginctl show-user username | grep Linger
- Output:
Linger=yes(enabled) orLinger=no(disabled).
Alternative Check:
ls /var/lib/systemd/linger/
Enabled users appear as files in this directory.
Example: Persistent Web Server with Linger
Goal: Run a Python web server (http.server) under user iman that survives logout.
Steps:
- Enable linger for
iman:
sudo loginctl enable-linger iman
- Create a systemd user service:
As iman, create the service file:
mkdir -p ~/.config/systemd/user/
nano ~/.config/systemd/user/simple-web.service
Paste this configuration:
[Unit]
Description=Simple Web Server
[Service]
ExecStart=/usr/bin/python3 -m http.server 8080
WorkingDirectory=/home/iman/web-content # Customize this path
Restart=always
[Install]
WantedBy=default.target
- Reload the user’s systemd:
systemctl --user daemon-reload
- Start & enable the service:
systemctl --user enable --now simple-web.service
- Verify:
systemctl --user status simple-web.service # Should show "active"
curl http://localhost:8080 # Test the server
- Log out: The service persists!
- Managing User Services
- Start/stop a service:
systemctl --user start service-name
systemctl --user stop service-name
- View logs:
journalctl --user-unit=service-name
- Important Notes
- Security: Services run with user privileges. Ensure they don’t expose unnecessary ports or run untrusted code.
- Resource Limits: Use
systemdresource controls (e.g.,MemoryMax=in service files) to prevent abuse. - Dependencies: User services can’t depend on system services (e.g.,
network.target). Use sockets or timers instead. - Cleanup: Disable linger if services are no longer needed.
- Troubleshooting
- Service fails to start:
- Check paths in the service file.
- Test the command manually (
ExecStart=...). - Inspect logs:
journalctl --user-unit=service-name.
- Linger not working:
- Confirm linger is enabled:
loginctl show-user username. - Reboot the system to trigger startup.
- Confirm linger is enabled:
- Conclusion
Linger empowers non-root users to manage persistent services safely. By combining it with systemd user units, you can deploy user-level services that survive reboots and logouts, simplifying long-term tasks while maintaining security isolation.
Next Steps:
- Explore
systemduser timers for scheduled tasks. - Learn about resource constraints in service files (
man systemd.resource-control).
