WindowsMacSoftwareSettingsSecurityProductivityLinuxAndroidPerformanceConfigurationApple All

How to Manage Services Using systemctl

Edited 2 weeks ago by ExtremeHow Editorial Team

InitializationServicesCommand LineSystemdManagementControlStartup ScriptsSysAdminConfigurationMonitoring

How to Manage Services Using systemctl

This content is available in 7 different language

In any Linux-based system, services are an integral aspect. These services are background processes that run continuously to contribute to various functionalities such as networking, database hosting, web servers, and much more. Managing these services efficiently is an important part of system administration. This guide will explain how to manage services using systemctl, which is a command-line utility to monitor and control the systemd system and service manager.

Understanding systemd and systemctl

Before delving into systemctl, it's important to understand what systemd is. Systemd is a system and service manager for Linux that provides parallelization capabilities, uses sockets and D-Bus activation to start services, provides on-demand daemon starting, keeps track of processes using Linux control groups, and maintains mount and automount points.

systemctl is a command-line utility used to manage systemd. It provides a lot of options for controlling and managing services and units. Units are resources that systemd knows how to manage; these usually include services, but also other types such as mount points and sockets.

Listing services

Before managing services, it is good to know how to list them to understand the currently running services. The following commands will help:

systemctl list-units --type service --all

This command lists all services, and shows the status of each. The output will show whether each service is active, inactive, or failed.

Starting the service

To start a service that is not currently running, use the start command as follows:

sudo systemctl start <service-name>

For example, to start the Apache HTTP server, you would run:

sudo systemctl start httpd

Stopping the service

If you need to stop a service, stop command is used. This is useful in various scenarios, such as when performing maintenance or updates:

sudo systemctl stop <service-name>

Example:

sudo systemctl stop httpd

This stops the Apache HTTP Server from running.

Restarting the service

Sometimes, instead of stopping and restarting a service, you may want to restart it directly. This can be done with restart command:

sudo systemctl restart <service-name>

Example:

sudo systemctl restart httpd

This restarts the Apache HTTP server. This is especially useful when you have made changes to the configuration file and want the service to reload these changes.

Checking the status of a service

To check whether a service is running, you can use status command. By doing so, you get information about the current status of the service, any error messages, and log output:

systemctl status <service-name>

For example:

systemctl status httpd

This will display detailed information about the httpd service, showing whether it is active, enabled, or had any problems during its last startup.

Enabling and disabling services

Enabling a service ensures that it starts automatically at boot time. Conversely, disabling a service means that it will not start automatically:

sudo systemctl enable <service-name>
sudo systemctl disable <service-name>

For example, to enable the Apache HTTP Server:

sudo systemctl enable httpd

Similarly, to stop it from starting automatically:

sudo systemctl disable httpd

Enabling is particularly beneficial for services that you want to always be running when your system is on, such as web or database servers.

Masking and unmasking services

Masking a service is a powerful way to ensure that a service does not run at all. Once a service is masked, it cannot be started in any way until it is unmasked:

sudo systemctl mask <service-name>
sudo systemctl unmask <service-name>

Example:

sudo systemctl mask httpd

This command ensures that even if you try to start the Apache HTTP server, it will not start. To allow it to run again, unmask it:

sudo systemctl unmask httpd

Reloading the systemd manager configuration

If you make changes to systemd-related configuration files, you must reload the systemd manager configuration using the following:

sudo systemctl daemon-reload

This command is especially important when you add new configuration files or make changes to existing files in /etc/systemd/system or /lib/systemd/system directories.

Editing service units

Sometimes, you may need to edit service unit files to customize service behavior. With systemctl, you can do this without directly accessing the service files:

sudo systemctl edit <service-name>

This opens the unit file in your default text editor. Once you save your changes and close the editor, systemctl will create an override. Don't forget to reload the systemd manager configuration afterwards.

Conclusion

Managing services using systemctl is an essential skill for any Linux system administrator. This powerful utility provides simple commands to control services running on the system. Whether you are starting, stopping, enabling, or disabling services, systemctl simplifies these processes, increasing system reliability and performance.

It is also important to understand the levels of service management, from simple operations such as starting and stopping to complex operations such as masking or editing unit files. As you become more comfortable using systemctl, you will be able to manage services on a Linux system more effectively, which will contribute to overall system stability and efficiency.

If you find anything wrong with the article content, you can


Comments