Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

systemd in Plain Words

systemd is the program that starts and manages everything else on a Linux server. The kernel runs it first, as process number 1, and from then on it starts services in the right order, restarts them when they fail, and stops them cleanly at shutdown.

ps -p 1 -o comm=

On Ubuntu, Debian, AlmaLinux, and Rocky Linux this prints systemd, so the same tools work on all four.

Units: What systemd Manages

Everything systemd manages is a unit, described by a small text file. The suffix of the name tells the type:

TypeExampleManages
servicenginx.serviceA daemon
socketssh.socketA listening socket
timerlogrotate.timerA schedule
mounttmp.mountA mounted file system
targetmulti-user.targetA group of units

Packages install their unit files under /usr/lib/systemd/system; your own files and overrides go in /etc/systemd/system, which takes priority. How to write one is shown in systemd unit files, and what a service runs is explained in what is a daemon.

Targets Instead of Runlevels

Older init systems booted into numbered runlevels. systemd uses targets, which are named groups of units:

TargetOld runlevelMeaning
multi-user.target2, 3, 4Normal server, no desktop
graphical.target5With a desktop
rescue.target1Single user, for repairs
reboot.target6Restart

A server normally boots into multi-user.target; systemctl get-default shows which target yours uses.

systemctl in Six Commands

CommandWhat it does
systemctl status nginxState, process ID, last log lines
systemctl start nginxStarts it now; stop stops it
systemctl restart nginxStops and starts it
systemctl reload nginxRereads the configuration without a stop
systemctl enable --now nginxStarts it now and at every boot
systemctl --failedLists units that failed

Commands that change something need sudo. After you edit a unit file, run sudo systemctl daemon-reload so systemd reads the new version. Filters for long lists are in how to list services with systemctl.

The Journal

systemd collects the output of every service, plus kernel and boot messages, in one log called the journal, kept by systemd-journald. Read it with journalctl:

sudo journalctl -u ssh -n 50
sudo journalctl -b -p err

The first shows the last 50 lines from the SSH service (sshd on AlmaLinux and Rocky Linux), the second every error since the last boot. Details are in journalctl.

Timers

A timer unit starts a service on a schedule, like a cron job. systemctl list-timers shows every timer with its last and next run. The two approaches are compared in what is cron.

What systemd Replaced

Before systemd, most distributions used SysV init, with shell scripts in /etc/init.d, and Ubuntu used Upstart. Where the service command is installed, the old form service nginx restart still works and passes the request on to systemctl. systemd also brought small tools for settings that used to mean editing files: hostnamectl (see how to change the hostname in Ubuntu), timedatectl, and systemd-analyze.

When a Service Does Not Start

  1. Run systemctl status with the service name and read the last log lines.
  2. Check the configuration with the program's own test, such as sudo nginx -t.
  3. Read more of the log with journalctl -u and the service name.
  4. After a reboot, run systemctl --failed; systemd-analyze blame shows which units slowed the boot.

A safe restart routine is in how to reboot a Linux server.

Was this answer helpful?
Loading...