systemd is the init system on nearly every mainstream Linux distribution. It is the first process the kernel starts (PID 1) and it owns the lifecycle of every service on the machine.
Units
systemd models everything it manages as a unit. A unit is described by a plain-text file, and its suffix tells systemd what kind of thing it is.
.service - a daemon or one-shot command
.socket - a listening socket that can start its service on demand
.timer - a scheduled trigger, the modern replacement for cron
.mount / .automount - a filesystem mount point
.target - a grouping of units, roughly what runlevels used to be
Where unit files live
Three directories hold unit files, in increasing order of priority. A file in a higher-priority directory completely replaces one of the same name below it.
/usr/lib/systemd/system/ # shipped by packages - do not edit
/run/systemd/system/ # runtime, volatile
/etc/systemd/system/ # local administrator overrides - edit hereNever edit a unit file under /usr/lib/systemd/system directly: the next package upgrade will overwrite it. Use `systemctl edit` to create a drop-in override instead.
A minimal service
[Unit]
Description=Example API server
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/usr/local/bin/example-api --port 8080
Restart=on-failure
RestartSec=5s
User=example
[Install]
WantedBy=multi-user.targetThe three sections map onto three questions: when should this run relative to other units, how is the process started, and what should pull it in at boot.
systemd is PID 1 and manages every service as a unit.
Unit type is determined by the file suffix: .service, .socket, .timer, .target.
Local overrides belong in /etc/systemd/system, never in /usr/lib.
[Unit] sets ordering, [Service] sets how to run, [Install] sets what enables it.
Check yourself
Which directory should hold a unit file you wrote yourself, so a package upgrade cannot overwrite it?
You edited /etc/systemd/system/example-api.service but `systemctl restart example-api` still runs the old command line. Explain why, and give the command that fixes it.