Drop-ins & Overrides
Packages ship unit files in /usr/lib/systemd/system/. You will constantly want to change one or two settings in them: more memory for PostgreSQL, a different restart policy for Nginx, an extra environment variable. Copying the whole file to /etc/systemd/system/ works, but then you own all of it, and the fixes in the next package version never reach you. A drop-in changes only what you name.
How drop-ins work
For a unit nginx.service, systemd also reads every *.conf file in a directory called nginx.service.d/, in any of the unit search paths, sorted by file name, after the main file:
/usr/lib/systemd/system/nginx.service ← from the package
/etc/systemd/system/nginx.service.d/
10-limits.conf ← yours, applied first
50-restart.conf ← yours, applied second (wins on conflicts)Each drop-in is a partial unit file: section headers plus the keys you want to change.
# /etc/systemd/system/nginx.service.d/50-restart.conf
[Service]
Restart=always
RestartSec=3s
LimitNOFILE=65536The easy way to create one is systemctl edit, which opens an editor on /etc/systemd/system/<unit>.d/override.conf and runs daemon-reload when you save (sample session):
$ sudo systemctl edit nginx.service # writes nginx.service.d/override.conf
$ systemctl cat nginx.service | grep -A4 override
# /etc/systemd/system/nginx.service.d/override.conf
[Service]
Restart=always
RestartSec=3s
LimitNOFILE=65536
$ sudo systemctl restart nginx
$ sudo systemctl revert nginx.service # delete all drop-ins and overrides in /etc for this unit
Removed "/etc/systemd/system/nginx.service.d/override.conf".
Removed "/etc/systemd/system/nginx.service.d".In automation (Ansible, cloud-init, an image build) you write the file yourself and run systemctl daemon-reload.
Single values and lists
Most keys hold one value, and a later file simply replaces it. List-valued keys (ExecStart=, ExecStartPre=, Environment=, After=, Wants=…) append instead. To replace a list you first assign the empty value, which clears it. A model of the merge rules:
LISTS = {"ExecStart", "ExecStartPre", "Environment"}
def merge(*files):
unit = {}
for f in files:
for line in f.strip().splitlines():
key, _, value = line.partition("=")
if key in LISTS:
unit[key] = [] if value == "" else unit.get(key, []) + [value]
else:
unit[key] = value
return unit
vendor = """
ExecStart=/usr/bin/app --port 8000
Environment=MODE=dev
Restart=no
"""
wrong = """
ExecStart=/usr/bin/app --port 9000
Restart=on-failure
"""
right = """
ExecStart=
ExecStart=/usr/bin/app --port 9000
Environment=MODE=prod
Restart=on-failure
"""
for name, dropin in (("without reset", wrong), ("with reset", right)):
print(name)
for k, v in merge(vendor, dropin).items():
print(f" {k} = {v}")without reset
ExecStart = ['/usr/bin/app --port 8000', '/usr/bin/app --port 9000']
Environment = ['MODE=dev']
Restart = on-failure
with reset
ExecStart = ['/usr/bin/app --port 9000']
Environment = ['MODE=dev', 'MODE=prod']
Restart = on-failure
For Environment= both assignments are kept, and the later one (MODE=prod) wins when the variable is set, which is why adding an environment variable in a drop-in needs no reset. The real systemd agrees on ExecStart=. Here the drop-in sits next to the unit, as it would in /etc/systemd/system/, and systemd-analyze verify reads both:
mkdir -p app.service.d
cat > app.service <<'EOF'
[Unit]
Description=Demo app
[Service]
ExecStart=/usr/bin/python3 -m http.server 8000
EOF
cat > app.service.d/override.conf <<'EOF'
[Service]
ExecStart=/usr/bin/python3 -m http.server 9000
EOF
echo "--- without the reset"
systemd-analyze verify ./app.service
cat > app.service.d/override.conf <<'EOF'
[Service]
ExecStart=
ExecStart=/usr/bin/python3 -m http.server 9000
Restart=on-failure
EOF
echo "--- with the reset"
systemd-analyze verify ./app.service && echo "OK"--- without the reset
app.service: Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.
Unit app.service has a bad unit file setting.
--- with the reset
OK
Overriding the [Unit] section and dependencies
Dependencies append too, so a drop-in can add an ordering or a requirement without repeating the vendor's list. Unlike ExecStart=, though, dependency keys (After=, Wants=, Requires=…) cannot be reset with an empty assignment: a drop-in can only add dependencies. Removing one is one of the few cases where a full copy in /etc/systemd/system/ is the only way.
# /etc/systemd/system/nginx.service.d/10-after-app.conf
[Unit]
After=inventory.service
Wants=inventory.serviceDrop-ins for many units at once
Since systemd 239 a drop-in directory can name a prefix of units: /etc/systemd/system/service.d/ applies to every service, and app-.service.d/ to every unit whose name starts with app-. Top-level drop-ins are powerful and easy to forget; use them for policies such as "every service gets ProtectSystem= unless it opts out", and document them.
Finding what is in effect
On a live server (sample session):
$ systemd-delta --type=extended,overridden
[EXTENDED] /usr/lib/systemd/system/nginx.service → /etc/systemd/system/nginx.service.d/50-restart.conf
[OVERRIDDEN] /etc/systemd/system/postgresql@.service → /usr/lib/systemd/system/postgresql@.service
2 overridden configuration files found.
$ systemctl show nginx -p Restart -p LimitNOFILE -p DropInPaths
Restart=always
LimitNOFILE=65536
DropInPaths=/etc/systemd/system/nginx.service.d/50-restart.confsystemd-delta is the audit command: it lists every unit you extended with drop-ins or fully overrode. An [OVERRIDDEN] entry is a full copy that will drift from the package; consider turning it into a drop-in.
Test yourself
Why is a drop-in better than copying the vendor unit to /etc/systemd/system/?
- It is faster at boot
- It changes only the named settings, so fixes in the package's unit file still apply
- Drop-ins don't need daemon-reload
- Copies are not allowed
A drop-in contains only ExecStart=/usr/bin/app --port 9000 for a Type=simple service. What happens?
- The port changes to 9000
- The service now has two ExecStart lines and is refused
- The drop-in is ignored
- systemd picks the last line
Two drop-ins, 10-a.conf and 50-b.conf, both set Restart=. Which value wins?
- 10-a.conf's
- 50-b.conf's, because drop-ins are applied in file-name order
- Neither, it's an error
- The vendor file's