Unix - Systemd Unit File Structure
A systemd unit file defines how a service, device, mount point, socket, or other component should be started, stopped, and managed.
These files are typically located in:
A unit file is a simple text file divided into standardized sections.
1. [Unit] Section
This section describes the unit at a high level and defines its relationships with other units.
Common directives:
Meaning:
-
Description: human-readable name.
-
Documentation: where to find its docs.
-
After/Before: ordering — when to start relative to others.
-
Requires: hard dependency; failure stops this unit.
-
Wants: soft dependency; failure doesn't stop the unit.
2. [Service] Section
Used only for .service units.
Defines how the service runs, what command starts it, and how systemd supervises it.
Common directives:
Key parameters:
-
Type=
-
simple: default, runs and stays running. -
forking: service daemonizes (like old SysV daemons). -
oneshot: runs a single task and exits. -
notify: service notifies systemd when ready.
-
-
ExecStart: required; the command to start the service.
-
ExecStop: optional; command to stop it gracefully.
-
Restart: auto-restart options (
always,on-failure, etc.). -
User/Group: run service as non-root.
-
Environment: environment variables.
3. [Install] Section
Defines how the unit integrates into the system when using systemctl enable.
Meaning:
-
WantedBy: which target this service should start with.
-
RequiredBy: hard dependency from another target.
-
Alias: alternative name.
When you run:
systemd creates symlinks based on the WantedBy or RequiredBy values.
Complete Example Unit File
Summary of Unit File Sections
| Section | Purpose |
|---|---|
| [Unit] | Metadata and dependencies |
| [Service] | How the service starts, runs, and restarts (services only) |
| [Install] | Integration with boot targets (systemctl enable) |