If you manage Linux servers, understanding systemd is a must-have skill nowadays. Systemd has been widely adopted as the default init system and service manager in most mainstream distros like RHEL, CentOS, Ubuntu, Debian and opensuse. It starts services in parallel at boot time for faster load times and offers unified control of system components via systemctl.
In this comprehensive guide, let‘s deep dive into essential systemd concepts and how to manage system services using systemctl commands. Even if you have used SysV init in the past, systemd represents a completely new way of thinking about service management in Linux.
Why Systemd? A Better Init System
Traditionally Linux used a System V (SysV) style init system which booted all services serially. This slowed down boot times as servers added more services. SysV init shell scripts also made service management messy with no unified control.
Systemd was created specifically to overcome issues with SysV init by parallelizing service startups to optimize boot time. It also centralizes management by having unified unit files for each component rather than individual scripts spread across folders. Some key improvements over legacy init systems:
Speed: Boots services simultaneously to reduce target system runtime.
Dependency handling: Service units can specify dependencies on other units intelligently.
Resource control: Integrated Linux control groups (cgroups) for tuning service resource usage.
Unified management: Centralized unit files using .service, .mount, .socket and other extensions.
Core Systemd Concepts
Before diving into systemctl usage, let‘s understand core systemd concepts. Key unit types include:
Unit | Description | File suffix |
---|---|---|
Service units | Manage daemon services | .service |
Target units | Logical groupings of services | .target |
Mount units | Control filesystem mounts | .mount |
Socket units | IPC socket activation | .socket |
These units incorporate information like metadata, dependencies, environment variables and execution instructions for systemd.
Targets are pre-defined states in system boot process. For example multi-user.target = operational system with network, logins etc. enabled.
An Overview of Systemctl Commands
systemctl is used for managing services and other unit types in systemd. Common systemctl commands include:
Managing services life-cycle: start, stop, restart, reload
Runtime status: status, is-active
On-boot behavior: enable, disable
View system state: list-units, list-unit-files
Now let‘s explore essential systemctl usage in-depth…
Stopping and Starting Services
The basic systemctl command structure for service units is:
sudo systemctl [action] [unit name]
To start a service:
sudo systemctl start apache2
Stop a currently running service:
sudo systemctl stop apache2
Restart a service:
sudo systemctl restart apache2
This will first stop it before starting it again.
To reload a service configuration without interrupting service:
sudo systemctl reload apache2
For network service changes to apply on RHEL/CentOS 7 and older, you need to restart the network service itself:
sudo systemctl restart network
Enabling Services at Boot
To start a service automatically at system bootup:
sudo systemctl enable apache2
This creates a symlink from unit file at /etc/systemd/system to the /etc/systemd/system/multi-user.target.wants folder.
To prevent a service from starting automatically:
sudo systemctl disable apache2
Checking Status Information
View service status, process logs and other runtime information:
sudo systemctl status apache2
Verify if a service is actively running:
sudo systemctl is-active apache2
Check whether a service is enabled or disabled from auto-starting on boot:
sudo systemctl is-enabled apache2
This displays the underlying enable symlink for the service.
Viewing Systemd Units
To list all active systemd units running on the system:
sudo systemctl list-units
Filter to only display loaded service units:
sudo systemctl list-units --type=service
To view all installed unit files whether running or not:
sudo systemctl list-unit-files
Managing Targets and Dependencies
System boot process relies on targets which are groupings of service units and other dependencies.
Check the default target set:
sudo systemctl get-default
List all targets:
sudo systemctl list-units --type=target
Set the multi-user target:
sudo systemctl set-default multi-user.target
This means all services for a multi-user production system will be started.
Units can also specify dependencies on other services for runtime ordering using Requires= and After= directives in unit files.
Debugging Systemd Services
Troubleshooting services first requires checking status:
sudo systemctl status apache2
Then query logs using journalctl which consolidates all systemd unit logs:
sudo journalctl -u apache2.service --since today
Boots are profiled using systemd-analyze:
systemd-analyze blame
This outputs the time taken by each service to start and stop during the boot process.
For more detailed profiling, create plots using systemd-analyze plot > output.svg.
Conclusion
Learning systemctl is essential for controlling services, mounts, sockets and other components via systemd. Compared to SysV init scripts, it offers standardized and unified management of all core Linux functionality. With practice, system administrators can quickly get proficient at handling services from the command line using systemctl.