Skip to main content

Systemd

Systemd is a modern initialization and management system for Linux systems that replaces the traditional SysV init system. It provides more advanced features for managing system services and boot processes.

CommandsDescription
systemctl show --property=UnitPathDisplay paths of unit files
systemctl show --property=UnitPath --user --no-pagerDisplay user-specific paths of unit files
systemctl list-units --type=targetSystemd Targets
systemctl list-unit-files --type target --allall target units, regardless of state
sudo systemctl list-unit-files --type=service --state=enabled --allList all enabled service units
systemctl list-unit-files --type=service --state=enabled --all --userList all user enabled service units
sudo systemctl list-unit-files | grep enabledList enabled units using grep
systemctl --state=failedList failed units
systemctl --state=active --type=targetList active targets
CommandsDescription
systemctl get-defaultGet system the default target
systemctl get-default --userGet user default target
systemctl cat graphical.targetShow content of graphical.target unit
systemctl status default.target --userShow status of user default.target unit
systemd-analyzeMeasure system startup time
systemd-analyze blameIdentify startup delay
systemctl is-system-runningOverall status of the system
systemctl daemon-reloadReload systemd configuration and units
sudo systemctl edit --full serviceEdit the specified service unit file
sudo systemctl edit --force --full serviceCreate new unit if not exists
systemd-analyze verify serviceVerify the syntax of service units
sudo systemctl isolate rescue.targetChange system state to rescue target
systemctl soft-rebootReboot the system without touching the kernel
systemctl status PIDShow the status of a specific process

list-units shows only loaded units by systemd, while list-unit-files displays all available unit files, loaded or not

How system boots

  1. When the system boots up, the firmware (BIOS or UEFI) initializes the hardware and starts the bootloader (like GRUB 2)
  2. The bootloader then loads both the Linux kernel and an initial RAM-based filesystem (initramfs) into memory
  3. The initramfs contains a small executable called init
  4. This init executable is actually a version of systemd, which performs necessary actions such as loading appropriate filesystem drivers, handling device events with udev, ...
  5. Once the real root filesystem is found, checked, and mounted, a second instance of systemd takes over as the main system and service manager, and this instance is what gets the PID 1
  6. This systemd instance starts other system services like systemd-journald and a user manager instance for each logged-in user. These user manager instances are started as user@UID.service, where UID is the numerical user ID of the logged-in user. These instances use the same executable as the system manager, but start a different set of units specific to each user
  7. Each systemd --user instance manages a hierarchy of units specific to that user.

The process of starting a user manager instance in systemd is handled through the pam_systemd module. This module is responsible for registering user sessions with the systemd login manager, systemd-logind.service

# Firmware initializes hardware and loads bootloader
firmware -> bootloader (GRUB)

# Bootloader loads kernel and initramfs into memory
bootloader -> kernel, initramfs

# initramfs runs init (systemd)
initramfs -> init (systemd)

# init prepares and mounts the real root filesystem
init -> mount root filesystem

# init executes the main systemd instance on the root filesystem
init -> systemd

# systemd starts system services and user sessions
systemd -> services, user sessions

# Each user session may run a systemd --user instance
user session -> systemd --user

Targets

Systemd targets are units in the systemd initialization system that represent specific system states or operational modes. They are used to group and manage other units, such as services, that are relevant to a particular mode of operation

systemctl cat graphical.target

Isolate

Start the unit specified on the command line and its dependencies and stop all others, unless they have IgnoreOnIsolate=yes

Add a system service

sudo nano /etc/systemd/system/sshtunnel.service

[Unit]
Description=SSH Tunnel
After=network.target

[Service]
User=mlibre
Restart=always
RestartSec=20
ExecStart=ssh -L 0.0.0.0:1234:localhost:3128 -N pachan@43.204.151.127 -p 8756

[Install]
WantedBy=graphical.target

sudo systemctl daemon-reload
sudo systemctl enable sshtunnel
sudo systemctl reenable sshtunnel.service
sudo systemctl restart sshtunnel
sudo systemctl status sshtunnel
sudo journalctl -f -u sshtunnel

Add a user service

nano ~/.config/systemd/user/my-service.service

[Unit]
Description=My User Service

[Service]
ExecStart=echo "hi everyone, hope good things happen for all"

[Install]
WantedBy=default.target


systemctl --user daemon-reload
systemctl --user enable my-service
systemctl --user reenable my-service.service
systemctl --user restart my-service
systemctl --user status my-service
journalctl --user -f -u my-service

Run a script after suspending has finished (resume)

sudo nano /etc/systemd/system/gamma.service
[Unit]
Description=Start Script in terminal
After=suspend.target graphical.target

[Service]
User=mlibre
Type=idle
Environment=DISPLAY=:0
ExecStartPre=/bin/sleep 10
ExecStart=/bin/bash /usr/local/bin/gamma_on_startup

[Install]
WantedBy=suspend.target graphical.target
sudo systemctl daemon-reload
sudo systemctl enable gamma.service
systemctl status gamma
journalctl -u gamma