1. Understand a systemd unit

A service unit describes how systemd starts, stops and supervises a process. A unit can be enabled at boot, active now, both, or neither.

systemctl status ssh
systemctl is-enabled ssh
systemctl is-active ssh
systemctl list-units --type=service --state=failed
中文说明:enabled 表示配置为开机启动;active 表示当前处于运行状态,两者不是同一个概念。

2. Read logs with journalctl

sudo journalctl -u ssh --since "1 hour ago"
sudo journalctl -u ssh -n 100 --no-pager
sudo journalctl -u ssh -f
sudo journalctl -p warning..alert -b

Use -u to filter by unit, --since to narrow the time window, -b for the current boot and -f to follow new entries.

3. Diagnose a failed service

systemctl status example.service --no-pager
sudo journalctl -u example.service -b --no-pager
systemctl cat example.service
systemctl show example.service -p ExecStart -p User -p Restart

Look for the first meaningful error, not only the final “failed” line. Check executable paths, permissions, environment files, ports and dependency ordering.

4. Validate a unit before changing state

sudo systemd-analyze verify /etc/systemd/system/example.service
sudo systemctl daemon-reload

Run daemon-reload after editing unit files so systemd re-reads definitions. It does not itself restart the service.

5. Safe service lifecycle

sudo systemctl reload example.service
sudo systemctl restart example.service
sudo systemctl enable --now example.service
Operational caution: use reload only if the service supports it. Before restarting a remote access, firewall, proxy or networking service, confirm an alternate recovery path and understand the impact on active sessions.

6. Check boot performance

systemd-analyze blame | head -20
systemd-analyze critical-chain

Boot timing reports help locate slow units, but the slowest unit is not automatically the root cause of every startup delay.

Field checklist

  • Capture status and recent logs first.
  • Identify the first root error and its timestamp.
  • Validate configuration before reload/restart.
  • Verify service state and application health afterward.

References & further reading

Commands are examples for a representative Ubuntu/Linux environment. Review paths, versions, access requirements and maintenance impact before applying them to production systems.