Skip to content

Managing Services in systemd

systemd is the default 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 over to systemd (PID 1), which is responsible for:

  • starting and stopping services (services),
  • mounting filesystems,
  • configuring networking and environment,
  • managing dependencies between components.

The primary interface for interacting with systemd is the utility systemctl.


Basic Syntax

systemctl [options] command [service]

Note

In most cases, superuser privileges are required to work with systemctl – 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).


Controlling Service Operation

Using nginx as an example – applicable to any service: sshd, postgresql, clamav-daemon, docker, etc.

Action Command Remark
Check status sudo systemctl status nginx Shows whether it’s running, PID, recent logs, errors
Start sudo systemctl start nginx Starts only until the next reboot
Stop sudo systemctl stop nginx
Restart sudo systemctl restart nginx Full stop → start (if not running, it will start)
Reload config without stopping sudo systemctl reload nginx Works if the service handles SIGHUP (nginx, apache, postfix, etc.)
Reload or Restart sudo systemctl reload-or-restart nginx If reload isn’t supported, it falls back to restart
Try to 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 won’t start automatically on reboot – enable it separately for autostart.


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 link but does not stop the current process
Check autostart status systemctl is-enabled nginx Possible values: enabled, disabled, static, masked
Reset to defaults → re‑enable sudo systemctl reenable nginx Useful after changing WantedBy= in the unit file
Restore “default” sudo systemctl preset nginx Applies preset autostart rules from the package (rarely used)

Advanced Features

1. Force systemd to reload its configuration

If you edit a .service file:

sudo systemctl daemon-reload
sudo systemctl restart nginx

2. View dependencies

systemctl list-dependencies nginx

3. Service logs (via journalctl)

sudo journalctl -u nginx --since "1 hour ago"
sudo journalctl -u nginx -f  # live log view

4. Masking a service (complete disable, even manually)

sudo systemctl mask nginx     # creates a link to /dev/null
sudo systemctl unmask nginx   # removes the mask

Handy Shortcuts for Everyday Use

# Quickly verify if a service is enabled and running
systemctl is-active --quiet nginx && echo "OK" || echo "STOPPED"

# Start and enable autostart in one command
sudo systemctl enable --now nginx

# Disable and stop
sudo systemctl disable --now nginx

Note

The --now flag (available since systemd v220+, 2015) performs enable + start or disable + stop simultaneously.

question_mark
Is there anything I can help you with?
question_mark
AI Assistant ×