Sub topic 15
Inspect Services and Logs with systemd
Assume you can run commands with sudo, understand basic shell output, and know the name of the service you want to inspect. This chapter uses systemd, the service manager used by most current Debian, Ubuntu, Fedora, Rocky, and AlmaLinux systems.
A unit is a resource managed by systemd. A service unit describes a background program, such as an SSH server. The systemctl command controls units. The journalctl command reads systemd's event log, called the journal.
Check a service
Use systemctl status to see whether a service is running and to view recent messages:
systemctl status sshOn many RHEL-family systems, the service is named sshd instead:
systemctl status sshdThe status output includes:
Active: active (running), meaning the service is currently running.Loaded, showing whether systemd found the unit file.- The main process ID and recent log lines.
- Whether the service failed during startup.
The service name is not always the same as the package or command name. If you do not know the name, list running service units:
systemctl list-units --type=service --state=runningTo see whether a service starts automatically at boot, check its enabled state:
systemctl is-enabled sshenabled means systemd will start it during boot. disabled means it will not. These states are separate: a service can be enabled but currently stopped, or running but disabled for the next boot.
Read service logs
Show all journal entries for one service:
sudo journalctl -u sshLimit the output to the current boot:
sudo journalctl -u ssh -bFollow new entries as they arrive, similar to tail -f:
sudo journalctl -u ssh -fUseful time and priority filters include:
sudo journalctl -u ssh --since "30 minutes ago"
sudo journalctl -p warning -b
sudo journalctl -b -1The last command reads the previous boot's journal, if persistent logs are available.
Common errors and safe actions
| Observation | Likely meaning | Next action |
|---|---|---|
| `inactive (dead)` | The service is stopped | Check `journalctl -u NAME -b`, then start it if expected |
| `failed` | Startup or runtime failed | Read recent logs and validate its configuration |
| `disabled` | It will not start at boot | Enable it only if the server needs it |
| `Unit not found` | The name is wrong or not installed | List units or check the package |
Use systemctl restart only after understanding the impact. Restarting a network or database service can interrupt users.
Exercise
Which command shows errors from the current boot, and which command follows new messages for the SSH service?
Show answer
Use sudo journalctl -p err -b for current-boot errors and sudo journalctl -u ssh -f to follow SSH messages.