
Mastering systemd: A Practical Guide for Modern Linux
In the world of modern Linux, systemd
is the undisputed king of system and service management. While it has been a source of debate, its power, efficiency, and widespread adoption make it an essential tool for any system administrator or advanced Linux user. More than just an init system, systemd
is a comprehensive suite of tools for starting, stopping, and monitoring everything on your server.
This guide will demystify systemd
, providing a practical playbook of the most important commands and concepts you need to manage a Linux system effectively.
The Core Command: Getting to Grips with systemctl
The primary tool you will use to interact with systemd
is systemctl
. This single command is your gateway to controlling services, viewing their status, and managing how they behave at boot.
1. Managing Services (Starting, Stopping, and Checking Status)
These are the bread-and-butter commands you’ll use daily. The syntax is straightforward and intuitive. Remember to use sudo
for actions that require root privileges.
- To start a service:
bash
sudo systemctl start servicename.service
- To stop a running service:
bash
sudo systemctl stop servicename.service
- To restart a service (stop then start):
bash
sudo systemctl restart servicename.service
- To reload a service’s configuration without a full restart:
bash
sudo systemctl reload servicename.service
- To check the detailed status of a service: This is incredibly useful for troubleshooting, as it shows if the service is active, its PID, memory usage, and the latest log entries.
bash
systemctl status servicename.service
2. Controlling Services at Boot Time
Starting a service is temporary; it won’t survive a reboot. To make a service start automatically when the system boots, you need to enable it.
- To enable a service to start on boot:
bash
sudo systemctl enable servicename.service
- To prevent a service from starting on boot:
bash
sudo systemctl disable servicename.service
- To check if a service is currently enabled:
bash
systemctl is-enabled servicename.service
It’s a crucial distinction:start
/stop
are for the current session, whileenable
/disable
control boot-time behavior.
Understanding systemd Unit Files
Everything systemd
manages is defined by a configuration file called a unit file. These plain text files, typically ending in .service
, .target
, or .timer
, describe a resource and how to manage it. You can find them in directories like /etc/systemd/system/
and /lib/systemd/system/
.
A typical .service
file is broken into three main sections:
[Unit]
: This section contains metadata about the unit, such as aDescription
and dependencies (After=
orWants=
). For example, you might specify that your web application service should only startAfter=network.target
.[Service]
: This is the most important section for services. It defines how the service should be managed. Key directives includeExecStart
(the command to run),User
(the user to run the process as), andRestart
(how to handle failures, e.g.,Restart=always
).[Install]
: This section defines how the unit should behave when it’s enabled or disabled. TheWantedBy=
directive is common here, tellingsystemd
which target (like a runlevel) should include this service.WantedBy=multi-user.target
is a standard choice for services that should run in a typical multi-user environment.
Powerful Logging with journalctl
Forget grepping through massive text files in /var/log
. systemd
has its own powerful, centralized logging system called the journal. You can access it using the journalctl
command.
The journal offers structured, indexed logging, making it incredibly fast and easy to filter.
- To view all logs (newest at the bottom):
bash
journalctl
- To follow logs in real-time (like
tail -f
):
bash
journalctl -f
- To view logs for a specific service: This is a lifesaver for debugging.
bash
journalctl -u nginx.service
- To filter logs by time:
“`bash
journalctl –since “1 hour ago”
journalctl –since
Source: https://linuxhandbook.com/courses/systemd-playbook/