Skip to content

systemd

What Is It?

systemd is the init system and service manager for modern Linux distributions. It manages the boot process, service lifecycle (start, stop, restart, enable), logging via journald, and system targets.

Installation

dnf install systemd (pre-installed)

Key Files and Directories

Path Purpose
/etc/systemd/system/ Custom unit files
/usr/lib/systemd/system/ Package-provided unit files
/var/log/journal/ Journal logs

Configuration

systemd manages services through unit files. Package-provided units live in /usr/lib/systemd/system/. Custom or override units go in /etc/systemd/system/ (which takes precedence).

Minimal Working Configuration

A custom service unit file (e.g. /etc/systemd/system/proxy.service for a Flask app):

[Unit]
Description=Python Proxy Service

[Service]
ExecStart=/usr/bin/python3 /usr/local/lib/server.py
Environment=PYTHONUNBUFFERED=1
Restart=on-failure
Type=simple
User=proxy

[Install]
WantedBy=default.target

After creating or modifying a unit file:

systemctl daemon-reload    # Reload unit file definitions
systemctl start proxy      # Start the service
systemctl enable proxy     # Start automatically on boot

Important Directives

[Unit] section:

Description
Human-readable description of the service.
After / Before
Ordering dependencies. After=network.target means start after networking is up.
Requires / Wants
Dependency relationships. Requires is hard (failure propagates); Wants is soft.

[Service] section:

ExecStart
The command to run when the service starts. Must be an absolute path.
Type
How systemd determines the service is ready. simple (default) — the process itself is the service. forking — the process forks and the parent exits.
User / Group
Run the service as a specific user/group instead of root.
Restart
When to restart: on-failure, always, no. on-failure restarts only on non-zero exit codes.
Environment
Set environment variables for the service process.
WorkingDirectory
Set the working directory before executing the command.

[Install] section:

WantedBy
Which target pulls in this service. default.target for general services, multi-user.target for server environments.

Common Commands

# Service lifecycle
systemctl start <service>
systemctl stop <service>
systemctl restart <service>
systemctl reload <service>       # Reload config without restart (if supported)

# Enable/disable auto-start on boot
systemctl enable <service>
systemctl disable <service>

# Check status
systemctl status <service>
systemctl is-active <service>
systemctl is-enabled <service>

# List all services
systemctl list-units --type=service
systemctl list-units --type=service --state=running

# Reload unit files after changes
systemctl daemon-reload

# View unit file contents
systemctl cat <service>

# Edit a unit file (creates override)
systemctl edit <service>

Logging and Debugging

systemd includes journald, a structured logging system that captures stdout/stderr from all services.

# View logs for a specific service
journalctl -u <service>

# Follow logs in real time
journalctl -u <service> -f

# Show recent entries
journalctl -u <service> -n 50

# Show logs since last boot
journalctl -u <service> -b

# Show logs in reverse order (newest first)
journalctl -r -u <service>

# Filter by time
journalctl -u <service> --since "2024-03-01 10:00" --until "2024-03-01 12:00"

Troubleshooting checklist:

  1. systemctl status <service> — shows active state, PID, and recent log lines
  2. journalctl -u <service> -n 30 — recent log entries
  3. systemctl cat <service> — verify unit file contents
  4. systemctl daemon-reload — if you edited a unit file
  5. Check ExecStart path and permissions — must be absolute, user must have execute permission

Security Considerations

  • Dedicated service users: Always set User= to a non-root account for application services. This limits damage if the service is compromised.
  • Restart=on-failure: Ensures crashed services recover automatically, but avoid Restart=always for services that crash in a loop (use RestartSec= to add delay).
  • ProtectSystem= / ProtectHome=: systemd can restrict filesystem access. ProtectSystem=full makes /usr and /etc read-only for the service.
  • Do not edit files in /usr/lib/systemd/system/: Package updates will overwrite them. Use /etc/systemd/system/ for custom units or systemctl edit for overrides.

Further Reading

  • Concepts: Configuration Management
  • SOPs: Service Management