Table of Contents
- Understanding Systemd: A Brief Overview
- What Are Systemd Targets?
- Key Concepts: Units, Dependencies, and Targets
- Common Systemd Targets Explained
- How Systemd Targets Work: The Boot Process
- Practical Applications of Systemd Targets
- Comparing Systemd Targets with SysVinit Runlevels
- Advanced Tips: Managing Targets with
systemctl - Conclusion
- References
1. Understanding Systemd: A Brief Overview
Before diving into targets, let’s recap what systemd is. systemd is a system and service manager responsible for initializing the system during boot, managing running services, handling power management, and more. Unlike SysVinit, which relied on sequential shell scripts (/etc/init.d/), systemd uses a dependency-based architecture to start units in parallel, reducing boot times and improving flexibility.
At the heart of systemd are units—configuration files that define system resources. Units come in several types, including:
service: Manages daemons (e.g.,nginx.service,sshd.service).mount: Controls filesystem mounts (e.g.,mnt-data.mount).socket: Manages inter-process communication (IPC) sockets.target: Groups other units to define a system state (the focus of this blog).
2. What Are Systemd Targets?
A systemd target is a special type of unit (*.target) that acts as a “grouping mechanism” for other units. Unlike service or mount units, targets do not execute actions directly. Instead, they declare dependencies on other units (services, mounts, sockets, etc.) to coordinate the system’s state.
Think of targets as “meta-units” that answer the question: “What should the system look like right now?” For example, graphical.target ensures the system boots into a graphical desktop by pulling in the X server, window manager, and related services.
Targets are analogous to SysVinit’s “runlevels” but with key improvements:
- Flexibility: Targets can be combined or extended (e.g., a “kiosk.target” for public displays).
- Parallelism:
systemdstarts units in parallel (where dependencies allow), speeding up boot times. - Explicit Dependencies: Targets clearly define which units they require, avoiding ambiguity.
3. Key Concepts: Units, Dependencies, and Targets
To grasp targets, you must first understand how systemd manages dependencies between units. Targets rely on these dependencies to activate the correct set of units. Here are the critical dependency directives:
3.1 Unit Dependencies
Wants=: A “soft” dependency. The target will attempt to start the listed units, but it proceeds even if some fail. Example:graphical.targetmightWants=gnome-session.service(if Gnome fails, the target still loads).Requires=: A “hard” dependency. The target will fail if any listed unit fails to start. Example:multi-user.targetRequires=network.target(networking is critical for multi-user mode).After=/Before=: Controls the order of activation.After=network.targetensures the unit starts afternetwork.target;Before=does the opposite.
3.2 Targets as “State Managers”
Targets use these dependencies to assemble a coherent system state. For example, multi-user.target (a non-graphical multi-user state) typically includes:
local-fs.target: Ensures local filesystems are mounted.network.target: Ensures networking is active.sshd.service: Enables SSH access.cron.service: Starts the cron daemon.
4. Common Systemd Targets Explained
systemd includes dozens of targets, but only a handful are critical for daily use. Below are the most important ones, organized by use case:
4.1 Emergency and Rescue Targets
| Target | Purpose |
|---|---|
emergency.target | Minimal state: Single-user mode with no mounted filesystems (except / as read-only), no networking, and only a root shell. Use for catastrophic failures (e.g., unbootable /etc/fstab). |
rescue.target | Single-user mode with basic services: Local filesystems mounted, essential drivers loaded, but no multi-user services (e.g., SSH). Use for fixing broken configurations (e.g., sudo permissions). |
4.2 Operational Targets
| Target | Purpose |
|---|---|
multi-user.target | Default for servers: Multi-user mode with networking, no graphical interface. Runs most services (e.g., nginx, mysql). |
graphical.target | Default for desktops/laptops: Multi-user mode with a graphical interface. Depends on multi-user.target and adds display managers (e.g., gdm.service, lightdm.service). |
4.3 Shutdown and Power Targets
| Target | Purpose |
|---|---|
poweroff.target | Triggers system shutdown. Invoked by systemctl poweroff. |
reboot.target | Triggers system reboot. Invoked by systemctl reboot. |
halt.target | Stops the system without powering off (rarely used today). |
4.4 Special Targets
| Target | Purpose |
|---|---|
default.target | A symlink to the system’s default target (e.g., graphical.target or multi-user.target). Determines the boot state. |
shutdown.target | Coordinates units during shutdown (e.g., stopping services, unmounting filesystems). |
network.target | Marks the point where networking services (e.g., NetworkManager.service) are active. |
5. How Systemd Targets Work: The Boot Process
Targets are the backbone of the Linux boot sequence. Here’s a simplified breakdown of how systemd uses targets to boot your system:
Step 1: Kernel Initialization
After the BIOS/UEFI and bootloader (e.g., GRUB) load the kernel, the kernel initializes hardware and mounts the initial RAM filesystem (initramfs). It then launches systemd as PID 1 (the “init” process).
Step 2: systemd Starts default.target
systemd’s first task is to activate default.target (a symlink to your default operational target, e.g., graphical.target).
Step 3: Resolving Dependencies
systemd parses default.target’s dependencies and builds a dependency graph. For example, graphical.target might require:
[Unit]
Description=Graphical Interface
Requires=multi-user.target
After=multi-user.target
Wants=display-manager.service
This tells systemd:
- Start
multi-user.targetfirst (viaAfter=). - Ensure
multi-user.targetsucceeds (viaRequires=). - Try to start
display-manager.service(viaWants=).
Step 4: Activating Units in Parallel
systemd activates units in parallel (where dependencies allow). For multi-user.target, this might include:
- Mounting filesystems (
local-fs.target). - Starting networking (
network.target). - Launching services like
sshdandcron.
Step 5: Reaching the Target
Once all dependencies of default.target are satisfied, the system enters the desired state (e.g., graphical desktop or multi-user server).
6. Practical Applications of Systemd Targets
Targets are not just for booting—they empower you to customize and troubleshoot your system. Below are key use cases:
6.1 Setting the Default Target
The default target determines the system’s state after boot. To check the current default:
systemctl get-default
# Output: graphical.target (common on desktops) or multi-user.target (common on servers)
To set a new default (e.g., switch a server to multi-user.target):
sudo systemctl set-default multi-user.target
6.2 Booting into a Target Temporarily
To test a target without changing the default (e.g., boot into rescue mode), modify the boot command in your bootloader (GRUB):
- During boot, press
Eto edit the GRUB menu entry. - Find the line starting with
linux(orlinux16). - Add
systemd.unit=<target>at the end. Example:linux /boot/vmlinuz-5.15.0-78-generic root=... systemd.unit=rescue.target - Press
Ctrl+Xto boot.
6.3 Creating Custom Targets
You can define custom targets for specialized workflows (e.g., a “kiosk” for public displays or a “media-server” target). Here’s a step-by-step example for a “kiosk.target”:
Step 1: Create the Target File
Create /etc/systemd/system/kiosk.target with:
[Unit]
Description=Kiosk Mode (Fullscreen Browser)
Requires=multi-user.target
After=multi-user.target
AllowIsolate=yes # Allow "systemctl isolate kiosk.target"
Step 2: Create a Service for the Kiosk
Create /etc/systemd/system/kiosk-browser.service to launch a fullscreen browser:
[Unit]
Description=Kiosk Browser
Wants=kiosk.target
After=display-manager.service
[Service]
User=kiosk-user # Create a dedicated user for security
ExecStart=/usr/bin/firefox --kiosk https://example.com
Restart=always # Restart if the browser crashes
[Install]
WantedBy=kiosk.target # Start when kiosk.target is active
Step 3: Enable and Test the Target
sudo systemctl daemon-reload # Reload systemd to detect new units
sudo systemctl enable kiosk-browser.service
sudo systemctl set-default kiosk.target # Set as default (optional)
sudo reboot
6.4 Troubleshooting with Targets
Targets are invaluable for fixing broken systems:
emergency.target: Use when the system fails to mount filesystems (e.g., corrupted/etc/fstab). Remount/as read-write withmount -o remount,rw /to edit files.rescue.target: Use for fixing service issues (e.g., a failedsshdconfiguration). Services likejournaldare active, so you can check logs withjournalctl.
7. Comparing Systemd Targets with SysVinit Runlevels
If you’re familiar with SysVinit, targets map loosely to runlevels but with more granularity. Here’s a comparison table:
| SysVinit Runlevel | Purpose | Equivalent systemd Target |
|---|---|---|
| 0 | Poweroff | poweroff.target |
| 1 | Single-user mode | rescue.target |
| 2 | Multi-user (no network) | N/A (use custom target) |
| 3 | Multi-user (text-only) | multi-user.target |
| 4 | Unused (custom) | N/A (use custom target) |
| 5 | Multi-user (graphical) | graphical.target |
| 6 | Reboot | reboot.target |
8. Advanced Tips: Managing Targets with systemctl
The systemctl command is your primary tool for interacting with targets. Here are essential commands:
List All Active Targets
systemctl list-units --type=target --state=active
Isolate a Target (Change State Immediately)
systemctl isolate <target> switches the system to the target right now (stops all units not in the target’s dependency chain). Use with caution!
sudo systemctl isolate rescue.target # Switch to rescue mode immediately
View Target Dependencies
systemctl show -p Wants,Requires multi-user.target
# Output: Shows which units multi-user.target depends on
Mask a Target (Prevent Activation)
Masking a target ensures it can never be activated (useful for disabling unwanted targets):
sudo systemctl mask graphical.target # Prevent booting into GUI
9. Conclusion
Systemd targets are the cornerstone of how Linux systems define and manage their operational state. By grouping units with explicit dependencies, targets provide flexibility, speed, and clarity unmatched by traditional runlevels. Whether you’re setting up a server, troubleshooting a desktop, or building a custom kiosk, mastering targets is essential for effective Linux system administration.
As you experiment with targets, remember: systemd’s dependency-based architecture rewards clarity. Always define dependencies explicitly, test custom targets in non-critical environments, and use journalctl -u <target> to debug issues.