Writing & overriding unit files
Write a service unit and override vendor settings with drop-in files.
By Invicly Tech, , in Linux Administration (RHCSA-style). About 9 minutes to read.
In short
Units are plain INI-style files. Vendor units ship in /usr/lib/systemd/system and should never be edited; instead you override them with drop-in files in /etc/systemd/system, which systemctl edit creates for you. After changing any unit file, run systemctl daemon-reload.
Explained simply
A unit file is a short text file that tells systemd how to run something. It uses sections in square brackets and key=value lines, like an INI file. A few lines are enough to turn any program into a proper service that starts at boot and restarts when it crashes.
Packages ship their unit files in /usr/lib/systemd/system. You should not edit those, because the next package update would overwrite your change. Your own units and your changes go in /etc/systemd/system, which always wins.
To change just one or two settings of a packaged unit, you add a drop-in file: a tiny file that only contains the lines you want to change. systemctl edit creates it for you in the right place, and the original file stays untouched.
systemd reads unit files into memory. After you change any of them by hand, run systemctl daemon-reload so it notices. systemctl edit does that step for you.
Press Play on a diagram to walk through it one step at a time.
- 1/etc/systemd/system/NAME.d/*.conf
Admin drop-ins: override individual settings.
- 2/etc/systemd/system
Admin units: replace a unit entirely.
- 3/run/systemd/system
Runtime units, gone after reboot.
- 4/usr/lib/systemd/system
Vendor units from packages; do not edit.
- 1[Unit]
Description, After=, Wants=, start limits
- 2[Service]
Type, ExecStart, User, Environment, Restart
- 3Hardening lines
NoNewPrivileges, ProtectSystem, PrivateTmp
- 4[Install]
WantedBy= used by systemctl enable
In depth
Choosing the right Type=
Type= tells systemd how to know the service has started. simple, the default, treats the service as started as soon as the process is created. exec waits until the program has actually been executed, so a wrong path makes the start fail properly. Both expect the program to stay in the foreground.
forking is for old-style daemons that start, fork a child into the background and exit; set PIDFile= so systemd can find the main process. oneshot is for commands that do a job and exit, like a clean-up script; add RemainAfterExit=yes if the unit should count as active afterwards. notify waits until the program reports that it is ready, which programs built for systemd support.
The classic mistake is running a program that daemonizes itself with Type=simple: systemd sees the parent exit and considers the service finished.
- simple or exec
- Program stays in the foreground
- Most modern apps
- forking
- Old daemon that backgrounds itself
- Set PIDFile=
- oneshot
- Runs a task and exits
- Often with timers
- notify
- Program signals when ready
- Built-in systemd support
The settings that make a solid service
ExecStart= needs an absolute path and is not run through a shell, so pipes, redirections, globs and && do not work; if you need them, use ExecStart=/bin/sh -c '...' or, better, a small script. ExecStartPre= runs checks first, and ExecReload=/bin/kill -HUP $MAINPID gives you systemctl reload.
User= and Group= drop root rights. WorkingDirectory= sets the current directory. Environment=KEY=value sets variables, and EnvironmentFile=/etc/app/env loads them from a file; a leading dash, as in EnvironmentFile=-/etc/app/env, means do not fail if it is missing.
Restart=on-failure restarts after a crash or non-zero exit, and RestartSec=5 waits five seconds first. StartLimitIntervalSec= and StartLimitBurst= in [Unit] stop endless restart loops; by default, more than 5 starts in 10 seconds marks the unit failed.
- Restart=always also restarts after a clean exit
- TimeoutStopSec= sets how long to wait before SIGKILL (default 90 s)
- systemd-analyze verify app.service checks a unit for mistakes
How drop-ins are merged
systemd builds each unit from the main file plus every .conf file in a matching NAME.service.d directory, read in alphabetical order by file name. Drop-ins in /etc override those in /run and /usr/lib. systemctl edit httpd writes /etc/systemd/system/httpd.service.d/override.conf and reloads systemd when you save.
Most settings simply take the last value. Settings that can appear many times, like ExecStart= or Environment=, add to the list instead. To replace ExecStart, first clear it with an empty ExecStart= line, then set the new one; otherwise a simple service complains that it has more than one ExecStart. Dependencies like After= and Wants= can be added in a drop-in but not removed.
systemctl edit --full copies the whole unit to /etc, which also hides future vendor fixes. systemd-delta lists every overridden unit, and systemctl revert httpd removes your overrides.
- 1Vendor unit
/usr/lib/systemd/system/httpd.service
- 2Drop-ins
httpd.service.d/*.conf, alphabetical
- 3daemon-reload
systemd merges and caches the result
- 4restart the service
New settings apply to the process
- 5Check
systemctl cat and systemctl show
Hardening and user services
Unit files can also sandbox a service with a few lines. NoNewPrivileges=yes stops the process and its children from gaining rights through SUID programs. PrivateTmp=yes gives it its own /tmp. ProtectSystem=strict makes the whole file system read-only for it, and ReadWritePaths=/var/lib/app opens only the directories it needs. ProtectHome=yes hides home directories. systemd-analyze security app.service rates how exposed a unit is and suggests more options.
Normal users can run their own services without root. Their units live in ~/.config/systemd/user, and they use systemctl --user start, enable and status. By default those services stop when the user's last session ends; sudo loginctl enable-linger alice keeps her user services running without a login and starts them at boot, which is how rootless Podman containers are often run.
Learn it in 4 short steps
About 40m each. Track them with a timer in your plan.
- 1Read a vendor unit with systemctl cat~40m
- 2Write a custom service unit~40m
- 3Override settings with systemctl edit~40m
- 4Reload with daemon-reload~40m
Key points
- systemctl cat NAME shows the unit plus every drop-in that applies.
- systemctl edit NAME creates a drop-in; --full copies the whole unit.
- To replace a list setting such as ExecStart, first clear it with an empty ExecStart=.
- Run systemctl daemon-reload after editing files by hand.
Key terms
Tap a card to flip it. 0/5 known
Examples
# sudo systemctl edit httpd creates:
# /etc/systemd/system/httpd.service.d/override.conf
[Service]
Restart=on-failure
RestartSec=5s
LimitNOFILE=65536
# Then: sudo systemctl daemon-reload && sudo systemctl restart httpdOverride vendor settings without touching the packaged unit.
Turn a Python web app into a service
A small API lives in /opt/api and runs with /opt/api/venv/bin/python app.py. Step 1: create a system user for it. Step 2: write the unit with the user, working directory, an environment file, restart policy and some hardening. Step 3: reload systemd, verify the file, then enable and start the service. Step 4: confirm it runs as the api user and follow its logs.
# /etc/systemd/system/api.service
[Unit]
Description=Example API
After=network-online.target
Wants=network-online.target
[Service]
User=api
WorkingDirectory=/opt/api
EnvironmentFile=-/etc/api/env
ExecStart=/opt/api/venv/bin/python app.py
Restart=on-failure
RestartSec=5
NoNewPrivileges=yes
PrivateTmp=yes
[Install]
WantedBy=multi-user.targetChange how a packaged service starts
You need to add an option to the vendor's ExecStart for a service called myd, without editing the packaged file. Step 1: see the current unit with systemctl cat. Step 2: run systemctl edit and add a [Service] section that first clears ExecStart and then sets the new line. Step 3: check the merged result. Step 4: restart, and check that the process has the new option.
systemctl cat myd | grep ExecStart
# ExecStart=/usr/sbin/myd --foreground
sudo systemctl edit myd
# [Service]
# ExecStart=
# ExecStart=/usr/sbin/myd --foreground --verbose
systemctl cat myd # shows the vendor file and override.conf
sudo systemctl restart myd
ps -o args= -p "$(systemctl show -p MainPID --value myd)"Quick reference
- Vendor units: /usr/lib/systemd/system; yours: /etc/systemd/system
- systemctl edit NAME makes a drop-in and reloads; --full copies the whole unit
- Clear a list setting with an empty line first: ExecStart= then ExecStart=/new/cmd
- ExecStart is not run by a shell; use /bin/sh -c or a script for pipes and redirects
- Restart=on-failure plus RestartSec=5 restarts a crashed service after 5 s
- systemd-delta shows overridden units; systemctl revert NAME removes overrides
- systemd-analyze verify FILE finds errors; systemd-analyze security NAME rates hardening
- loginctl enable-linger USER keeps that user's services running without a login
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Show the full effective unit for sshd, including any drop-ins, and list every unit on the system that has overrides.
- Exercise 2medium
Without editing the vendor file, make httpd restart automatically 3 seconds after any failure, and prove the setting is active.
- Exercise 3hard
Create a oneshot service disk-report that writes df -h output to /var/log/disk-report.txt at boot, runs after local file systems are mounted, and counts as active after it finishes.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Where should I put my own service files?
- In /etc/systemd/system. Files there override packaged units in /usr/lib/systemd/system and are never touched by package updates.
- Do I need daemon-reload after systemctl edit?
- No, systemctl edit reloads for you when you save. You only need it after editing or creating files by hand.
- Can a unit file use shell features like pipes?
- Not directly. ExecStart runs the program without a shell, so wrap the command in /bin/sh -c '...' or put it in a script.
- Should I use cron or a systemd unit for my app?
- Long-running apps belong in a service unit, which restarts and logs them. For scheduled jobs, cron still works, but a systemd timer with a oneshot service gives you logs and dependency handling.