TExcelTAD
Home › Linux Server Administration › systemd Units & Unit Files

systemd Units & Unit Files

On almost every current Linux server, PID 1 is systemd. It starts everything else, supervises it, collects its logs and stops it in the right order at shutdown. Everything systemd manages is a unit, described by a small INI-style unit file. cron & systemd showed a first service and timer; this lesson explains the model behind them.

Unit types

Suffix Manages Example
.service a process (daemon or one-shot job) nginx.service, ssh.service
.socket a listening socket that starts a service on first connection ssh.socket on Ubuntu 24.04
.timer a schedule that starts a service logrotate.timer, apt-daily.timer
.target a named group of units, a synchronisation point multi-user.target, network-online.target
.mount / .automount a mount point (generated from /etc/fstab too) data.mount for /data
.path watches a file or directory and starts a service on change certbot-reload.path
.slice / .scope cgroup groups for resource control system.slice, user-1000.slice
.device, .swap kernel devices and swap space dev-sda.device

The name of a mount unit is derived from its path (/var/lib/data → var-lib-data.mount), which is why you will see oddly escaped names in systemctl list-units.

Where unit files live

systemd reads unit files from several directories. When the same name exists in more than one, the first one in this order wins:

Directory Owner Use
/etc/systemd/system/ you, the admin your own units and full overrides
/run/systemd/system/ runtime generated at boot, gone after reboot
/usr/lib/systemd/system/ (also /lib/…) the package manager units shipped by packages: never edit these

So you never edit /usr/lib/systemd/system/nginx.service: the next package upgrade would overwrite your change. You either copy it to /etc/systemd/system/ (a full override) or, much better, add a drop-in that changes a few settings (Drop-ins & Overrides). User services (systemctl --user) have their own paths under ~/.config/systemd/user/.

Anatomy of a unit file

ini
[Unit]
Description=Inventory API
Documentation=https://git.example.com/acme/inventory
After=network-online.target postgresql.service
Wants=network-online.target

[Service]
Type=simple
User=inventory
ExecStart=/opt/inventory/bin/inventory --port 8000
Restart=on-failure

[Install]
WantedBy=multi-user.target
  • [Unit]: generic metadata and relationships to other units (ordering, dependencies, conditions). Every unit type has it.
  • [Service] (or [Timer], [Socket], [Mount]…): the type-specific part. Writing a Service Unit covers the service keys.
  • [Install]: only read by systemctl enable. WantedBy=multi-user.target means "when enabled, create a symlink so that multi-user.target pulls this unit in at boot".

Keys are case-sensitive, values are not quoted (except inside command lines), and a key can repeat when the setting is a list (Environment=, ExecStartPre=).

Checking a unit file before you install it

systemd-analyze verify loads a unit file the way systemd would and reports problems. It works without a running systemd, which makes it the right tool for CI and for the sessions here:

bash · try it yourself
cat > inventory.service <<'EOF'
[Unit]
Description=Inventory API
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 -m http.server 8000
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF
systemd-analyze verify ./inventory.service && echo "OK"
OK

Silence means success. Now the typos everyone makes at least once:

bash · try it yourself
cat > typo.service <<'EOF'
[Unit]
Description=Inventory API
Afer=network-online.target

[Service]
Type=simpel
ExecStart=/usr/bin/python3 -m http.server 8000
Restart=on_failure
EOF
systemd-analyze verify ./typo.service
echo "exit code: $?"
/home/learner/typo.service:3: Unknown key name 'Afer' in section 'Unit', ignoring.
/home/learner/typo.service:6: Failed to parse service type, ignoring: simpel
/home/learner/typo.service:8: Failed to parse service restart specifier, ignoring: on_failure
exit code: 0

Read the result carefully: these are warnings, the exit code is 0, and systemd would still start the unit, silently ignoring the misspelled lines. The service would run without the ordering you wanted, as the default type, and never restart. In CI, fail the check when verify prints anything:

bash · try it yourself
cat > typo.service <<'EOF'
[Service]
ExecStart=/usr/bin/python3 -m http.server 8000
Restat=always
EOF
out=$(systemd-analyze verify ./typo.service 2>&1)
if [ -n "$out" ]; then echo "$out"; echo "unit check FAILED"; else echo "unit check passed"; fi
/home/learner/typo.service:3: Unknown key name 'Restat' in section 'Service', ignoring.
unit check FAILED

Real errors (not warnings) make verify exit non-zero, for example a service without any ExecStart=:

bash · try it yourself
printf '[Unit]\nDescription=Nothing to run\n[Service]\nType=simple\n' > empty.service
systemd-analyze verify ./empty.service
echo "exit code: $?"
empty.service: Service has no ExecStart=, ExecStop=, or SuccessAction=. Refusing.
Unit empty.service has a bad unit file setting.
exit code: 1

Looking at units on a live server

On a real server (sample session) you list and inspect units like this:

bash
$ systemctl list-units --type=service --state=running | head -5
  UNIT                     LOAD   ACTIVE SUB     DESCRIPTION
  cron.service             loaded active running Regular background program processing daemon
  nginx.service            loaded active running A high performance web server and a reverse proxy server
  ssh.service              loaded active running OpenBSD Secure Shell server
  systemd-journald.service loaded active running Journal Service
$ systemctl cat nginx.service | head -3
# /usr/lib/systemd/system/nginx.service
[Unit]
Description=A high performance web server and a reverse proxy server
$ systemctl list-unit-files --state=enabled | wc -l
47

systemctl cat is the one to remember: it prints the unit file and every drop-in, each with its path as a comment, so you see exactly what systemd loaded.

Test yourself

Where should you put your own unit file for an app you deploy?

  1. /usr/lib/systemd/system/
  2. /etc/systemd/system/
  3. /run/systemd/system/
  4. /var/lib/systemd/

systemd-analyze verify prints Unknown key name 'Restat' and exits with 0. What would systemd do with that unit?

  1. Refuse to start it
  2. Start it and ignore the unknown line, so the service would never be restarted
  3. Fix the typo automatically
  4. Restart it always

What does the [Install] section's WantedBy=multi-user.target do?

  1. Starts the service immediately
  2. Tells systemctl enable to link the unit into multi-user.target, so it starts at boot
  3. Orders the unit after multi-user.target
  4. Nothing on servers