Systemd
Systemd is a modern init system and system manager that has become the default for many Linux distributions, including Fedora, CentOS, Debian, Ubuntu, and Arch Linux. It replaces the traditional SysVinit system, offering parallel startup of services, improved dependency management, and a unified way to manage system processes.
Overview of Systemd
Key Concepts
Unit Files: Configuration files that define how services, mount points, devices, sockets, and other system resources are managed.
Targets: Group units into a logical unit for booting (similar to runlevels in SysVinit).
Journal: A logging system that collects and manages logs from various sources.
Socket Activation: Starts services on-demand based on incoming network requests.
Basic Commands
Managing Services
Start a Service:
Stop a Service:
Restart a Service:
Reload Service Configuration:
Check Status of a Service:
Enable a Service (start at boot):
Disable a Service:
Check if a Service is Enabled:
System Commands
Reboot the System:
Shut Down the System:
Halt the System:
Enter Rescue Mode:
Enter Emergency Mode:
Unit Files
Unit files describe various system resources and are located in directories like /etc/systemd/system/
and /lib/systemd/system/
. They have different types, such as:
Service Units (
.service
): Describe services.Target Units (
.target
): Group other units for booting.Mount Units (
.mount
): Define mount points.Socket Units (
.socket
): Describe network sockets.Timer Units (
.timer
): Schedule tasks.
Example Service Unit File
Here is an example of a simple service unit file for a hypothetical web server (/etc/systemd/system/example.service
):
[Unit]: General information about the service, including dependencies.
[Service]: Service-specific details, including the command to start the service.
[Install]: Installation information, defining the target to which this service should be linked.
Managing Logs with Journal
Systemd uses the journal
to manage logs. The journalctl
command allows you to view and query these logs.
Basic journalctl
Commands
journalctl
CommandsView All Logs:
View Logs for a Specific Unit:
View Logs Since Boot:
Follow Real-Time Logs:
View Logs by Time:
Targets
Targets in systemd are similar to runlevels in SysVinit. They group units into a logical unit for booting. Common targets include:
default.target: The default target that systemd loads at boot.
multi-user.target: Similar to runlevel 3 in SysVinit (multi-user mode).
graphical.target: Similar to runlevel 5 in SysVinit (multi-user mode with graphical interface).
rescue.target: Similar to single-user mode in SysVinit (maintenance mode).
emergency.target: A minimal environment for emergency repairs.
Example: Creating a Custom Service
Suppose you have a custom script that you want to run as a service. Here’s how to create a systemd service for it:
Create Your Script (e.g.,
/usr/local/bin/my_script.sh
):Make it executable:
Create a Service Unit File (
/etc/systemd/system/my_script.service
):Reload Systemd Configuration:
Start and Enable the Service:
Check the Status:
Conclusion
Systemd is a powerful and flexible system and service manager that provides many advantages over traditional init systems like SysVinit. It supports parallel service startup, on-demand service activation, and centralized management of logs and services. Understanding how to manage services, configure unit files, and use journalctl
for logging is essential for modern Linux system administration.
Last updated