Systemd - why not init anyway (notes - no particular order)
Overview of Systemd
Systemd is a modern init system and service manager for Linux operating systems. It is designed to overcome the limitations of the traditional SysVinit and Upstart systems. Systemd is responsible for bootstrapping the user space and managing system processes once the system is up and running.
It provides a range of functionalities, such as:
Service management: Starting, stopping, enabling, and disabling services.
Parallel startup: Faster boot times by starting services concurrently.
Socket activation: Services start only when their associated socket is accessed.
Logging: Integration with the journald system for centralized logging.
Timers: Replacing cron jobs with more flexible scheduling.
Target management: Groups of services for different states (e.g., multi-user, graphical).
Unit files: Configuration files describing services, devices, mount points, and more.
Key Components of Systemd
Unit Files:
Systemd uses unit files to define how services, sockets, mounts, and other system resources are managed.
Common types of unit files:
.service: Describes a service.
.socket: Defines a socket for inter-process communication.
.mount: Specifies a file system mount point.
.timer: Manages timer-based activation of units.
.target: Represents a group of units to manage system states.
PID 1:
- Systemd runs as the first process on the system (PID 1), managing initialization and critical system services.
Journal:
- Systemd includes a centralized logging system (journald) for collecting and storing log messages.
Basic Systemd Commands
Checking the Status of Systemd
systemctl --version
Service Management
Start a service:
sudo systemctl start <service-name>
Stop a service:
sudo systemctl stop <service-name>
Restart a service:
sudo systemctl restart <service-name>
Reload a service's configuration (without restarting):
sudo systemctl reload <service-name>
Check the status of a service:
systemctl status <service-name>
Enable/Disable Services
Enable a service to start at boot:
sudo systemctl enable <service-name>
Disable a service from starting at boot:
sudo systemctl disable <service-name>
Check if a service is enabled:
systemctl is-enabled <service-name>
System State
Reboot the system:
sudo systemctl reboot
Shut down the system:
sudo systemctl poweroff
Enter rescue mode:
sudo systemctl rescue
List all units (active or not):
systemctl list-units
Working with Unit Files
List all unit files:
systemctl list-unit-files
Edit a unit file:
sudo systemctl edit <unit-name>
Reload unit files after editing:
sudo systemctl daemon-reload
Logs and Troubleshooting
View logs for a service:
journalctl -u <service-name>
View the system log:
journalctl
View logs since the last boot:
journalctl -b
Examples
Example 1: Managing a Web Server
Start the web server:
sudo systemctl start apache2
Enable the web server to start on boot:
sudo systemctl enable apache2
Check the status of the web server:
systemctl status apache2
Example 2: Viewing Logs
View logs for SSH:
journalctl -u sshd
View logs for the last hour:
journalctl --since "1 hour ago"
Example 3: Using Timers
Create a timer that runs a backup script daily:
Create the service file
/etc/systemd/system/backup.service
:[Unit] Description=Backup Service [Service] ExecStart=/usr/local/bin/backup.sh
Create the timer file
/etc/systemd/system/backup.timer
:[Unit] Description=Run backup service daily [Timer] OnCalendar=daily Persistent=true [Install] WantedBy=timers.target
Enable and start the timer:
sudo systemctl enable backup.timer sudo systemctl start backup.timer
To list all services managed by systemctl
, you can use the following commands depending on your requirements:
List All Services
This includes both active and inactive services:
systemctl list-units --type=service
- This command displays all services currently recognized by
systemd
.
List All Service Unit Files
This shows all service unit files on the system, regardless of their active state:
systemctl list-unit-files --type=service
Output columns:
UNIT FILE: The name of the service unit file.
STATE: The enablement state (enabled, disabled, static, masked, etc.).
Filter Services Based on State
List Active Services
systemctl list-units --type=service --state=active
List Inactive Services
systemctl list-units --type=service --state=inactive
List Failed Services
systemctl list-units --type=service --state=failed
Customizing Output
Show Specific Columns
To view only specific columns (e.g., just the service names):
systemctl list-units --type=service --no-pager --no-legend | awk '{print $1}'
Show All Units (Paginated)
By default, systemctl
output is paginated. You can bypass pagination by adding --no-pager
:
systemctl list-units --type=service --no-pager
Explanation of Output
When you run systemctl list-units --type=service
, you'll see columns like:
UNIT: The name of the service.
LOAD: Whether the unit configuration is loaded.
ACTIVE: The state of the unit (active, inactive, etc.).
SUB: The detailed state (running, exited, dead, etc.).
DESCRIPTION: A short description of the service.
Example
Command:
systemctl list-units --type=service
Output:
UNIT LOAD ACTIVE SUB DESCRIPTION
dbus.service loaded active running D-Bus System Message Bus
cron.service loaded active running Regular background program processing daemon
networking.service loaded active exited Raise network interfaces
ssh.service loaded active running OpenBSD Secure Shell server
...
This output lists all services currently loaded into memory, their states, and their descriptions.