What Is the Role of systemd in Ubuntu?
This article provides an overview of systemd, the
default system and service manager in Ubuntu. It explains how systemd
initializes the operating system, manages background processes, and
handles system logging. Readers will learn the core concepts of systemd,
including units and targets, as well as the essential
systemctl commands needed to start, stop, enable, and
monitor services effectively.
Understanding systemd
In Ubuntu, systemd acts as the first process that starts after the Linux kernel boots up, holding the process ID (PID 1). It is responsible for bringing the system to a usable state by starting services, mounting filesystems, and managing network connections.
Unlike older initialization systems like SysVinit, systemd parallelizes the startup of services, which significantly reduces boot times. It remains in the background as a daemon, constantly monitoring the state of the system and ensuring that critical services are restarted if they fail.
Core Concepts: Units and Targets
To manage the system efficiently, systemd organizes tasks into different types of units. The most common unit types include:
- Service Units (
.service): Represent background applications and daemons, such as a web server (Apache/Nginx) or a database (MySQL). - Target Units (
.target): Groups of units that represent a specific system state or runlevel, such asgraphical.target(for a full desktop environment) ormulti-user.target(for a non-graphical server state). - Timer Units (
.timer): Used to trigger actions based on time, serving as a modern alternative to cron jobs.
Managing Services with systemctl
The primary tool used to interact with systemd in Ubuntu is the
systemctl command-line utility. Administrators use this
tool to control the lifecycle of background services.
Controlling Service State
To alter the immediate running state of a service, the following commands are used:
- Start a service:
sudo systemctl start <service_name> - Stop a service:
sudo systemctl stop <service_name> - Restart a service:
sudo systemctl restart <service_name>
Configuring Boot Behavior
Systemd also controls whether a service should launch automatically when Ubuntu boots up:
- Enable a service at boot:
sudo systemctl enable <service_name> - Disable a service at boot:
sudo systemctl disable <service_name>
Checking Service Status
To inspect the current state of a service, including whether it is active, inactive, or experiencing errors, use the status command:
systemctl status <service_name>
This command provides a detailed snapshot of the process, including its PID, memory usage, and recent log entries.
System Logging with journald
In addition to service management, systemd includes a centralized
logging component called journald. Instead of
scattering logs across various text files in /var/log,
journald collects log data from the kernel, system services, and boot
processes into a secure, structured binary format.
Administrators can query these logs using the journalctl
command. For example, running
journalctl -u <service_name> allows users to view
logs specific to a single service, making troubleshooting in Ubuntu
straightforward and centralized.