Telling Systemd What To Do
How does the conductor know what music to play? It reads sheet music. How does systemd know how to run a web server, mount a filesystem, or schedule a backup? It reads Unit Files.
A Unit is any resource that systemd knows how to manage. A unit file is a plain text configuration file — no scripting, no logic — that describes that resource: what it is, how to start it, what it depends on, and when it should run.
Where unit files live:
| Path | Purpose | Edit? |
|---|---|---|
/lib/systemd/system/ |
Installed by packages (e.g., apt install nginx) |
Never |
/etc/systemd/system/ |
Created by the system administrator | Yes |
~/.config/systemd/user/ |
Per-user units (no root required) | Yes |
Files in /etc/systemd/system/ take precedence over /lib/systemd/system/. This is how you customize vendor-provided units without touching the originals.
The Anatomy of a Unit File
Unit files are divided into sections, each in square brackets — [Unit], [Service], and [Install] is the standard three-section skeleton nearly every service unit follows.
Breaking down each section:
-
[Unit]— Metadata and dependency declarations.Descriptionis what appears insystemctl status.After=network.targetmeans "don't start me until the network is up." This controls ordering, not requirement — the distinction matters and is covered in the dependency section below. -
[Service]— The operational details.ExecStartis the full path to the command that starts the service.Restart=alwaystells systemd to restart the process if it exits for any reason.RestartSec=5adds a 5-second delay before restarting, preventing a crash-loop from hammering the CPU. -
[Install]— Defines when this unit should be activated.WantedBy=multi-user.targetmeans "when the system reaches multi-user mode (normal server operation), include me." This section is only consulted when you runsystemctl enable.
The Type= directive in [Service] is worth understanding. Type=simple (the default) means systemd considers the service started as soon as ExecStart launches. Type=forking means the process forks into the background and the parent exits — the old Unix daemon pattern. Type=oneshot means the process runs to completion and exits; systemd waits for it to finish before considering the unit "active." This is used for scripts and batch jobs.