Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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.
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 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:
Key systemd directories:
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.
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.
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.
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 prevents a service from being started, even manually:
sudo systemctl mask <service>.service # Prevent service start
sudo systemctl unmask <service>.service # Remove the mask
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.
[Service]: Configuration for service units.
[Install]: How the unit is linked to targets.
sudo systemctl daemon-reload
Start and enable the custom service:
sudo systemctl start my-custom.service
sudo systemctl enable my-custom.service
Targets group units to manage system states.
Change the default target:
sudo systemctl set-default <target>.target
Switch to a target:
sudo systemctl isolate <target>.target
systemd-analyze
Show critical boot chains:
systemd-analyze critical-chain
List all failed units:
systemctl --failed
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
journalctl -u <service>.service
Follow logs in real-time:
journalctl -u <service>.service -f
Show all logs since boot:
journalctl -b
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>