3.5.2. Managing Unit States and System Configuration
💡 First Principle: systemd unit states form a clear progression: installed → enabled (boot-linked) → active (running). These are independent: a unit can be enabled but inactive (configured to start at boot but not running now), or active but disabled (running now but won't survive a reboot). Always check both dimensions.
Unit State Management:
systemctl start nginx # Activate unit now
systemctl stop nginx # Deactivate unit now
systemctl restart nginx # Stop then start (drops active connections)
systemctl reload nginx # Re-read config without restart (SIGHUP)
systemctl enable nginx # Create symlink for boot start
systemctl disable nginx # Remove boot symlink
systemctl enable --now nginx # Enable AND start (most common pattern)
systemctl disable --now nginx # Disable AND stop
systemctl mask nginx # Prevent start entirely (symlink to /dev/null)
systemctl unmask nginx # Remove mask
systemctl status nginx # State, recent journal entries, PID
systemctl is-active nginx # Returns "active" or "inactive" (scriptable)
systemctl is-enabled nginx # Returns "enabled" or "disabled" (scriptable)
systemctl daemon-reload # Re-read all unit files (after editing a unit file)
systemctl edit nginx # Create/edit drop-in override
systemctl cat nginx # Show effective unit file with overrides
Masking makes a unit impossible to start — even as a dependency. Use this for services that should never run (e.g., systemctl mask postfix on systems that use another MTA).
systemd Utilities:
# Boot analysis
systemd-analyze # Total boot time (firmware + loader + kernel + userspace)
systemd-analyze blame # Time per unit, sorted slowest-first
systemd-analyze critical-chain # The critical path through the dependency graph
# Hostname management
hostnamectl # Show hostname and OS info
hostnamectl set-hostname webserver01 # Change hostname persistently
# Time and NTP
timedatectl # Show date, time zone, NTP status
timedatectl set-timezone America/New_York
timedatectl set-ntp true # Enable NTP sync
# DNS resolution (systemd-resolved)
resolvectl status # DNS servers, search domains, DNSSEC status
resolvectl query google.com # Resolve using systemd-resolved
resolvectl flush-caches # Clear DNS cache
# Kernel parameters
sysctl -a # List all kernel parameters
sysctl net.ipv4.ip_forward # Read specific parameter
sysctl -w net.ipv4.ip_forward=1 # Set parameter (runtime only)
# Persistent: add to /etc/sysctl.conf or /etc/sysctl.d/99-custom.conf
# Then apply: sysctl -p /etc/sysctl.d/99-custom.conf
⚠️ Exam Trap: After editing a unit file in /etc/systemd/system/, you must run systemctl daemon-reload before the changes take effect — systemd caches unit files in memory. Forgetting this is a very common mistake. The symptom: your changes appear to have no effect even though the file looks correct.
Reflection Question: You create a new unit file at /etc/systemd/system/myapp.service. You run systemctl start myapp but get "Unit myapp.service not found." What single command resolves this, and why is it needed?