Stage 3 · Build
Boot & Init Systems
systemd Units & Targets
Services, sockets, mounts, targets, ordering, and dependency inspection with systemctl.
Unit Types
systemd manages everything as units. Each unit type represents a different kind of resource: services, sockets, mounts, timers, and more. Understanding unit types is essential for systemd administration.
| Suffix | Type | Purpose |
|---|---|---|
| .service | Service | System services (daemons) |
| .socket | Socket | Socket activation (on-demand services) |
| .mount | Mount | Filesystem mount points |
| .target | Target | Grouping of units (system states) |
| .timer | Timer | Scheduled activation (replaces cron) |
| .path | Path | Filesystem path monitoring |
| .scope | Scope | External process groups |
| .slice | Slice | cgroup hierarchy for resource control |
| .swap | Swap | Swap space |
Target Units
# List all targets
systemctl list-units --type=target
# Key targets
# multi-user.target — Multi-user mode (no GUI)
# graphical.target — Graphical mode (with GUI)
# rescue.target — Single-user mode
# emergency.target — Emergency mode (minimal)
# default.target — Default boot target (symlink to graphical or multi-user)
# Check default target
systemctl get-default
# multi-user.target
# Change default target
sudo systemctl set-default multi-user.target
# Switch targets at runtime
sudo systemctl isolate rescue.target
sudo systemctl isolate multi-user.target
# Check target dependencies
systemctl list-dependencies graphical.targetTargets represent system states. multi-user.target is equivalent to runlevel 3, graphical.target to runlevel 5.
Unit Dependencies
# Types of dependencies
# Requires — Strong dependency (fail if dependency fails)
# Wants — Weak dependency (try to start, but don't fail)
# After — Ordering (start after this unit)
# Before — Ordering (start before this unit)
# BindsTo — Stronger than Requires (stop if dependency stops)
# PartOf — Restart/stop when dependency restarts/stops
# Conflicts — Cannot run simultaneously
# WantedBy — reverse dependency (used in [Install] section)
# View dependencies of a service
systemctl list-dependencies sshd.service
# sshd.service
# ├─sshd-keygen.service
# ├─network.target
# └─sysinit.target
systemctl list-dependencies --after sshd.service
systemctl list-dependencies --before sshd.serviceDependencies control both ordering (After/Before) and requirement (Requires/Wants). A unit needs both to start successfully.
systemctl Operations
# Service management
sudo systemctl start nginx # Start a service
sudo systemctl stop nginx # Stop a service
sudo systemctl restart nginx # Restart a service
sudo systemctl reload nginx # Reload configuration (if supported)
sudo systemctl enable nginx # Enable at boot
sudo systemctl disable nginx # Disable at boot
sudo systemctl status nginx # Check status
sudo systemctl is-active nginx # Check if active
sudo systemctl is-enabled nginx # Check if enabled
# Mask/unmask (prevent manual start)
sudo mask nginx # Prevent starting
sudo unmask nginx # Allow starting
# List all services
systemctl list-units --type=service
systemctl list-unit-files --type=serviceenable/disable controls boot-time starting. start/stop controls runtime. mask prevents even manual starting.
Unit File Structure
# /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target postgresql.service
Requires=postgresql.service
Wants=redis.service
[Service]
Type=simple
User=appuser
Group=appgroup
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/myapp --config /opt/myapp/config.yaml
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=5
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
# Key fields:
# Type=simple — Process stays in foreground (no daemonize)
# Type=forking — Process forks and exits parent
# Type=oneshot — Runs once, then exits
# Restart=on-failure — Restart only on non-zero exitThe [Install] section determines how the unit is enabled. WantedBy=multi-user.target means it starts when multi-user mode is reached.
Dependency Inspection
# Show full dependency tree
systemctl list-dependencies --all nginx.service
# Show only required dependencies
systemctl list-dependencies --require nginx.service
# Analyze boot time
systemd-analyze
# Startup finished in 5.234s (kernel) + 12.345s (initrd) + 45.678s (userspace)
# Show time per unit
systemd-analyze blame | head -10
# 15.234s network-manager.service
# 12.345s postgresql.service
# 10.123s docker.service
# Critical chain (longest dependency path)
systemd-analyze critical-chain
# The time after the product of the longest chain.
# Verify unit file syntax
systemd-analyze verify /etc/systemd/system/myapp.servicesystemd-analyze helps identify slow services and dependency bottlenecks. critical-chain shows the longest startup path.
systemd-analyze blame shows the slowest services. Focus optimization on the top offenders. Consider socket activation for rarely-used services.
Masked units cannot be started manually or by dependencies. Only mask units you are certain should never run. Use disable for most cases.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.