systemd — Complete, Concise Notes
What is systemd?
systemd is the init system and service manager for modern Linux distributions.
It is responsible for:
- Booting the system
- Starting & supervising services
- Managing processes, logs, devices, mounts, users, timers, and networking
It replaced SysVinit & Upstart in most distros (Ubuntu, RHEL, CentOS, Debian, Arch).
Why systemd exists (Problems it solved)
Old init systems:
- Sequential startup (slow)
- Poor dependency handling
- No supervision after start
- Scattered logging
systemd provides:
- Parallel startup → faster boot
- Dependency-aware service management
- Automatic restarts & monitoring
- Unified logging & control interface
Core Concepts
1. Units
Everything in systemd is a unit.
A unit is a configuration file describing something systemd manages.
Common unit types:
| Unit Type | Purpose |
|---|---|
service | Daemons / background processes |
socket | Socket activation |
target | Group of units (like runlevels) |
mount | Mount points |
automount | On-demand mounts |
timer | Cron replacement |
path | File/directory watcher |
device | Hardware devices |
slice | Resource control (cgroups) |
scope | Externally created processes |
Example unit name:
nginx.service
2. Unit File Locations
Priority order:
/etc/systemd/system/ # Admin overrides (highest priority)
/run/systemd/system/ # Runtime units /lib/systemd/system/
# Distribution default units
How systemd Boots (Boot Flow)
- BIOS/UEFI
- Bootloader (GRUB)
- Kernel loads
- PID 1 = systemd
- systemd:
- Mounts filesystems
- Starts default target
- Launches services in parallel
- Applies dependencies & ordering
Check PID 1:
ps -p 1 -o comm=
Targets (Runlevels Replacement)
Targets replace SysV runlevels.
| Target | Equivalent |
|---|---|
poweroff.target | 0 |
rescue.target | 1 |
multi-user.target | 3 |
graphical.target | 5 |
reboot.target | 6 |
Default target:
systemctl get-default
Set default:
systemctl set-default multi-user.target
Services
Service lifecycle
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx
systemctl status nginx
Enable at boot:
systemctl enable nginx
Disable:
systemctl disable nginx
Anatomy of a Service File
Example:
[Unit] Description=Nginx Web Server After=network.target
[Service] ExecStart=/usr/sbin/nginx -g 'daemon off;' Restart=always User=www-data
[Install] WantedBy=multi-user.target
Sections explained
- [Unit] → description & dependencies
- [Service] → how process runs
- [Install] → when to start (target linkage)
Dependencies & Ordering
Dependency types
Requires=→ hard dependencyWants=→ soft dependencyAfter=→ start orderBefore=→ inverse order
Example:
Requires=network.target After=network.target
Socket Activation (Key systemd Feature)
systemd:
- Opens socket before service starts
- Starts service only when traffic arrives
Benefits:
- Faster boot
- Lower memory usage
Example:
sshd.socket → sshd.service
Timers (Cron Replacement)
Timer unit + service unit.
Example:
systemctl list-timers
Timer benefits over cron:
- Missed jobs run after reboot
- Dependency-aware
- Unified logging
Logging — journald
systemd logging is handled by journald.
View logs:
journalctl journalctl -u nginx journalctl -b journalctl -f
Persistent logs:
/var/log/journal/
Process & Resource Management (cgroups)
systemd uses cgroups to:
- Track all processes of a service
- Enforce limits
Examples:
MemoryLimit=500M CPUQuota=50%
Check cgroups:
systemd-cgls
systemd vs traditional init
| Feature | SysVinit | systemd |
|---|---|---|
| Startup | Sequential | Parallel |
| Dependencies | Manual | Automatic |
| Supervision | ❌ | ✅ |
| Logging | syslog | journald |
| Resource control | ❌ | cgroups |
| Socket activation | ❌ | ✅ |
Common Debugging Commands
systemctl status <service> journalctl -xe systemctl list-units systemctl list-unit-files systemctl show <service>
Key systemd Components
| Component | Role |
|---|---|
systemd | PID 1 init |
systemctl | Control interface |
journald | Logging |
logind | User sessions |
udevd | Device manager |
timesyncd | Time sync |
resolved | DNS |
networkd | Network management |
Why interviewers care
systemd shows:
- Linux internals knowledge
- Service reliability understanding
- Debugging ability
- Production readiness
One-line summary (remember this)
systemd is a dependency-aware, parallel init and service management system that controls how Linux boots, runs, logs, and manages resources.