Table of Contents
Overview
Systemd is the default init system and service manager on most modern Linux distributions. In this chapter you’ll learn how systemd fits into the system, what kinds of units it manages, how to interact with services, and how typical tasks like enabling, disabling, and troubleshooting services work in practice.
You’ll use systemctl and journalctl a lot in later chapters, so the goal here is to make you comfortable with the basic concepts and day‑to‑day commands.
Systemd in the System
At a high level:
PID 1issystemdon most Linux distros.- It:
- Starts and stops services.
- Manages system boot targets (what comes up on boot).
- Tracks service dependencies and ordering.
- Collects logs via
journald. - Manages timers, mounts, network-related units, and more.
You do not need to fully understand the boot process yet; focus on how systemd exposes a uniform interface to manage system components.
Units: The Building Blocks of systemd
Systemd manages units. Each unit is a configuration file that describes something systemd controls.
Common unit types you’ll see:
service– A background service (daemon), e.g.sshd.servicesocket– A listening socket that can start a service on demandtimer– A systemd-based scheduled task (similar tocron)mount– A filesystem mount pointautomount– Auto-mount trigger unittarget– A grouping of units (a “state” of the system)path– Watches paths and triggers services on changesdevice– Represents a device in/devswap– Swap space
Unit file names follow the pattern:
name.type(e.g.nginx.service,multi-user.target,home.mount)
You interact with them using systemctl.
Common Unit Locations
You’ll encounter unit files in three main locations:
/usr/lib/systemd/system/or/lib/systemd/system/- Packaged units installed by the distribution.
- Don’t edit these directly.
/etc/systemd/system/- Local overrides and custom units.
- Your custom service/override files go here.
/run/systemd/system/- Runtime units (generated at boot or temporary changes).
Systemd merges these, with priority:
/etc/systemd/system(highest)/run/systemd/system/usr/lib/systemd/systemor/lib/systemd/system
Using `systemctl` for Day-to-Day Management
systemctl is the primary command-line tool for interacting with systemd.
General structure:
systemctl [OPTIONS] COMMAND [NAME...]
You’ll often use it with root privileges (sudo), especially when changing system state.
Listing and Inspecting Units
List all loaded units:
systemctl list-unitsList only services:
systemctl list-units --type=serviceList all unit files (including disabled, static, not loaded at runtime):
systemctl list-unit-files
systemctl list-unit-files --type=serviceCheck status of a specific service:
systemctl status sshd.service
You can usually omit the .service suffix:
systemctl status sshdThe status output typically shows:
- Whether the service is loaded (has a unit file).
- Whether it is enabled/disabled (starts at boot or not).
- Whether it is active (running) or failed.
- Recent log entries via the systemd journal.
Starting, Stopping, Restarting Services
These actions affect the current session only (not whether it starts at boot):
- Start immediately:
sudo systemctl start nginx- Stop:
sudo systemctl stop nginx- Restart:
sudo systemctl restart nginx- Reload configuration without a full restart (only if supported):
sudo systemctl reload nginx- Try reload, and if that fails, restart:
sudo systemctl reload-or-restart nginxCheck results with:
systemctl status nginxEnabling and Disabling at Boot
These control whether a service starts automatically when the system reaches certain boot targets:
- Enable a service (start at boot):
sudo systemctl enable nginx- Enable and start right now:
sudo systemctl enable --now nginx- Disable a service (don’t start automatically):
sudo systemctl disable nginx- Disable and stop right now:
sudo systemctl disable --now nginx
Note “enable” usually creates symlinks in /etc/systemd/system/ pointing to the main unit file.
Checking and Changing the Default Target (Boot State)
A target describes a collection of units that define a system state, such as:
multi-user.target– “text-mode multi-user” (no graphical desktop)graphical.target– multi-user + graphical session manager
View current default target:
systemctl get-defaultSet default (what the system aims for at boot):
sudo systemctl set-default graphical.target
sudo systemctl set-default multi-user.targetSwitch to a different target immediately (similar to changing runlevels):
sudo systemctl isolate multi-user.target
Be cautious with isolate on remote systems; you can lock yourself out if you stop network or SSH services.
Understanding Service States
A service can be:
active– Running as expected.inactive– Not running.failed– Exited with an error or crashed.activatingordeactivating– In transitional states.reloading– Reloading configuration.
Check:
systemctl is-active sshd
systemctl is-enabled sshd
systemctl is-failedThese commands are useful in scripts to make decisions based on service state.
Basic Troubleshooting of Services
When a service misbehaves, you usually:
- Check its status.
- Inspect the logs.
- Try to (re)start it.
- If needed, edit the unit or associated config files.
Checking Status and Recent Logs
Status includes logs, but often you’ll want more:
sudo systemctl status nginxGet only recent logs for a service:
sudo journalctl -u nginxFollow logs in real time:
sudo journalctl -u nginx -fLimit by time:
sudo journalctl -u nginx --since "10 minutes ago"
sudo journalctl -u nginx --since yesterdayLook for errors or misconfigurations reported by the program itself.
Restarting and Analyzing Failures
Try restart:
sudo systemctl restart nginx
sudo systemctl status nginx
If the service is failed, systemd often prints:
- The exit code.
- A hint to check
journalctl -u SERVICE. - Sometimes suggestions for missing dependencies.
If a unit file was modified or newly created, reload systemd’s view of unit files:
sudo systemctl daemon-reloadThis does not restart services; it only reloads configuration into systemd.
Anatomy of a Service Unit File
Service units typically live under:
/usr/lib/systemd/system/name.service(packaged)/etc/systemd/system/name.service(local/custom)
View a unit file as systemd sees it:
systemctl cat nginx.serviceA typical service unit:
[Unit]
Description=My Example Service
After=network.target
[Service]
Type=simple
User=www-data
Group=www-data
ExecStart=/usr/bin/my-service --option
Restart=on-failure
[Install]
WantedBy=multi-user.targetImportant sections:
[Unit]- Metadata and dependencies.
Description=– human-readable.After=,Before=– ordering.Requires=,Wants=– dependencies.[Service]- How the service runs.
Type=– e.g.simple,forking,oneshot.ExecStart=– the main command to start the service.ExecReload=,ExecStop=– commands for reload/stop.User=,Group=– run as non-root user.Restart=– restart policy.[Install]- Ties the service into targets when you run
enable. WantedBy=multi-user.targetmeans enabling it creates a symlink so it is started whenmulti-user.targetis reached.
While a later subsection will cover “Creating custom services” in depth, you should recognize these fields when reading unit files.
Working With Configuration Overrides
Instead of editing packaged unit files directly, systemd supports drop-in overrides.
Create an override for a service:
sudo systemctl edit sshdThis opens an editor and creates:
/etc/systemd/system/sshd.service.d/override.conf
You might, for example, add environment variables or change limits:
[Service]
Environment="MY_FLAG=1"After saving:
sudo systemctl daemon-reload
sudo systemctl restart sshdView the resulting merged configuration:
systemctl cat sshdYou’ll see the original unit plus the drop-in file at the bottom.
Socket-Activated Services (Conceptual Overview)
Some services are started on demand when there’s network or socket activity. Systemd supports this via .socket units.
At a high level:
- A
.socketunit listens on a network port or Unix socket. - When traffic arrives, systemd automatically starts the associated
.serviceunit. - This can save resources by not running idle services constantly.
You interact with socket units similarly to services:
systemctl list-units --type=socket
systemctl status systemd-journald.socketThe details of writing socket-activated services are more advanced and usually covered after you’re familiar with basic service units.
Timers vs cron (High-Level)
Systemd timer units are another way to schedule tasks (similar to cron), often preferred on systemd-based systems because:
- They integrate with the unit system (logging, dependencies, etc.).
- They can handle missed runs (e.g., if the system was down).
Interaction is analogous to services:
systemctl list-timers
systemctl status my-backup.timer
You’ll learn how to create timers later; here you just need to know they exist and are managed with systemctl like other units.
Practical Examples
Below are a few practical patterns you’ll use repeatedly as an administrator.
Example: Ensuring a Critical Service Is Running and Enabled
Check if sshd is running and enabled at boot:
systemctl is-active sshd
systemctl is-enabled sshdIf not:
sudo systemctl enable --now sshdExample: Quickly Inspecting a Newly Installed Service
After installing, say, apache2 or httpd:
systemctl status httpd
systemctl list-unit-files | grep httpd
sudo journalctl -u httpd --since "5 minutes ago"Then enable and start:
sudo systemctl enable --now httpdExample: Temporarily Stopping a GUI to Troubleshoot
Switch to multi-user (text-only) target:
sudo systemctl isolate multi-user.targetWhen done, return to graphical:
sudo systemctl isolate graphical.targetDon’t do this on remote machines unless you know how you’ll get back in.
Summary
In this chapter you:
- Saw systemd’s role as the init system and service manager.
- Learned that systemd organizes everything into unit types (services, targets, timers, sockets, etc.).
- Practiced using
systemctlto: - List units and unit files.
- Start, stop, restart, enable, disable services.
- Inspect unit status and simple service state.
- Learned how to:
- Read service unit files.
- Apply configuration overrides with
systemctl edit. - Use
journalctlto gather logs for a specific service. - Touched on how targets, sockets, and timers fit into the systemd model.
These commands and concepts are core to everyday Linux administration, and you’ll use them repeatedly in later chapters on service management, logging, backups, and security.