What is systemd?

systemd is the init system and service manager used in most Linux distributions (Fedora, Ubuntu, Arch, etc). It is the first process (PID 1) that runs after the kernel boots and is responsible for bringing up the rest of the system.

It is designed to provide a robust framework for managing the system’s lifecycle, starting from early boot and continuing until system shutdown.

Role and Functionality of systemd?

  • PID 1 Process

systemd runs as the first process during the boot sequence, orchestrated by the Linux kernel. All the other user-space processes are started directly by systemd or it’s child processes.

  • Service Management

systemd manages the startup, shutdown, monitoring, and lifecycle of system services. It replaces older init systems like SysV init and offer backward compatibility with traditional init scripts.

  • Parallelization

Unlike traditional init systems that starts services one second after another, systemd can start services in parallel, significantly reducing boot times.

  • Dependency Management

systemd handles dependencies between services and other system resources, ensuring that services start in the correct order and only when required.

  • Resource Management

It uses Linux control groups (cgroups) to track and manage system resources, providing better control and isolation of processes.

  • Logging

systemd includes a built-in logging mechanism called journalctl, which collects and manages logs from all system services, making troubleshooting easier.

List of Commands

ActionCommand ExampleDescription / UsageExtra Commands & Tips for Beginners
Start a servicesudo systemctl startStarts the specified service immediatelysystemctl status (check if running)
Stop a servicesudo systemctl stopStops the specified service immediatelysystemctl is-active (check if stopped)
Restart a servicesudo systemctl restartRestarts the specified service
Reload a servicesudo systemctl reloadReloads configuration without full restart
Enable a servicesudo systemctl enableEnables the service to start at bootsystemctl is-enabled (check enabled)
Disable a servicesudo systemctl disableDisables the service from starting at boot
Check service statussystemctl statusShows detailed status of the service
Check if activesystemctl is-activeReturns “active” or “inactive” for the service
Check if enabledsystemctl is-enabledReturns “enabled” or “disabled” for the service
Mask a servicesudo systemctl maskPrevents a service from starting, even manuallysudo systemctl unmask (to undo)
Show service infosystemctl showDisplays detailed properties of the servicesystemctl show -p
List dependenciessystemctl list-dependenciesLists units that depend on or are required by this service--before and --after flags for order[1][2]
List all servicessystemctl list-units --type=serviceLists all active service units
Reload systemdsudo systemctl daemon-reloadReloads all unit files and updates systemd after changes

What is journalctl?

journalctl is the primary command-line tool for viewing and managing logs collected by systemd’s journald service. Instead of traditional plain-text log files are created around the system, systemd centralizes logs in structured, binary format, making it easier to search, filter and analyze system and application events.

1. Viewing and Following Logs

Command ExampleDescription / Purpose
journalctlView all logs
journalctl -fFollow (tail) logs in real-time

2. Filtering Logs by Service

Command ExampleDescription / Purpose
journalctl -uView logs for a specific service

3. Filtering Logs by Time and Boot

Command ExampleDescription / Purpose
journalctl -nShow the last N log entries
journalctl --since ""Show logs since a specific time
journalctl -bShow logs from the current boot
journalctl -b -1Show logs from the previous boot

4. Filtering Logs by Priority and Content

Command ExampleDescription / Purpose
journalctl -rShow logs in reverse order (newest first)
journalctl -pFilter by log priority (e.g., -p err, -p info)
journalctl -g ""Search logs for a pattern
journalctl _PID=Show logs for a specific process ID

5. Output Formatting

Command ExampleDescription / Purpose
journalctl -o jsonOutput logs in JSON format
journalctl -o catOutput only the message content (no metadata)
journalctl --no-pagerDump logs directly to terminal (no pager)

6. Maintenance and Disk Usage

Command ExampleDescription / Purpose
journalctl --disk-usageShow journal disk usage
sudo journalctl --vacuum-time=Remove logs older than specified time

Units, Services & Target

What is Units?

A unit is any resource that systemd can manage. Each unit is described by a configuration file(aka unit file) with a specific extension indicating its type.

Common Units

  • .service
  • .target
  • .socket
  • .device
  • .mount
  • .automount, .swap, .path, .timer, .slice, and .scope

Example

A unit file can be,

  • etc/systemd/system/example.service (service unit),
  • etc/systemd/system/my-mount.mount (mount unit),
  • etc/systemd/system/daily-backup.time (time unit)

What is Services?

A service unit (.service) is used to manage applications or daemons running on the system. It defines how to start, stop, restart, or reload a service, and it’s dependencies.

Example

A simple service unit file for a custom script (/etc/systemd/system/myscript.service):

[Unit]
Description= My Custom Script Service

[Service]
ExecStart=/usr/loca/bin/myscript.sh

[Install]
WantedBy=multi-user.target

What is Targets?

A target unit (.target) is a group of units used to synchronize the system’s state. Targets are used to bring the system to a specific state, such as booting to a graphical or multi-user environment.

Common Targets

  • multi-user.target: System is ready for multiple users, but without a graphical interface.
  • graphical.target: System is ready for multiple users with a graphical interface.
  • rescue.target: System is in single-user rescue mode.

Example:

The multi-user.target is a standard target for non-graphical, multi-user system. When the system boots, it tries to reach this target, starting all services that are required for this state.

Summary of Units, Services & Target

Unit TypeExample NamePurpose/Example Use
servicemyscript.serviceManages a custom script or application
targetmulti-user.targetGroups services for multi-user, non-graphical boot
socketsshd.socketActivates a service when a network connection arrives
devicedev-sda5.deviceManages a hardware device
mountmnt-data.mountManages a filesystem mount point
timerdaily-backup.timerSchedules tasks (like cron jobs)