This lesson provides a foundational overview of systemd unit files and how to manage them with systemctl, the primary tool for controlling the init system. We will cover the basic structure of a unit file and the essential commands for managing Linux services.
Understanding a Systemd Unit File
A systemd unit file is a plain text file that describes a service, a mount point, a device, or another resource that systemd can manage. Here is a basic example of a service unit file named foobar.service:
[Unit]
Description=My Foobar Service
After=network.target
[Service]
ExecStart=/usr/bin/foobar
[Install]
WantedBy=multi-user.target
This simple service file is divided into sections:
- [Unit]: This section contains metadata and dependency information. The
Descriptionprovides a human-readable name for the unit. Directives likeAfterandBeforecontrol the startup order, ensuring this unit starts after the network is available. - [Service]: This section defines how to manage the service. The
ExecStartdirective is crucial, as it specifies the command to execute to start the service. Other directives likeExecStopandExecReloadcan define how to stop or reload the service. - [Install]: This section defines the behavior of the unit when it is enabled or disabled with
systemctl. TheWantedBydirective tells systemd to start this service as part of a specific target, such as themulti-user.targetfor a standard non-graphical boot.
This is just a glimpse into systemd unit files. For more advanced configurations, further reading on the topic is highly recommended.
Essential Systemctl Commands
Now, let's explore the essential systemctl commands you'll use to interact with systemd units and manage Linux services.
Listing Systemd Units
To see all active units that systemd is currently managing, use the list-units command.
systemctl list-units
Checking a Unit's Status
To view the detailed status of a specific unit, including whether it's active, enabled, and its latest log entries, use the status command.
systemctl status networking.service
Managing Service States
You can control the runtime state of a service using start, stop, and restart.
To start a service immediately:
sudo systemctl start networking.service
To stop a running service:
sudo systemctl stop networking.service
To stop and then start the service again:
sudo systemctl restart networking.service
Enabling and Disabling Services
Enabling a service creates a symbolic link that hooks it into the boot process, ensuring it starts automatically. Disabling it removes that link.
To enable a service to start on boot:
sudo systemctl enable networking.service
To disable a service from starting on boot:
sudo systemctl disable networking.service
These commands are the building blocks for service management on modern Linux systems. Mastering them is a key step in your Linux journey.