Skip to main content
RunBook Academy

LinuxVII · systemd and Service ManagementUnit management

systemctl and unit management

Foundation⏱ ~10 minbashsystemctljournalctl

What you'll learn

  • Distinguish start/stop/restart from enable/disable
  • Use systemctl status, is-active, is-enabled, is-failed
  • Read the active state and sub-state of any unit
  • Mask a unit to prevent accidental activation

Prerequisites

Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09

Not yet marked complete on this device.

systemctl is the interface to systemd. Two pairs of verbs do most of the work: start / stop for runtime, enable / disable for boot. Understanding the distinction is the foundation of every operational change.

Start, stop, restart, reload

Service impact possibleruntime verbs
$ sudo systemctl start sshd; sudo systemctl stop sshd; sudo systemctl restart sshd; sudo systemctl reload sshd

Illustrative output

VerbEffect on running stateEffect on boot
startactivates nownothing
stopdeactivates nownothing
restartstops, startsnothing
reloadruns the unit’s ExecReload=; fails if the unit defines nonenothing
enablenothingactivates at boot
disablenothingdeactivates at boot

enable, disable, mask, unmask

Service impact possibleboot verbs
$ sudo systemctl enable sshd; sudo systemctl disable sshd; sudo systemctl mask sshd; sudo systemctl unmask sshd

Illustrative output

Reading state

Read-only / Safesystemctl status
$ systemctl status sshd
● sshd.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2026-08-04 11:23:45 UTC; 5 days ago
Main PID: 12345 (sshd)
Tasks: 5 (limit: 23456)
Memory: 12.4M
CPU: 5.230s
CGroup: /system.slice/sshd.service
└─12345 /usr/sbin/sshd -D
...

Illustrative output

Read-only / Safeis-active / is-enabled / is-failed
$ systemctl is-active sshd; systemctl is-enabled sshd; systemctl is-failed sshd
active
enabled
active

Illustrative output

Read-only / Safeis-failed both ways
$ systemctl is-failed sshd; echo "exit=$?"; systemctl is-failed broken.service; echo "exit=$?"
active
exit=1
failed
exit=0

Illustrative output

Script it on the exit code, never on the string:

# Correct: exit 0 means "this unit is in the failed state"
if systemctl is-failed --quiet "$unit"; then
    alert "$unit has failed"
fi

# WRONG - this alert never fires. A failed unit prints
# 'failed', not 'inactive', so the comparison is never true.
# It is also silently wrong the other way: a unit that is
# merely stopped prints 'inactive' and would alert falsely.
if [ "$(systemctl is-failed "$unit")" = "inactive" ]; then
    alert "$unit has failed"
fi

--quiet suppresses the state word so the exit code is the only output, which is what a check script wants. For the fleet-wide form, ask systemd for the whole list rather than looping:

Read-only / Safeevery failed unit at once
$ systemctl list-units --state=failed --no-legend
nginx.service loaded failed failed A high performance web server

Illustrative output

Clearing a failed state with reset-failed

A unit that has failed stays failed. systemd remembers it, and it also remembers how many times the unit has been started recently. Exceed the start rate limit — StartLimitBurst starts within StartLimitIntervalSec, 5 in 10 seconds on a stock system, both directives living in [Unit] — and systemd refuses every further start and restart, including the one you issue after fixing the problem.

Service impact possiblereset-failed then start
$ sudo systemctl reset-failed myapp; sudo systemctl start myapp
Active: active (running) since Mon 2026-08-10 09:14:02 UTC

Illustrative output

Listing and filtering

Read-only / Safelist-units
$ systemctl list-units --type=service; systemctl list-units --state=failed; systemctl list-unit-files --type=service | head
UNIT                         LOAD   ACTIVE SUB     DESCRIPTION
accounts-daemon.service      loaded active running Accounts Service
chrony.service                loaded active running chrony, an NTP client/server
...

Illustrative output

Production patterns

Read-only / Safepost-boot health check
$ systemctl list-units --state=failed --no-pager
0 loaded units listed.

Illustrative output

Read-only / Safereverse deps
$ systemctl list-dependencies --reverse sshd
sshd.service
● └─runlevel2.target
...

Illustrative output

Knowledge check

Knowledge check · 5 questions

  1. Q1. What is the difference between `disable` and `mask`?

  2. Q2. A unit that has been enabled but never started will not be running until the next boot.

  3. Q3. Which of the following are correct systemd management practices? Select all that apply.

  4. Q4. A monitoring script contains `if [ "$(systemctl is-failed nginx)" = "inactive" ]; then alert; fi`. nginx has been in the failed state for three days and no alert has fired. Why?

  5. Q5. You are writing a post-deploy gate that must fail the pipeline if anything on the host is broken. Which check is the most reliable?

Passing score: 75%. Answers are checked in this browser.