Systemctl: Everything you need to know
Systemctl is a command-line utility that manages systemd services in Linux systems. Systemd is a system and service manager for Linux, responsible for starting and managing services during boot, among other tasks. Systemctl allows administrators to interact with and control these services. Understanding how to use systemctl effectively is crucial for managing Linux systems. This article covers everything you need to know about systemctl, from basic commands to advanced usage.
What is Systemd?
Systemd is an init system used by most Linux distributions. It manages the boot process, system services, and other system components. Introduced to replace the older SysVinit, systemd offers parallel service startup, reducing boot time. It also provides a unified way to manage services, making system administration more consistent across different distributions.
Systemctl Overview
Systemctl is the primary tool for interacting with systemd. It allows users to start, stop, enable, disable, restart, and reload services. Additionally, systemctl can query the status of services, list available units, and manage system states. It’s an essential tool for anyone managing Linux systems.
Basic Systemctl Commands
Understanding the basic systemctl commands is the first step in mastering systemd management.
Starting and Stopping Services
To start a service, use the following command:
sudo systemctl start <service_name>
For example, to start the Apache web server:
sudo systemctl start apache2
To stop a running service, use:
sudo systemctl stop <service_name>
Stopping the Apache service would look like this:
sudo systemctl stop apache2
Restarting and Reloading Services
Restarting a service can be done with:
sudo systemctl restart <service_name>
This command stops and then starts the service again. Reloading a service, however, only reloads the configuration without interrupting the service:
sudo systemctl reload <service_name>
Reloading is useful when you’ve made configuration changes that don’t require a full restart.
Enabling and Disabling Services
Enabling a service ensures it starts automatically on boot:
sudo systemctl enable <service_name>
To disable a service and prevent it from starting at boot, use:
sudo systemctl disable <service_name>
These commands modify the symbolic links in /etc/systemd/system/
to control the service’s startup behavior.
Checking Service Status
To check the status of a service, use:
sudo systemctl status <service_name>
This command provides detailed information about the service’s current state, recent logs, and whether it is active, inactive, or failed.
Advanced Systemctl Usage
Beyond basic service management, systemctl offers advanced features that give administrators finer control over systemd.
Listing All Units
Systemctl allows you to list all systemd units (services, mounts, sockets, etc.) with:
systemctl list-units
This command shows all active units. To see all units, including inactive ones, add the --all
option:
systemctl list-units --all
Masking and Unmasking Services
Masking a service prevents it from being started, even manually:
sudo systemctl mask <service_name>
This creates a symbolic link from the service’s configuration file to /dev/null
, making it impossible to start. To reverse this action, unmask the service:
sudo systemctl unmask <service_name>
Unmasking restores the service to its original state.
Checking System Boot and Shutdown Times
Systemctl can also provide information about system boot and shutdown times. To view the system’s boot history, use:
systemctl list-boots
For more detailed information about a specific boot, use:
systemctl status
This command provides an overview of the current boot session, including how long the system has been running.
Managing System States with Systemctl
Systemctl is not just for services; it also manages system states like rebooting, shutting down, or entering rescue mode.
Rebooting the System
To reboot the system, use:
sudo systemctl reboot
This command initiates a graceful reboot, ensuring all services stop correctly.
Shutting Down the System
To shut down the system, use:
sudo systemctl poweroff
This command powers off the machine safely, stopping all services first.
Entering Rescue Mode
Rescue mode is a single-user mode useful for recovery tasks. To enter rescue mode, use:
sudo systemctl rescue
This command stops most services and brings the system to a minimal state with basic functionality.
Entering Emergency Mode
Emergency mode is similar to rescue mode but with even fewer services running. It is useful for critical recovery tasks:
sudo systemctl emergency
In this mode, only the root file system is mounted, and no other services run.
Systemctl and Unit Files
Systemctl manages services through unit files, which are configuration files describing how systemd should handle a service. Understanding unit files is key to advanced systemctl usage.
Unit File Structure
Unit files are typically located in /etc/systemd/system/
and /lib/systemd/system/
. They follow a standard format with sections like [Unit]
, [Service]
, and [Install]
.
[Unit]
section describes the service and its dependencies.[Service]
section defines how the service starts, stops, and restarts.[Install]
section configures installation details like target directories.
Viewing and Editing Unit Files
To view a unit file, use:
systemctl cat <service_name>
This command displays the unit file’s contents. To edit a unit file, use a text editor like nano:
sudo nano /etc/systemd/system/<service_name>.service
After editing, you must reload the systemd daemon to apply changes:
sudo systemctl daemon-reload
Reloading ensures systemd recognizes any changes made to unit files.
Creating Custom Unit Files
You can create custom unit files for scripts or applications you want systemd to manage. Create a new unit file in /etc/systemd/system/
, then define the necessary sections.
For example, a simple custom service might look like this:
[Unit] Description=My Custom Service [Service] ExecStart=/path/to/script.sh Restart=on-failure [Install] WantedBy=multi-user.target
Once created, enable and start the service with:
sudo systemctl enable my_custom.service sudo systemctl start my_custom.service
Troubleshooting with Systemctl
Systemctl includes powerful tools for troubleshooting systemd services. Understanding these can save time and effort when diagnosing problems.
Analyzing Failed Services
If a service fails to start, use:
sudo systemctl status <service_name>
This command shows error messages and recent logs, helping identify the problem.
Viewing Detailed Logs
For more detailed information, use:
journalctl -u <service_name>
This command displays logs related to the specific service, providing further insight into what went wrong.
Debugging with Verbose Output
To debug issues, you can run systemctl with the --no-pager
option:
sudo systemctl --no-pager <command>
This command provides more verbose output, which can be useful for in-depth troubleshooting.
Conclusion
Systemctl is an indispensable tool for Linux system administration. From basic service management to advanced system control, it offers everything needed to manage a systemd-based Linux environment. Understanding how to use systemctl effectively can significantly enhance your ability to maintain and troubleshoot Linux systems. Whether you’re a novice or an experienced administrator, mastering systemctl will improve your efficiency and control over your system.
Thank you for reading the article! If you found the information useful, you can donate using the buttons below:
Donate ☕️ with PayPalDonate 💳 with Revolut