2.5 Systemd Boot Targets & Unit Management (101.3)
Key Takeaways
- systemd is the modern PID 1 service manager in Linux, offering parallel unit startup, socket activation, and explicit dependency modeling.
- Systemd manages 11 distinct unit types: .service, .target, .socket, .mount, .automount, .swap, .device, .timer, .path, .slice, and .scope.
- Unit configuration files follow strict precedence: /etc/systemd/system/ (admin overrides) > /run/systemd/system/ (runtime) > /lib/systemd/system/ (vendor defaults).
- System operating states are defined by targets: rescue.target (single-user), multi-user.target (CLI server), and graphical.target (desktop GUI).
- systemctl is used for service control (start/stop/restart), enablement (enable/disable), inspection (status/is-active), masking, and changing targets (isolate, set-default).
2.5 Systemd Boot Targets & Unit Management
Quick Summary:
systemdis the standard initialization system and service manager for modern Linux distributions. Acting as PID 1, it replaces traditional SysVinit with parallel service activation, socket- and D-Bus-based activation, and declarative unit configuration. Operating states are organized into targets (e.g.,multi-user.target,graphical.target), and all runtime service administration is performed through thesystemctlcommand.
1. Systemd Architecture & Core Features
Unlike traditional sequential init scripts, systemd treats services and resources as structured objects called Units.
Core Systemd Capabilities
- Parallel Execution: Starts non-dependent services concurrently, drastically reducing boot times.
- Socket-Based Activation: Creates listening sockets (UNIX domain or TCP) before launching daemons. When a packet arrives, systemd launches the service instantly, eliminating startup race conditions.
- cgroups Process Tracking: Groups service child processes inside dedicated Control Groups (cgroups). When a service stops, systemd cleanly terminates all child processes, preventing orphaned daemons.
- Explicit Dependency Modeling: Uses unit directives (
Requires=,Wants=,BindsTo=,Conflicts=,Before=,After=) to establish rigorous startup sequences.
2. The 11 Systemd Unit Types
Systemd organizes all system resources into 11 distinct unit types, distinguished by their filename extensions.
| Unit Extension | Resource Managed | Real-World Example & Purpose |
|---|---|---|
.service | Daemons and background processes | sshd.service, nginx.service (manages starting, stopping, restarting daemons) |
.target | Logical grouping of units representing a system state | multi-user.target, graphical.target (replaces SysV runlevels) |
.socket | IPC or network sockets for on-demand daemon activation | sshd.socket, cups.socket (activates .service when traffic arrives) |
.mount | Filesystem mount points | var-log.mount (mounts /var/log; filename must match mount path dashes) |
.automount | On-demand filesystem mounting upon access | home-backup.automount (mounts filesystem only when directory is accessed) |
.swap | Swap partitions or swap files | dev-nvme0n1p3.swap (activates swap space in kernel) |
.device | Hardware devices exposed by udev/kernel | sys-subsystem-net-devices-eth0.device (enables dependency on hardware) |
.timer | Monotonic or calendar-based job scheduler | logrotate.timer, fstrim.timer (replaces traditional cron jobs) |
.path | Inotify-based file/directory monitoring | spool-watch.path (activates a service when a file is created or modified) |
.slice | Hierarchical cgroups resource partition | user.slice, system.slice (manages CPU, memory, and I/O resource limits) |
.scope | Externally created transient processes | session-1.scope (tracks user login sessions) |
3. Unit File Locations & Configuration Precedence
Systemd loads unit files from three main directory locations. If unit files with identical names exist across multiple directories, systemd strictly follows a top-down priority hierarchy.
[1. /etc/systemd/system/] ──► Priority 1 (Highest): Local Administrator Configuration
│ (Custom units, modified copies, enabled symlinks)
▼
[2. /run/systemd/system/] ──► Priority 2 (Intermediate): Volatile Runtime Units
│ (Dynamically generated at boot in RAM)
▼
[3. /lib/systemd/system/] ──► Priority 3 (Lowest): Vendor Package Defaults
/usr/lib/systemd/system/ (Installed by rpm/dpkg packages; overwritten on updates)
Drop-In Configuration Overrides (.d/ Directories)
Instead of copying an entire 100-line vendor unit file to /etc/systemd/system/sshd.service, administrators can create a drop-in override directory named <unit>.d/:
# /etc/systemd/system/sshd.service.d/override.conf
[Service]
Restart=always
RestartSec=5s
After creating or modifying unit files, you must reload the systemd manager configuration:
sudo systemctl daemon-reload
Exam Trap — Unit Masking: Masking a unit renders it completely unstartable (manually or as a dependency). Systemd accomplishes this by creating a symbolic link pointing to
/dev/null:sudo systemctl mask apache2 # Creates: /etc/systemd/system/apache2.service -> /dev/null
4. Systemd Targets & Operating States
A target unit (.target) does not run a daemon directly. Instead, it groups other unit files together using dependency symlinks located in <target_name>.wants/ and <target_name>.requires/ directories.
Standard Systemd Targets
| Systemd Target | SysV Equivalent | Description & Operating Environment |
|---|---|---|
poweroff.target | Runlevel 0 | Shuts down and powers off the system hardware. |
rescue.target | Runlevel 1 / S | Single-user maintenance mode with a root shell and all local filesystems mounted read-write. No network services. |
emergency.target | N/A | Minimal emergency shell. Root filesystem is mounted read-only, no extra services or background daemons are started. |
multi-user.target | Runlevel 3 | Standard multi-user command-line environment with full networking and multi-user login. Default for Linux servers. |
graphical.target | Runlevel 5 | Full multi-user environment with networking and a graphical display manager (X11 / Wayland / GDM / SDDM). |
reboot.target | Runlevel 6 | Shuts down and reboots the system. |
default.target | initdefault | A symbolic link pointing to the default boot target (e.g., /etc/systemd/system/default.target -> /lib/systemd/system/multi-user.target). |
5. System Administration with systemctl
systemctl is the central CLI utility for inspecting, controlling, enabling, and diagnosing systemd units.
Service Lifecycle Management
sudo systemctl start <unit>: Starts (activates) the unit immediately.sudo systemctl stop <unit>: Stops (deactivates) the unit immediately.sudo systemctl restart <unit>: Stops and restarts the unit.sudo systemctl reload <unit>: Instructs the service to reload its configuration files without dropping active connections (e.g.,nginx -s reload).sudo systemctl reload-or-restart <unit>: Reloads configuration if supported; otherwise, restarts the service.
Boot Enablement & State Inspection
sudo systemctl enable <unit>: Enables the unit to start at boot by creating a symlink in the target's.wants/directory under/etc/systemd/system/.sudo systemctl disable <unit>: Disables automatic startup by deleting the.wants/symlink.sudo systemctl enable --now <unit>: Enables and immediately starts the service in a single command.systemctl status <unit>: Displays unit state (active, inactive, failed), PID, memory usage, cgroup processes, and recent log snippets.systemctl is-active <unit>: Returns exit code 0 and printsactiveif running.systemctl is-enabled <unit>: Returns exit code 0 and printsenabledif configured for auto-start.systemctl is-failed <unit>: Checks if the unit crashed or entered an error state.systemctl list-units --type=service: Lists all currently loaded service units in memory.systemctl list-unit-files: Lists all unit files installed on disk and their enablement status.
Target Operations & System Control
systemctl get-default: Queries and prints the current default boot target.sudo systemctl set-default graphical.target: Updates the/etc/systemd/system/default.targetsymlink.sudo systemctl isolate multi-user.target: Immediately switches the system state tomulti-user.target, stopping all units not part of the new target.sudo systemctl reboot: Reboots the machine.sudo systemctl poweroff: Powers off the machine.
Which systemctl command changes the persistent default boot state of a Linux server from a graphical desktop interface to a multi-user command-line interface?
An administrator wants to completely prevent the 'cups.service' printing daemon from being started under any circumstances, including manual execution by users or automatic invocation as a dependency of another service. Which command should be used?
In systemd unit configuration hierarchy, which directory takes the highest priority when evaluating unit files with identical names?