Systemd screenshot.

The systemd to-the-point guide and cheat sheet

Systemd is currently the fastest growing Linux and widely adopted service manager and init system. This part guide, part cheatsheet will give you all the commands in a neatly organized fashion so you can master it and become a proficient Linux power user.

Introduction

Systemd is a system and service manager for Linux, designed to improve boot times, manage system states, and control services efficiently. It replaces older init systems like SysVinit and Upstart with a modern approach, focusing on parallel service startup, dependency management, and a unified interface for managing system resources.

Systemd is structured around units, which represent resources like services, timers, mounts, and sockets. Understanding and using systemd effectively is crucial for managing Linux systems, whether you’re an admin or developer. It’s above all else, a powerful and versatile tool for managing Linux systems.

Understanding its unit structure, commands, and advanced features can help you optimize system performance and streamline administration tasks. Practice creating and managing unit files, experimenting with timers, and analyzing logs to gain proficiency (and this is the most important part, if you need to get good at it and be a proficient systems manager, you’ll need to practice. In short, if you want to be good at it, toy with it! create your own VM and practice, and if you break things, you restore a snapshot 🙂 ).

Note: for a comprehensive yet lean history and evolution of Linux init systems, see this article: An Introduction to Linux Init Systems and their evolution through time.

Is systemd a Program?

Well, yes, in short everything is a program, but more specifically, systemd is a relatively low level part of your system. Basically, it comes to this: Bios/UEFI -> Boot loader (think Grub) -> Kernel -> systemd

As you can see your init system is down there interacting near the kernel level.

Units and Components

Units

Units are the core of systemd. Each unit has a type, configuration, and behavior. In short, you’ll use these to completely control all the processes and daemons in your system (using service), the sockets, the mounted filesystems, etc. Common unit types include:

  • Service units (.service): Manage daemons and background services.
  • Timer units (.timer): Schedule and trigger actions based on time.
  • Socket units (.socket): Manage sockets for services.
  • Target units (.target): Group units into states, like multi-user.target.
  • Mount units (.mount): Control mounted file systems.

Key systemd directories:

  • /etc/systemd/system: System-specific configuration (overrides, custom units).
  • /usr/lib/systemd/system: Default units provided by packages.
  • ~/.config/systemd/user: User-specific unit files.

Remember that everything in Linux is basically a file, even your hardware is represented inside the /dev/ directory as a series of files, and your init system is no exception.

Components

Systemd is not just “a thing” but several things working together in order to offer the user the functions needed to manage services, initialize the system and perform other equally important tasks. The internal structure of systemd is beyond the scope of this guide, since this is more of a cheatsheet and introduction to help you be able to use systemd for your own use. Nonetheless, I wanted to show you this chart from the Linux Foundation which shows how complex internally systemd is.

Systemd components chart by the Linux foundation.
Systemd components chart by the Linux foundation.

Managing Services with systemctl

This is perhaps the part of systemd you’ll interact the most with, specially if you have a server and need to keep a close control and status monitoring of your daemons.

Common Commands

Start, stop, enable, disable services:

sudo systemctl start <service>.service   # Start a service
sudo systemctl stop <service>.service    # Stop a service
sudo systemctl restart <service>.service # Restart a service
sudo systemctl reload <service>.service  # Reload configuration without stopping
sudo systemctl enable <service>.service  # Enable service to start on boot
sudo systemctl disable <service>.service # Disable service from starting on boot

Query service status:

sudo systemctl status <service>.service

Example:

sudo systemctl status apache2.service

Masking Services

Masking prevents a service from being started, even manually:

sudo systemctl mask <service>.service   # Prevent service start
sudo systemctl unmask <service>.service # Remove the mask

Understanding Unit Files

Unit files define the behavior and dependencies of a service or resource.

Basic Structure:

[Unit]
Description=My Custom Service
After=network.target

[Service]
ExecStart=/usr/bin/my-service --option
Restart=always

[Install]
WantedBy=multi-user.target

Explanation:

[Unit]: Metadata and dependencies.

  • Description: Brief description of the unit.
  • After/Before: Define startup order relative to other units.

[Service]: Configuration for service units.

  • ExecStart: Command to execute for starting the service.
  • Restart: Define restart behavior (e.g., always, on-failure).

[Install]: How the unit is linked to targets.

  • WantedBy: Determines which target the unit is part of.

Creating and Editing Units

  1. Create a custom unit file:
    Save the file under /etc/systemd/system/my-custom.service or /~/.config/systemd/user/my-custom.service.
  2. Reload systemd to apply changes:
sudo systemctl daemon-reload

Start and enable the custom service:

sudo systemctl start my-custom.service
sudo systemctl enable my-custom.service

Managing Targets

Targets group units to manage system states.

  • multi-user.target: Non-graphical multi-user mode.
  • graphical.target: Graphical user interface mode.
  • rescue.target: Single-user mode with minimal services.

Change the default target:

sudo systemctl set-default <target>.target

Switch to a target:

sudo systemctl isolate <target>.target

Analyzing System Boot and Performance

  • View boot performance:
systemd-analyze

Show critical boot chains:

systemd-analyze critical-chain

List all failed units:

systemctl --failed

Timers and Scheduling Tasks

Systemd timers replace cron jobs, offering better integration and control.

Create a timer:

#/etc/systemd/system/my-task.timer
[Unit]
Description=Run My Task Every Day

[Timer]
OnCalendar=daily

[Install]
WantedBy=timers.target

Create the corresponding service:

#/etc/systemd/system/my-task.service
[Unit]
Description=My Scheduled Task

[Service]
ExecStart=/usr/bin/my-script.sh

Enable and start the timer:

sudo systemctl enable my-task.timer
sudo systemctl start my-task.timer

Debugging and Logging

  • View logs for a specific service:
journalctl -u <service>.service

Follow logs in real-time:

journalctl -u <service>.service -f

Show all logs since boot:

journalctl -b

Advanced Features

  • Edit live unit configurations:
sudo systemctl edit <service>.service

Changes are saved in /etc/systemd/system/.d/override.conf.

Temporary service activation:
Start a service without enabling it:

sudo systemctl start <service>.service

Dependency debugging:
List all dependencies for a unit:

systemctl list-dependencies <unit>

Leave a Reply

Your email address will not be published. Required fields are marked *