Discover hosting solutions tailored to your workflow
- Host World
- Blog
- Operating system, OS
- Linux
- An Introduction to Systemctl: The Core of Service Management in Linux
An Introduction to Systemctl: The Core of Service Management in Linux
-
15 min read
-
15
- Viewing Active Units
- Understanding the Output
- Managing Unit States with the Systemctl Command
- Working with Masked Units and Creating Unit Files in Systemd
- Creating a New Unit File
- Explanation of Settings
- Viewing and Analyzing Unit Files and Dependencies
- Checking Unit Dependencies
- Viewing Unit Properties
- Editing and Removing Unit Files
- What Are Systemd Targets and How Do They Manage System States?
Systemctl is an essential command-line tool used for managing the systemd service manager in Linux. It includes various utilities, libraries, and daemons, all of which replace the older SysV init system. This tool allows users to efficiently monitor and control services, providing in-depth details about individual systemd services as well as those with system-wide roles. In this article, we will explore what is systemctl in Linux, its key functions, and how to use it to manage and monitor systemd services on Linux systems.
Systemctl and What It Does
What is systemctl and why is it integral to managing system services on a Linux machine? By utilizing this command, administrators can interact with systemd to control, check, and manage the status of services that run on the system. It is a powerful tool for ensuring services are running smoothly and meeting all dependencies.
The systemctl command is designed to simplify the management of services, making it an indispensable utility for anyone responsible for maintaining a Linux-based server or workstation. Whether you're starting, stopping, or checking on the status of a service, systemctl provides the necessary control and flexibility.
Service Management: A Vital Part of Systemd
Managing services is a crucial aspect of maintaining any Linux system, as it ensures that the necessary programs are running and performing their intended functions.
What is a Service in the Context of Systemctl?
In systemd, a service is referred to as a unit. A unit can be a software application, a device, or any other system resource that the operating system can manage. These units are managed using systemd unit files, which specify how each service should start, stop, or restart.
The systemctl command uses these unit files to manage services, ensuring they operate correctly and meet all requirements. Units are the building blocks of a Linux system, essential for maintaining its functionality and stability.
Checking the Status of a Service
One of the most useful features of systemctl is its ability to check the current status of any systemd service. By using the systemctl status command, you can gather valuable information about a service, such as whether it's running and why it may not be functioning as expected. If you're logged in as a non-root user, you'll need to use elevated privileges to run the systemctl commands.
[su_box title="Note:" style="glass" box_color="#3ac6eb" radius="20"]For each command or file path in this tutorial, replace service_name with the name of the service you wish to control.[/su_box]
systemctl status service_name.service
You can reference a unit without including the .service extension. Systemctl will automatically search for unit files with the .service suffix, allowing you to call the unit by its name alone. This means you don't need to explicitly add the .service extension when running commands.
systemctl status service_name
For example, here is what the output of the status command looks like when checking the httpd service.
[root@host ~]# systemctl status httpd
- httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2020-05-08 14:15:54 EDT; 1 weeks 2 days ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 3767 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 1321 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─1321 /usr/sbin/httpd -DFOREGROUND
├─3769 /usr/sbin/httpd -DFOREGROUND
Starting, Stopping, Restarting, and Reloading Services with the Systemctl Command
The systemctl command is essential for starting and stopping services in Linux. You can manage systemd services through unit files, which can be referenced with or without the .service extension.
To start a service, use the following systemctl status command:
systemctl start service_name
The same rules apply when stopping a service. To stop a running service:
systemctl stop service_name
It’s important to note that the systemctl start command can only be executed on services that are currently inactive, while the systemctl stop command can only be used on active services.
Restarting or Reloading Services
To avoid manually stopping and starting a service, you can use the systemctl restart command. This command allows you to restart a service without issuing separate stop and start commands. It gracefully stops the service and then restarts it, ensuring any configuration changes are applied.
systemctl restart service_name
In some cases, a full restart may not be necessary. If you only need the service to re-read its configuration files, you can use the systemctl reload command:
systemctl reload service_name
If you are unsure whether to restart or reload a service, systemctl offers the reload-or-restart command, which automatically decides the appropriate action.
systemctl reload-or-restart service_name
This provides a streamlined way to ensure that your services are running with the latest configuration changes.
Enabling or Disabling a Service with Systemctl
For cloud server services, it’s crucial to ensure that essential services remain active and online at all times. Manually starting each service can be time-consuming, especially when many services need to run concurrently. To simplify this process, the systemctl enable and systemctl disable commands allow you to manage how systemd services behave when the server starts or reboots.
By using systemctl enable, you can configure systemd to automatically start a service upon boot. This ensures that the service starts without needing manual intervention.
systemctl enable service_name
Many services, such as HTTPD, MySQL, and MariaDB, are preconfigured to start automatically. However, some services may require manual enabling to ensure they launch at boot. On the other hand, to prevent a service from starting automatically, you can disable it with the following command:
systemctl disable service_name
This command stops the service from starting on boot and keeps it inactive until manually started. To check whether a service is active or not, you can use the systemctl status command to display its current state.
systemctl status service_name
System State Management with Systemctl
The systemctl command provides a variety of functionalities beyond simply managing individual services. With these powerful commands, you can gain detailed insights into the system's state, monitor running jobs, and manage system configurations. Here's an overview of some key systemctl commands and their uses.
Viewing All Active Units
To get a complete list of all currently active units on your system, including services, timers, sockets, and other system resources, use the following command:
systemctl list-units
This will show all the units that are actively running, giving you a quick overview of system resources.
Displaying Dependent Units
Sometimes, units are dependent on others, meaning they must be started or stopped in a specific order. To see which units rely on a particular one, you can use:
systemctl list-dependencies
This command provides a hierarchical view of unit dependencies, which can help ensure that services are managed in the correct sequence.
Listing Active Systemd Jobs
Systemd jobs track the progress of operations like starting or stopping services. To view all active systemd jobs, use:
systemctl list-jobs
This command shows all currently running jobs, allowing you to monitor ongoing operations.
Reloading Systemd Configuration Files
After modifying service files or adding new units to the system, reload the systemd manager configuration with:
systemctl daemon-reload
This ensures that systemd recognizes any changes and applies them accordingly.
Checking Service Status and Auto-Start Settings
To check if a service is running, use:
systemctl is-active service_name
Similarly, to determine whether a service is set to auto-start on boot:
systemctl is-enabled service_name
These commands allow you to check both the current status and configuration of services, helping you manage their behavior effectively.
What Are Unit Files in Systemd? Overview and Configuration
A unit file is a plain-text configuration file used in systemd to define the properties of various system units, such as services, sockets, devices, or mount points. These files contain crucial information about how each unit should be started, stopped, and managed by systemd.
Unit files generally consist of several sections, each serving a specific purpose. These sections define dependencies between units, specify the commands for execution, set resource limits, and configure logging. By using these unit files, systemd can effectively manage and control the system's resources, ensuring that services run in the correct order and with appropriate settings. In essence, unit files simplify the organization and management of system components, improving system performance and stability.
Managing and Listing Unit Files with Systemctl
Viewing Active Units
The systemctl list-units command allows you to display all active systemd services on your server. The output is similar to what you see with the systemctl status command, but provides a broader overview without the detailed status information.
systemctl list-units
Here’s an example of the output from this command:
root@host [~]# systemctl list-units
UNIT LOAD ACTIVE SUB DESCRIPTION
crond.service loaded active running Command Scheduler
dovecot.service loaded active running Dovecot IMAP/POP3 email server
firewalld.service loaded active running firewalld - dynamic firewall daemon
httpd.service loaded active running The Apache HTTP Server
mariadb.service loaded active running MariaDB database server
named.service loaded active running Berkeley Internet Name Domain (DNS)
Understanding the Output
UNIT: The name of the systemd unit.
LOAD: Indicates if the unit’s configuration is loaded into memory.
ACTIVE: Shows whether the unit is running or inactive.
SUB: Provides further details about the unit’s state.
DESCRIPTION: A brief summary of what each unit does.
For a more detailed view of a specific unit, you can use systemctl status to get in-depth information.
Managing Unit States with the Systemctl Command
The systemctl command is an essential tool for managing systemd services and units on Linux. It provides detailed information about the state of all active services, allowing users to manage and monitor system resources effectively. To get a comprehensive list of all services and their statuses, you can use the –all flag with the systemctl list-units command. This command will show not only the units currently loaded and active but also services that are inactive, dead, failed, or not found on the system’s disk.
systemctl list-units --all
This output offers a full picture of the current state of all units running or attempted by systemd. You can refine this output using the –state= flag. For example, if you want to see only the failed units, you would use:
systemctl list-units --all --state=failed
You can also filter by specific unit types, such as mount units, with:
systemctl list-units --all --type=mount
In addition to list-units, the systemctl list-unit-files command displays all unit files available on the server, including those not loaded into systemd’s memory. This command helps you manage units that are configured but not actively running.
systemctl list-unit-files
The output shows the unit file's name along with its state, such as Enabled, Disabled, Static, or Masked. These states indicate whether a unit is set to start automatically during boot or whether it’s manually managed. Using these commands, you can effectively track, filter, and control system services and their behaviors, ensuring optimal system performance and stability.
Working with Masked Units and Creating Unit Files in Systemd
When using the systemctl list-unit-files command, some units may show a static status, meaning they lack an install section and cannot be enabled. These units are essential for the system’s core functionality. Additionally, masked units are explicitly disabled, preventing them from starting until unmasked. If you try to start a masked unit using the systemctl start command, you’ll receive an error message:
systemctl start service_name.service
Failed to start service_name.service: Unit is masked.
To unmask a unit and enable it again, you can use the systemctl unmask command:
systemctl unmask service_name.service
This command allows you to unmask the unit and then use systemctl enable to allow it to start automatically on boot.
Creating a New Unit File
In some situations, you may need to create a custom unit file for a daemon or a specific instance of an existing service. Unit files are created under the /etc/systemd/system/ directory and require root permissions. Here's how you can create and set up a new unit file:
touch /etc/systemd/system/service_name.service
chmod 644 /etc/systemd/system/service_name.service
Next, open the newly created file in a text editor (such as vim or nano) and add the configuration options. A basic example of a unit file would look like this:
[Unit]
Description=This is the manually created service
After=network.target
[Service]
ExecStart=/path/to/executable
[Install]
WantedBy=multi-user.target
Explanation of Settings
- Description: This describes the service and will appear when using systemctl status for the service.
- After: Indicates when the service should start relative to other services (e.g., after the network is up).
- ExecStart: Specifies the path to the executable for the service.
- WantedBy: Defines the target under which the service should start, similar to runlevels in older systems.
Once the file is created, reload systemd to apply the changes:
systemctl daemon-reload
systemctl start service_name.service
This ensures the new service is correctly loaded and started.
Viewing and Analyzing Unit Files and Dependencies
Once a unit file is created, you don’t have to manually open it to see its content. The systemctl command provides an efficient way to display the contents of any unit file directly. You can do this by running the following command:
systemctl cat service_name.service
This command shows you the full configuration of the service, eliminating the need to navigate to the file manually.
Checking Unit Dependencies
In addition to viewing unit files, you can also check the dependencies of a specific unit. The systemctl list-dependencies command reveals which units are dependent on the specified unit and how they interact with each other.
systemctl list-dependencies service_name.service
For example, if you run:
[root@host ~]# systemctl list-dependencies httpd.service
httpd.service
├─-.mount
├─system.slice
└─basic.target
...
There are useful flags that can refine the output. The –all flag lists all dependencies recursively, while –reverse shows dependencies in reverse order. You can also use –before and –after to list units that start before or after the specified one.
Viewing Unit Properties
To examine a unit's properties in detail, use the systemctl show command. This provides information such as the unit's type, restart behavior, and various timeouts:
systemctl show service_name.service
For instance:
[root@host ~]# systemctl show httpd.service
Type=notify
Restart=no
MainPID=4004
...
If you are interested in a specific property, you can use the -p flag to isolate it, such as:
systemctl show httpd.service -p MainPID
Editing and Removing Unit Files
When you need to modify a unit file, systemctl provides a built-in editing tool that simplifies the process, eliminating the need to manually locate the file on the server. You can modify a unit file directly using the edit command:
systemctl edit httpd.service
This creates a new directory under /etc/systemd/system with the same name as the service, but with a .d extension. For example:
/etc/systemd/system/httpd.service.d
The edits are stored here, and any changes made in this directory override the main unit file. The file created for modifications is typically named override.conf. If you prefer to edit the unit file directly rather than creating an override, you can use the --full option:
systemctl edit --full httpd.service
To remove a unit file or override snippet, you can use the following commands:
rm -r /etc/systemd/system/httpd.service.d
This command removes the directory containing the override. To delete the unit file completely:
rm /etc/systemd/system/httpd.service
After editing or removing unit files, it is crucial to reload systemd to apply the changes using:
systemctl daemon-reload
This ensures that your modifications take effect and systemd recognizes the updates. Be sure to check the status of your modified service using systemctl status to confirm the changes are applied correctly. Additionally, editing unit files allows for more granular control over service behavior, making it a vital tool for system administrators.
What Are Systemd Targets and How Do They Manage System States?
A systemd target is a special type of unit file that defines a specific system state or synchronization point. Targets are easily recognizable by their .target suffix and serve as the foundation for managing system states in systemd.
Similar to runlevels in older init systems, targets allow administrators to control the system’s state. For example, multi-user.target is the default target, corresponding to runlevel 3, which boots the system into a non-GUI console interface. Meanwhile, graphical.target corresponds to runlevel 5 and boots the system with a full graphical interface.
Although systemd comes with predefined targets, users can create custom ones to suit specific needs. By using systemd targets, administrators can seamlessly switch between system states, simplifying system management and troubleshooting in complex environments.
How to Manage Targets and Control Server States with Systemctl
Systemctl provides a powerful way to manage targets in systemd. You can list all available target files on your system using:
systemctl list-unit-files --type=target
Unlike traditional runlevels, systemd targets allow you to activate multiple states at once. To see all active targets on your system, use:
systemctl list-units --type=target
In addition to managing targets, the systemctl command can control your server’s state. You can stop, power off, or reboot the server easily using the following commands:
To stop the server:
systemctl halt
To power off the server completely:
systemctl poweroff
To reboot the server:
systemctl reboot
Conclusion
The systemctl command is an essential tool for managing Linux systems. It provides you with full control over systemd targets, making it easy to define and manage your system states. With systemctl, you can stop, reboot, or power off services efficiently, simplifying maintenance and troubleshooting tasks. By mastering this command, you will optimize system performance, reduce downtime, and ensure everything runs smoothly.
If you need any help or have questions about using systemctl, feel free to reach out. Our team is always ready to assist you and guide you through any challenges.
Leave your reviewsShare your thoughts and help us improve! Your feedback matters to us