Manage Systemd Services on Linux | A Comprehensive Guide

Systemd: A Unified Approach to Service Management in Linux
Currently, systemd is the predominant init system utilized across the majority of Linux distributions. This includes prominent examples such as Fedora, Red Hat, Ubuntu, Debian, openSUSE, and Arch Linux.
The systemctl command serves as the primary interface for querying the status of systemd and exerting control over the services it manages.
Standardization Across Distributions
Although its introduction was not without debate, systemd has undeniably fostered a degree of standardization in service management procedures throughout the Linux ecosystem.
Consequently, administrators can employ a consistent set of commands to manage services, irrespective of the specific Linux distribution leveraging systemd.
Privilege Escalation for System Modification
Important: When implementing system configuration changes on distributions like Ubuntu that employ sudo, it is necessary to prepend the commands with this utility.
Alternatively, on other Linux distributions, achieving the required privileges involves transitioning to the root user via the su command prior to execution.
This ensures that modifications are authorized and applied correctly within the system's security framework.
Determining Systemd Usage on Your Linux System
To ascertain if your Linux distribution utilizes systemd, initiate a Terminal window and execute the subsequent command. This will display the systemd version installed on your Linux system, confirming its presence if applicable:
systemd --version
The command's output will reveal the installed systemd version. If systemd is not present, the command will likely return an error message indicating it is not found.
Understanding Systemd
Systemd is a system and service manager for Linux operating systems. It has become the standard init system for many major distributions.
- It manages system boot, services, and other essential processes.
- Systemd offers improved performance and reliability compared to older init systems.
- Its widespread adoption makes it a crucial component for modern Linux administration.
Confirming systemd's presence is the first step in managing services and understanding your system's initialization process. Knowing this allows for effective troubleshooting and configuration.
Understanding the Boot Sequence
The systemd-analyze utility provides insights into your system's boot process. It reveals the total boot duration and identifies the services and processes contributing most significantly to the startup time.
To obtain a general overview of the boot process, execute the following command:
systemd-analyze
Detailed timing information for individual processes during startup can be accessed using this command:
systemd-analyze blame
This command lists each process and the time it consumed during the boot sequence.
Analyzing this output allows for pinpointing potential bottlenecks and optimizing system startup performance.
Understanding Systemd Units
Systemd operates utilizing "units," which represent various system resources. These units encompass services, defined by the .service extension, mount points (.mount), hardware devices (.device), and network sockets (.socket). A single command, systemctl, provides control over all these diverse unit types.
Listing Unit Files
To obtain a comprehensive list of all unit files present on your Linux system, the following command is employed:
systemctl list-unit-files
Listing Running Units
The status of currently active units can be determined by executing this command:
systemctl list-units
Identifying Failed Units
Units that have encountered errors and are in a failed state can be specifically identified using:
systemctl --failed
This command will display only those units that have not started successfully.
Analyzing these lists is crucial for effective system administration and troubleshooting.
Service Management
A comprehensive listing of both active and inactive services can be obtained using the systemctl command, mirroring its use for other systemd units, but specifically focused on services.
systemctl list-unit-files --type=service
The systemctl utility provides the means to control service operation, allowing you to initiate, halt, or restart them. Configuration reloading is also possible.
Notably, the status command is unique in that it produces visible output to the terminal; other commands operate without immediate feedback.
systemctl start name.service
systemctl stop name.service
systemctl restart name.service
systemctl reload name.service
systemctl status name.service
Employing the systemctl enable command configures systemd to automatically launch a service, or any other unit type, during system startup. Conversely, systemctl disable prevents automatic initiation of a service upon boot.
systemctl enable name.service
systemctl disable name.service
A service, or other unit, can be "masked" to entirely prevent its startup. Reversing this requires an "unmask" operation prior to future activation.
systemctl mask name.service
systemctl unmask name.service
The following image illustrates these concepts.
This overview represents only a fraction of systemd's capabilities and command set. Systemd incorporates a range of power-management functions for system shutdown, reboot, hibernation, and overall power state control.
Custom unit files can be created to define new services and mount points, or existing files can be modified to suit specific needs. Furthermore, systemd introduces "targets," which function similarly to traditional runlevels, but with key differences.
- Unlike runlevels, targets are identified by names.
- Systemd can simultaneously exist in multiple target states.
Systemd also features its own system journal, accessible via the journalctl command. By default, logs are stored in a binary format, though a plain-text format is also configurable.
For more detailed information, the Arch Linux wiki provides extensive documentation on systemd, much of which is applicable across various Linux distributions. Consulting your specific distribution’s systemd documentation is also recommended.
Image Credit: Bert Heymans on Flickr