Managing services with systemd
Goal
Start, stop and enable a service, read its state and its logs. This is the single most frequent exam task.
Lifecycle
systemctl status sshd
systemctl start sshd
systemctl enable --now sshd # enabled at boot AND started now
systemctl disable --now sshd
systemctl restart sshd
systemctl reload sshd # re-read config without dropping connections
systemctl mask cups # forbid any start, even by dependency
Inspect
systemctl list-units --type=service --state=running
systemctl list-unit-files | grep enabled
systemctl cat sshd # unit content + drop-ins
systemctl show sshd -p MainPID
systemctl is-enabled sshd; systemctl is-active sshd
Changing a unit without breaking it
Never edit /usr/lib/systemd/system/*.service. Use a drop-in:
systemctl edit sshd # creates /etc/systemd/system/sshd.service.d/override.conf
systemctl daemon-reload
[Service]
Restart=always
Journal
journalctl -u sshd -n 50 --no-pager
journalctl -u sshd --since "10 min ago"
journalctl -p err -b # errors since current boot
journalctl -f # live follow
Persistence: /etc/systemd/journald.conf → Storage=persistent then systemctl restart systemd-journald.
Verify
systemctl is-enabled sshd
journalctl -u sshd -n 3 --no-pager