Service units are where systemd behavior becomes explicit. If you can read [Unit], [Service], and [Install] sections confidently, you can diagnose and tune most service behavior.
Table of Contents
This post explains how service units are structured and how to change behavior safely.
Unit File Anatomy
Operational Patterns
Series Navigation
Unit file anatomy
A typical service unit has three sections.
[Unit]
Description=Example API service
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=app
Group=app
WorkingDirectory=/opt/example-api
Environment=PORT=8080
ExecStart=/opt/example-api/bin/server
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.targetSection role summary
Think about sections this way:
[Unit]defines dependency and ordering relationships[Service]defines how the process runs and recovers[Install]defines how the unit is linked into startup targets
Directive deep dive
The most important directives to master early:
Descriptiongives human-readable context in status outputAfterandBeforecontrol ordering but not requirementWantsandRequiresdeclare relationship strengthTypedeclares startup semantics such assimple,forking,oneshot, andnotifyExecStartis the primary command systemd executesExecReloaddefines reload behavior when the daemon supports itUserandGroupavoid running daemons as root unless requiredWorkingDirectorysets execution context for relative pathsEnvironmentandEnvironmentFileinject runtime settingsRestartandRestartSecdefine failure recovery policyWantedBycontrols install-time target linkage
Read existing units first
Before writing a new unit, inspect how packaged services are implemented on your host.
systemctl cat sshd.service
systemctl cat nginx.service
systemctl show nginx.service -p FragmentPath -p DropInPathsThis gives you examples of directives that match your distribution conventions.
Use drop-in overrides
Do not edit packaged files under /usr/lib/systemd/system directly because package updates may overwrite changes. Use a drop-in override under /etc/systemd/system.
sudo systemctl edit nginx.serviceThat command creates or edits:
/etc/systemd/system/nginx.service.d/override.confExample override:
[Service]
Restart=always
RestartSec=3s
Environment=NGINX_WORKERS=4Reload and apply changes
After creating or editing units, refresh systemd metadata, then apply runtime changes.
sudo systemctl daemon-reload
sudo systemctl restart nginx.service
systemctl status nginx.servicedaemon-reload is required for definition changes, while restart or reload applies behavior to the running process.
Reload vs restart
Pick the right action based on service capabilities and change scope:
- Use
reloadwhen config changes are reloadable and downtime must be minimized - Use
restartwhen binaries, startup parameters, or non-reloadable settings changed - Confirm the right choice in service docs because some units define
ExecReloadwhile others do not
Series navigation
With service units covered, the next topic is how system states and boot paths are modeled through targets and dependency semantics.
Next in this series
Next, we map SysV runlevels to systemd targets and break down dependency rules that shape boot order.
systemd Targets and Boot Dependencies: From Runlevels to multi-user.target