Managing Services in systemd¶
In this article
systemd is the standard init system and service manager in most modern Linux distributions:
- Ubuntu 16.04+ (strongly recommended Ubuntu 20.04/22.04/24.04)
- Debian 8+ (Debian 11/12 – current LTS releases)
- RHEL/CentOS 7+ (RHEL 9 / AlmaLinux/Rocky Linux 9 – modern CentOS replacements)
- Fedora, openSUSE, Arch, and others
After the kernel boots, it hands control to systemd (PID 1), which is responsible for:
- starting and stopping services,
- mounting filesystems,
- configuring the network and environment,
- managing dependencies between components.
The primary interface for interacting with systemd is the utility *****systemctl******.
Basic Syntax¶
Note
In most cases, using systemctl requires superuser privileges—use sudo.
Viewing Service Status¶
| Task | Command |
|---|---|
| List active units (services, sockets, timers, etc.) | systemctl list-units |
| Only running services | systemctl list-units --type=service |
| All services (including stopped and inactive) | systemctl list-units --type=service --all |
| List running services | systemctl list-units --type=service --state=running |
| Search for a specific service (e.g., nginx) | systemctl list-units '*nginx*' |
| Detailed information about a service | systemctl status nginx (with logs, PID, dependencies) |
Note
Add --no-pager to prevent the output from paging (e.g., systemctl status nginx --no-pager).
Managing Service Operation¶
We’ll use nginx as an example, but the commands apply to any service: sshd, postgresql, clamav-daemon, docker, etc.
| Action | Command | Note |
|---|---|---|
| Check status | sudo systemctl status nginx | Shows whether it’s running, its PID, recent logs, and errors |
| Start | sudo systemctl start nginx | Starts the service until the next reboot |
| Stop | sudo systemctl stop nginx | |
| Restart | sudo systemctl restart nginx | Full stop → start (if not running, it will start) |
| Reload configuration without stopping | sudo systemctl reload nginx | Works if the service supports SIGHUP (nginx, Apache, Postfix, etc.) |
| Reload or Restart | sudo systemctl reload-or-restart nginx | Falls back to restart if reload isn’t supported |
| Try restart (only if running) | sudo systemctl try-restart nginx | Safe for scripts |
| Check if service is active | systemctl is-active nginx | Output: active / inactive / unknown |
| Check if service has failed | systemctl is-failed nginx | Output: failed if the service exited with an error |
Note
After start/restart, the service will not automatically start on reboot—autostart requires separate configuration.
Managing Autostart¶
| Action | Command | What Happens |
|---|---|---|
| Enable autostart | sudo systemctl enable nginx | Creates a symlink from /usr/lib/systemd/system/nginx.service to /etc/systemd/system/multi-user.target.wants/ |
| Disable autostart | sudo systemctl disable nginx | Removes the symlink but does not stop the running process |
| Check autostart status | systemctl is-enabled nginx | Possible values: enabled, disabled, static, masked |
| Reset settings → re‑enable | sudo systemctl reenable nginx | Clears previous overrides and re‑enables the service |
| Reset to default | sudo systemctl preset nginx | Reverts any custom settings to the distribution default (rarely used) |
Advanced Features¶
1. Forcing systemd Configuration Reload¶
If you edited a .service file:
2. Viewing Dependencies¶
3. Service Journal (via journalctl)¶
4. Masking a Service (Complete Disable, Even Manually)¶
Useful Commands for Everyday Use¶
# Quickly check if the service is enabled and running
systemctl is-active --quiet nginx && echo "OK" || echo "STOPPED"
# Start and enable autostart with a single command
sudo systemctl start nginx && sudo systemctl enable nginx
# Disable and stop the service
sudo systemctl stop nginx && sudo systemctl disable nginx
Note
The --now flag (available since systemd v220+, 2015) performs enable + start or disable + stop simultaneously.