Sub topic 15
Inspect Services and Logs with systemd
This chapter covers how to see what a fresh server is running and how to read its logs. It assumes you can use a shell, run commands with sudo, and have finished the first-login steps from earlier chapters. The commands apply to Debian, Ubuntu, Rocky, Alma and Fedora, which all use systemd.
The working model
systemd is the init system, the first process (PID 1) the kernel starts. It starts and supervises everything else. It manages units, which are described by configuration files. The unit types you meet most often are:
- A service unit (
ssh.service) runs a daemon. - A socket unit (
ssh.socket) listens on a port and starts a service when a connection arrives. - A timer unit (
apt-daily.timer) starts another unit on a schedule, like cron. - A target unit (
multi-user.target) groups units into a boot state.
Two properties of a unit are easy to confuse:
| Property | Question it answers | Commands |
|---|---|---|
| Active state | Is it running right now? | `start`, `stop`, `restart`, `reload` |
| Enabled state | Will it start at boot? | `enable`, `disable` |
A unit can be running but not enabled, so it disappears after the next reboot. It can also be enabled but not running, for example after a crash.
Inspect services
Start with an overview of what is running and what has failed.
systemctl list-units --type=service --state=running
systemctl --failed
systemctl list-unit-files --type=service --state=enabledThe first command lists running services. The second lists units that failed. The third lists services configured to start at boot. Comparing the first and third lists shows what is enabled but not running, and the reverse.
Then examine one service in detail. The service name is ssh on Debian and Ubuntu and sshd on the RHEL family.
systemctl status ssh
systemctl is-active ssh
systemctl is-enabled sshsystemctl status prints the state, the main PID, the unit file path, memory use, and the last few log lines. The is-active and is-enabled commands print one word and set an exit code, so they work in scripts.
systemctl cat sshsystemctl cat shows the unit file together with any drop-in overrides. Do not edit files under /usr/lib/systemd/system/, because package updates replace them. Put changes in a drop-in instead:
sudo systemctl edit sshThis opens an editor and saves your override to /etc/systemd/system/ssh.service.d/override.conf.
Check what is listening
A service that is running may not be reachable, and an unexpected listener is a security concern. Compare the port list with what you intended to run.
ss -tlnpEach line shows an address, a port and the owning process. An address of 127.0.0.1 is reachable only from the server itself. 0.0.0.0 and [::] mean all interfaces. A database listening on 0.0.0.0 that you did not plan for is a finding to fix.
Disable what you do not need
A minimal server should run few services. Stop and disable an unneeded one in a single step:
sudo systemctl disable --now cups.serviceThe --now flag applies the change immediately as well as at boot. Use mask to stop a unit from being started at all, even as a dependency of another unit:
sudo systemctl mask bluetooth.service
sudo systemctl unmask bluetooth.serviceRead logs with journalctl
systemd collects logs from services, the kernel and the boot process in the journal. You query it with journalctl. Your admin user may need to be in the systemd-journal group (or adm on Debian) to read all entries without sudo.
journalctl -b
journalctl -u ssh
journalctl -u ssh --since "1 hour ago"
journalctl -p err -b
journalctl -f
journalctl -k-blimits output to the current boot.-b -1selects the previous boot.-ufilters by unit.--sinceand--untilaccept times such as"2026-11-28 03:00",yesterday, or"30 min ago".-p errshows entries of priorityerrand more severe. Priorities run fromemerg(0) todebug(7).-ffollows new entries as they arrive, liketail -f.-kshows kernel messages only.
You can combine filters. This command shows warnings and worse from the SSH service since the last boot, newest last:
journalctl -u ssh -p warning -b --no-pagerA worked example: find a failed login
After you harden SSH, rejected logins appear in the journal. Follow the log in one terminal and attempt a login with a wrong user from another:
journalctl -u ssh -fYou will see lines such as Invalid user test from 203.0.113.9. On an internet-facing server, expect many such lines within minutes from automated scanners. That is normal. It is a reason to keep password login off and the firewall on.
Journal storage and size
On some systems the journal is kept only in memory and is lost at reboot. This happens when /var/log/journal does not exist. Debian and Ubuntu server installs usually create it. Check the current state and disk use:
journalctl --disk-usage
ls -d /var/log/journalTo keep logs across reboots, set persistent storage and a size cap in a drop-in file:
# /etc/systemd/journald.conf.d/10-persistent.conf
[Journal]
Storage=persistent
SystemMaxUse=500MThen restart the journal daemon and confirm that earlier boots are listed:
sudo systemctl restart systemd-journald
journalctl --list-bootsYou can also trim old entries manually with sudo journalctl --vacuum-time=14d.
Diagnose a failed service
When a unit fails, work through the same sequence each time.
- List failures with
systemctl --failed. - Read
systemctl status UNITfor the exit code and the last log lines. - Read the full log with
journalctl -u UNIT -b --no-pager. - Fix the cause, which is usually a configuration error, a missing file, a port already in use, or a permissions problem.
- Clear the failed state with
sudo systemctl reset-failed UNIT, then start the unit again.
Many daemons can test their own configuration before a restart. You used sshd -t earlier for this reason. A service that has a start-rate limit may stay failed with the message start request repeated too quickly. Fixing the cause and running reset-failed clears that state.
Common errors
| Symptom | Likely cause | What to check | Column |
|---|---|---|---|
| `Unit ssh.service not found` | Wrong service name for the distribution | Use `sshd` on RHEL family; run `systemctl list-unit-files \ | grep ssh` |
| Service active but unreachable | Firewall, or it listens on `127.0.0.1` | `ss -tlnp` and the firewall status | |
| Service not running after reboot | Started but never enabled | `systemctl is-enabled UNIT` | |
| `journalctl` shows "No journal files were found" | Missing permissions or no persistent storage | Use `sudo`; check `/var/log/journal` | |
| Change to a unit file has no effect | Configuration not reloaded | `sudo systemctl daemon-reload`, then restart | |
| Only the last boot appears | Journal is volatile | Set `Storage=persistent` |
Exercise
A web service was started with systemctl start, and it worked. After a reboot it is not running, and systemctl status shows inactive (dead). What is the likely cause, which single command fixes it for the future, and which command shows the previous boot's log for that unit?
Show answer
The unit was started but not enabled, so it did not start at boot. Run sudo systemctl enable UNIT (or enable --now to also start it immediately). To read the previous boot's log, run journalctl -u UNIT -b -1, which requires a persistent journal.
Exercise
You run ss -tlnp on a new server and see a database process listening on 0.0.0.0:5432. You only need local access. Which command confirms the owning service, and what is the safest step to take before changing its configuration?
Show answer
The -p output names the process, and systemctl status on that PID or systemctl list-units --type=service --state=running identifies the unit. Before changing it, run the daemon's configuration test if it has one and keep a copy of the original file. Then set the listen address to 127.0.0.1, restart the unit, and check ss -tlnp again.