Overview
systemd plays a crucial role in efficiently managing the system boot process, supervising running services, and controlling various system states. Unlike init, which executes scripts sequentially, systemd parallelizes services based on dependencies, significantly improving boot speed. This is a vital factor contributing to the performance and stability of modern Linux systems.
Key Features
The key features of systemd are as follows:
- Parallel Booting: Analyzes service dependencies and runs multiple services simultaneously, shortening boot time.
- Integrated Management: Manages various elements such as system boot, services, devices, and logs within a single framework.
- Service Monitoring: Helps ensure stable operation by automatically restarting services if they fail.
- Runlevel Replacement: Introduces the concept of 'targets' instead of runlevels to control system states.
Relationship with init
systemd is the successor and replacement for traditional init systems. It inherits the role of system startup and shutdown performed by init, adding improved functionalities.
- init: The first process to run, which referred to the `/etc/inittab` file during boot to sequentially execute scripts based on runlevels.
- systemd: Also PID 1 process, but uses 'targets' instead of runlevels, and processes services in parallel based on dependencies, making it faster and more flexible.
- Compatibility: In most modern Linux systems, init is symbolically linked to systemd, so using the init command will still cause systemd to act.
Core Concepts
Essential core concepts for understanding systemd.
Unit
All objects managed by systemd (services, mount points, devices, etc.) are defined as 'units'. Each unit has a configuration file (`*.service`, `*.mount`, etc.) that defines a specific task.
- Service Unit: Manages background processes like web servers (nginx.service) or databases (mysql.service).
- Target Unit: Groups multiple units. It's a concept that replaces runlevels; 'multi-user.target' means multi-user mode, and 'graphical.target' means graphical mode.
- Mount Unit: Manages file system mount points.
systemctl
The primary command-line utility for controlling systemd. It is used to start, stop, restart services, or check unit status.
- Start a service: sudo systemctl start nginx
- Check service status: sudo systemctl status nginx
- Reboot system: sudo systemctl reboot
journalctl
A command to view system logs centrally managed by systemd. Logs can be filtered by service, time, or priority.
- View all logs: journalctl
- View logs for a specific service: journalctl -u nginx.service
- View real-time logs: journalctl -f
Usage Examples
Usage examples of systemctl and journalctl, the core commands of systemd.
Check System Boot Target
systemctl get-default
Checks which target the system booted into.
Shut Down System
sudo systemctl poweroff
Safely shuts down the system via systemd. Performs the same function as the poweroff command.
Enable/Disable Specific Service
sudo systemctl enable nginx
sudo systemctl disable nginx
Configures a specific service to start automatically at system boot, or disables it.
Restart Service
sudo systemctl restart nginx
Restarts a running web server service. Useful when configuration files have been changed.
Tips & Precautions
Useful information when using systemd.
Tips
- systemd unit files are primarily located in `/etc/systemd/system/` or `/lib/systemd/system/` directories.
- systemd manages most boot/shutdown-related commands in an integrated manner. Therefore, commands like `halt`, `reboot`, and `poweroff` are often symbolic links to `systemctl`.
- The `systemctl status` command allows you to check not only the service status but also recent logs at once.