Disclaimer: ChatGPT generated document.
systemd is the dominant init system and service manager on modern Linux distributions. It is much more than “just PID 1” — it is a full userspace platform that manages:
- Boot process
- Services
- Sockets
- Devices
- Mounts
- Logging
- Timers
- Networking
- Resource control (via cgroups)
- Containers (basic)
If you run Debian, Ubuntu, Fedora, Arch, RHEL, SUSE — you are almost certainly running systemd.
Before systemd, Linux typically used:
- SysVinit (System V init scripts)
- Upstart (Ubuntu)
- OpenRC (Gentoo, Alpine)
Problems with older init systems:
- Sequential startup
- Shell-script-based
- No dependency graph
- Poor service supervision
- No unified logging
- Weak integration with cgroups
systemd was introduced in 2010 to:
- Enable parallel startup
- Replace fragile shell scripts
- Use declarative unit files
- Integrate tightly with Linux kernel features (cgroups, namespaces, epoll)
It quickly became the standard.
systemd runs as PID 1.
As PID 1, it:
- Adopts orphaned processes
- Handles signals differently
- Is responsible for system shutdown
But systemd is modular.
It consists of:
- systemd (core init)
- journald (logging)
- logind (session management)
- networkd (network config)
- resolved (DNS)
- timedated
- localed
- hostnamed
- and others
It is essentially a mini operating environment on top of Linux.
Everything in systemd is a unit.
Units are defined by text files, typically in:
/etc/systemd/system/
/usr/lib/systemd/system/
Defines a background process.
Example:
nginx.service
Implements socket activation.
Logical grouping of units (like runlevels).
Example:
multi-user.target
graphical.target
Defines filesystem mount points.
Cron replacement.
Triggered by filesystem events.
Kernel device integration.
External processes grouped under systemd.
Example:
[Unit]
Description=My C++ Server
After=network.target
[Service]
ExecStart=/usr/bin/myserver
Restart=always
User=david
MemoryMax=500M
CPUQuota=50%
[Install]
WantedBy=multi-user.targetSections:
- Dependencies
- Ordering
- Conditions
- Process execution
- Restart policy
- Resource limits
- Sandboxing
- Target binding
- Enable/disable behavior
Older init systems:
- Sequential startup
systemd:
- Builds a dependency graph
- Uses aggressive parallelization
- Uses socket activation to defer startup
Boot speed improved significantly.
systemd replaces:
- Monit
- Supervisord
- daemontools
It:
- Tracks main PID
- Restarts on failure
- Applies watchdog
- Applies rate limiting
Restart policies:
Restart=no
Restart=on-failure
Restart=always
Restart=on-abort
systemd includes:
It:
- Captures stdout/stderr of services
- Stores structured logs
- Supports binary indexed format
- Supports metadata fields (PID, UID, cgroup)
View logs:
journalctl
journalctl -u nginx
journalctl -b
journalctl -fAdvantages:
- Structured
- Indexed
- Integrated with cgroups
systemd heavily uses Linux cgroups (v2).
Each service runs in its own cgroup:
/sys/fs/cgroup/system.slice/nginx.service
You can set:
MemoryMax=
CPUQuota=
IOWeight=
TasksMax=
This is why systemd is deeply tied to modern Linux.
SysV had runlevels:
- 3 → multi-user
- 5 → graphical
systemd uses targets:
- multi-user.target
- graphical.target
- rescue.target
- emergency.target
Check default:
systemctl get-defaultChange:
systemctl set-default multi-user.targetsystemd can:
- Open sockets before service starts
- Pass file descriptors to service
Service only starts when connection arrives.
This allows:
- On-demand services
- Faster boot
- Crash-safe restart
Used heavily by:
- D-Bus
- systemd services
- Some container systems
Instead of:
* * * * * /script.sh
You create:
backup.timer
backup.service
Timer example:
[Timer]
OnCalendar=daily
Persistent=trueMore powerful than cron:
- Dependency-aware
- Uses monotonic timers
- Integrated logging
systemd provides powerful sandboxing options:
ProtectSystem=strict
PrivateTmp=yes
NoNewPrivileges=yes
CapabilityBoundingSet=
RestrictNamespaces=yes
MemoryDenyWriteExecute=yes
This can significantly harden services without containers.
For example, you can:
- Deny filesystem writes
- Block device access
- Remove capabilities
- Restrict syscalls
This is extremely useful for production servers.
There are two systemd modes:
Manages system services.
Each user can run:
systemctl --userUser services live in:
~/.config/systemd/user/
Useful for:
- Background desktop apps
- Per-user daemons
- Developer workflows
Handles:
- User sessions
- Seats
- Power management
- TTY switching
- Inhibitors (e.g., block shutdown)
Critical for desktop environments.
They provide:
- Network configuration
- DHCP
- DNS caching
- Split DNS
- VPN-aware DNS
Often used in servers.
In containers:
- Sometimes disabled
- Sometimes used as PID 1
- Works with cgroup delegation
In Kubernetes:
- Usually not used inside pods
- But used on nodes
systemd has critics:
Arguments against:
- Too large
- Violates Unix philosophy
- Binary logs (journald)
- Tight coupling to Linux
Arguments for:
- Faster boot
- Unified control
- Strong supervision
- Security features
- Modern architecture
The debate is long-standing in Linux communities.
Internally:
- Uses epoll
- Uses event-driven architecture
- Uses D-Bus for IPC
- Uses declarative unit graph resolution
- Maintains state machine per unit
systemd is essentially a dependency-driven state machine engine.
Useful commands:
systemctl status
systemctl list-units
systemctl list-dependencies
systemd-analyze blame
systemd-analyze critical-chainInspect unit:
systemctl cat nginxGiven your interest in:
- C++
- Systems programming
- Performance
- Resource control
systemd matters because:
- It is the primary interface to Linux service orchestration
- It is deeply integrated with cgroups
- It impacts performance tuning
- It impacts production deployment
If you're writing high-performance C++ services:
- You must understand systemd unit behavior
- Restart policies
- Watchdog integration
- Resource limits
- Signal handling (SIGTERM vs SIGKILL)
- PID 1 signal semantics
- Always handle SIGTERM properly.
- Use Type=notify for readiness signals.
- Use WatchdogSec with sd_notify().
- Set resource limits via unit file.
- Use PrivateTmp and ProtectSystem for security.
systemd is:
- Linux's dominant init system
- A service manager
- A resource controller (via cgroups)
- A logging framework
- A timer scheduler
- A network manager
- A security sandboxing engine
- A dependency graph resolver
It fundamentally defines how modern Linux systems behave.
If you'd like, next I can:
- Deep dive into systemd internals and source architecture
- Show how to integrate a C++ service with sd_notify and watchdog
- Explain how systemd interacts with cgroups at kernel level
- Compare systemd vs OpenRC from an OS architecture perspective
- Walk through writing a production-grade systemd unit for a high-performance C++ server
Given your systems-level interests, we can go very deep.
