Writing and overriding unit files
Where unit files live, how drop-ins change a vendor unit without editing it, and what Type=, Restart= and template units with @ really do.
Writing and overriding unit files
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Packages put units in /usr/lib/systemd/system. Your changes go in /etc/systemd/system, which wins over /usr/lib for a file with the same name.
- A drop-in, foo.service.d/override.conf, changes only the settings it names, so package updates to the rest of the unit still reach you. `systemctl edit` creates one.
- List settings like ExecStart= add up across files. To replace one in a drop-in, assign it empty first, then set the new value.
- Dependencies such as After= can only be added in a drop-in, never removed. Removing one needs a full copy of the unit.
- systemd reads unit files into memory. After editing files by hand, run `systemctl daemon-reload` before restart, or the old definition is still in use.
- Type= says when a service counts as started. The default, simple, counts it started at fork, before the binary even runs. exec or notify are more honest.
- Restart=on-failure restarts after a crash, a non-zero exit or a timeout, but never after you ran `systemctl stop`.
- A template unit name@.service is instantiated as name@value.service, and %i inside the file is replaced by the value.
Editing a file the package owns
You install a log daemon and want it to keep more files open. You find its unit at /usr/lib/systemd/system/rsyslog.service, add a line, restart, and it works. Then a security update for the package lands, the file is replaced, and your change is silently gone.
systemd’s answer is a layered set of directories, where your layer sits above the package’s and only says what differs. Get this right and package updates and local changes stop fighting.
Where units come from
systemd.unit(5) lists the search path. Trimmed to the directories you will touch:
| Directory | Who writes it | Notes |
|---|---|---|
/etc/systemd/system/ |
You, the administrator | Highest priority of the persistent ones |
/run/systemd/system/ |
Runtime tools | Gone at reboot |
/usr/local/lib/systemd/system/ |
Locally installed software | |
/usr/lib/systemd/system/ |
Distribution packages | Never edit by hand |
A file found earlier in the list wins over a file with the same name further down. So copying a unit to /etc/systemd/system/ overrides it completely: the vendor file is not even parsed.
Drop-ins: change only what you mean to
Next to any unit foo.service, a directory foo.service.d/ may hold .conf files, read in name order, each adding or changing settings. They are parsed after the main unit file, and a drop-in with the same name in /etc beats one in /run, which beats one in /usr/lib.
systemctl edit foo.service creates /etc/systemd/system/foo.service.d/override.conf, opens your editor and reloads systemd when you save. systemctl edit --full instead creates a full copy in /etc. systemctl cat foo.service prints the unit and every drop-in, each with its path, which is the first command to run when a service behaves differently from its file.
Debian ships a real drop-in in its own systemd package. I downloaded Debian 13’s systemd 257.13 package and unpacked it on this Mac. rc-local.service.d/debian.conf contains:
[Unit]
# not specified by LSB, but has been behaving that way in Debian under SysV
# init and upstart
After=network-online.target
# Often contains status messages which users expect to see on the console
# during boot
[Service]
StandardOutput=journal+console
StandardError=journal+console
Two rules decide what a drop-in can do, both from systemd.unit(5):
-
List settings accumulate.
ExecStart=,Environment=and friends add to what is already there. To replace, assign it empty first:[Service] ExecStart= ExecStart=/usr/sbin/rsyslogd -n -iNONE -dWithout the blank line you get two
ExecStart=commands, which is an error for anything butType=oneshot. -
Dependencies cannot be removed.
After=,Wants=and the rest can only be added in a drop-in. To take one away you have to copy the whole unit to/etc.
daemon-reload
systemd parses unit files into memory. Editing a file on disk changes nothing until systemctl daemon-reload, which in the man page’s words reruns generators, reloads all unit files and recreates the dependency tree. Then restart the service so its process picks up the new definition. systemctl edit reloads for you, which is one more reason to use it.
Type=: when is a service “started”?
The previous lesson said After= waits until a unit has finished starting. Type= defines what that means:
simple(the default whenExecStart=is set): started the moment systemd forks, before the binary runs. A missing binary or badUser=still reports success fromsystemctl start.exec: started once the binary has been executed. The man page says thatexecis typically the better choice.notify: the daemon sendsREADY=1throughsd_notifywhen it is ready to serve. The most accurate, but the program must support it.notify-reloadadds a reload protocol on top.forking: for old daemons that fork and let the parent exit. The man page discourages it.oneshot: runs to completion, the shape for jobs and scripts. WithRemainAfterExit=yesit shows as active afterwards.
Restart=
Restart=no is the default. on-failure restarts after a non-zero exit, a crash by signal, a timeout or a watchdog miss. always restarts whatever the reason. None of them restart after systemctl stop. Restarts are rate-limited by StartLimitIntervalSec= and StartLimitBurst=, so a service that crashes in a tight loop ends up failed rather than spinning forever.
The Debian rsyslog unit from the same download combines the two:
[Service]
Type=notify
ExecStart=/usr/sbin/rsyslogd -n -iNONE
Restart=on-failure
LimitNOFILE=16384
The -n keeps rsyslogd in the foreground, which is what systemd wants: it supervises the process directly instead of chasing a forked child.
Template units
A unit with @ before the suffix is a template. getty@.service is never started as is. getty@tty1.service and getty@tty2.service are instances of it, and inside the file %i is replaced by the instance name and %I by the same with escaping undone. From the same Debian package:
[Service]
ExecStart=-/sbin/agetty -o '-- \\u' --noreset --noclear - ${TERM}
UtmpIdentifier=%I
TTYPath=/dev/%I
...
[Install]
WantedBy=getty.target
DefaultInstance=tty1
Twenty-four of the 210 unit files in that package are templates. The pattern fits anything you run several copies of with one parameter: app@8001.service, app@8002.service.
About the video
The embed is a clip, 15:58 to 43:06, of Learn Linux TV’s “Systemd Explained: How to Manage Linux Services Easily”, June 2023. The clip’s chapters are “Where are Systemd’s Unit files stored?”, “Systemd unit file directory priority/preference”, “Taking a closer look at a Systemd Service File”, “How reload differs from restart”, “Editing and Overriding Systemd Unit Files” and “Reloading Systemd with systemd daemon-reload”. It skips the start/stop/enable basics already covered in the devops systemd lesson, and stops before the closing tips. From the chapter list it does not cover template units, the empty-assignment rule or Type= choices in depth, so the lesson does. The full video is a “go deeper” link in the devops lesson, not an embed there. English was judged from the channel and its English caption tracks. I judged the content from the chapters, not a full watch.
What was run: downloading and unpacking the Debian 13 systemd and rsyslog packages and reading their unit files on macOS. Nothing here was started under systemd. Behaviour is from the systemd 262 man pages, checked 2026-11-28.
Where this fits
A service that runs on a schedule rather than all the time is a oneshot service plus a timer. That is next: systemd timers.
Check yourself
You put a drop-in with `ExecStart=/usr/bin/myapp --verbose` on a service that already has an ExecStart=. Starting it fails with an error about more than one ExecStart=. Why, and what is the fix?
ExecStart= is a list, so the drop-in added a second command, and only Type=oneshot may have more than one. Put an empty `ExecStart=` line first to clear the list, then the new one.
You edited /etc/systemd/system/app.service with vi and ran `systemctl restart app`. The change has no effect. What did you skip?
systemctl daemon-reload. systemd works from its in-memory copy of unit files until told to reload them. systemctl edit does the reload for you.
Why prefer a drop-in over copying the whole vendor unit to /etc?
A copy replaces the vendor unit entirely, so fixes the package ships later never reach you. A drop-in changes only the lines you care about.
A service with Type=simple and a typo in ExecStart= reports success from `systemctl start`. Why?
With simple, systemd considers the unit started as soon as it forks, before execve() runs the binary. Type=exec waits until the binary has been executed, so the error shows up at start.
getty@.service is started as getty@tty2.service. What does %I expand to?
tty2, the instance name between the @ and the suffix, with escaping undone.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.