Skip to main content

systemd

systemd is the init system and service manager used by most modern Linux distributions. As PID 1 it starts the system at boot and manages services, logging, timers, and mounts.

Understanding systemd

What Is systemd?

When the kernel finishes booting it starts exactly one process, PID 1, which starts everything else. On Ubuntu, Debian, RHEL, Amazon Linux, and others that process is systemd. It replaced SysV init's sequential shell scripts with declarative unit files, parallel startup, automatic restarts, and structured logs through journald.

How systemd Boots a System

systemd reads unit files, resolves their dependencies (After=, Requires=, Wants=), and starts independent units in parallel until it reaches the default target, usually multi-user.target on servers.

flowchart TD A[Kernel starts systemd] --> B[Read unit files] B --> C[Resolve dependencies] C --> D[Start units in parallel] D --> E([multi-user.target reached])

Unit files live in /usr/lib/systemd/system/ (installed by packages) and /etc/systemd/system/ (admin overrides, which take priority).

Common Unit Types

TEXT
.service a daemon or one-shot task
.timer scheduled run (alternative to cron)
.socket starts a service on first connection
.mount a mount point
.target a group of units, like a runlevel
.path triggers on filesystem changes

Essential systemctl Commands

Bash
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx # re-read config if the unit supports it
sudo systemctl enable --now nginx # start at boot and now
sudo systemctl disable nginx
systemctl status nginx
systemctl list-units --type=service --state=failed
sudo systemctl daemon-reload # after editing unit files
systemd-analyze blame # slowest boot units

How to Read Logs With journalctl

Bash
journalctl -u nginx -f # follow
journalctl -u nginx -n 50 --no-pager
journalctl -u nginx --since "1 hour ago"
journalctl -u nginx -p err # errors and worse
journalctl -u nginx -xe # explanatory context

Troubleshooting systemd

Symptom Command What to Look For
Service will not start journalctl -u name -n 50 Last error output
Restart loop systemctl status name Exit code, restart count
Unit edit ignored sudo systemctl daemon-reload Reload required
Slow boot systemd-analyze blame Longest units
Tip

Start with systemctl status name, then journalctl -u name -xe for the full story. Status often truncates the useful line.

Common Mistake

Editing a packaged unit under /lib/systemd/system. A package upgrade overwrites it. Use systemctl edit name to create a drop-in override instead.

Remember

After changing any unit file, run sudo systemctl daemon-reload. Until you do, systemd keeps using the old definition.

Frequently Asked Questions

What did systemd replace and why?

It replaced SysV init, and on some distributions Upstart. Those started services sequentially with shell scripts. systemd starts independent services in parallel, expresses dependencies declaratively, supports socket activation, and handles restarts and resource limits in unit files.

What is the difference between reload and restart?

Restart stops and starts the service, dropping connections. Reload asks the running service to re-read its configuration, typically through a signal such as SIGHUP configured in `ExecReload=`, and only works if the unit defines it.

Can systemd replace cron?

For many jobs, yes. Timer units add journal logging, dependency ordering, and catch-up of missed runs, though cron stays simpler for one-line recurring commands.

Found a mistake, an outdated step or a broken link?