This tutorial explains how to use the systemctl command to stop, start, load, reload, enable, disable, and restart a systemd service. Learn how to manage systemd services from the systemctl command.
Understanding control options/actions
A systemd service can be controlled by six types of actions. These actions are the start, stop, restart, reload, enable and disable. These actions can be divided into two types; boot-time actions and run-time actions.
Boot-time actions are the enable and disable. These actions are used to control a service at boot time.
Run-time actions are the start, stop, restart, and reload. These actions are used to control a service after the booting process in the current session.
Both types of actions are independent. They affect only the states to which they belong. It means, if a service is enabled at the boot-time, no matter whether you keep it running after the booting process or stop it, it will be started in the next booting process automatically. Vice versa, if a service is disabled at the boot-time, no matter whether you start it after the booting process or not, it will be stopped in the next booting process automatically.
A service always starts under a unique ID. This ID is known as PID. The PID is used to track, monitor and control the service.
When a service stops, its PID is also destroyed. The PID of a stopped service is never used to start that service again. If you start that service again, that service will use a new unique PID.
In the restart action, the running service is stopped completely and then the service is started again under the new PID. In simple words, a restart action is the combination of the stop and start actions.
In the reload action, only the unit configuration file of the service is reprocessed. Since the running service is not stopped, the PID of the service does not change.
Any changes made through the start, stop, restart and reload actions do not persistent after the system reboot. To make a change permanent, you must have to perform the related boot-time action. For example, if you want to permanently stop a running service that is enabled at the boot time then you must have to disable that service at boot time as well.
The systemctl command
The systemctl command is the primary command to manage systemd services. Using the systemctl command is relatively simple. You only need to supply the action and the name of the service to this command in the following way.
#systemctl [action] [name of service]
The following table lists commands to perform all above explained actions/operations.
Action/operation | Command |
Start | #systemctl start [name-of-service].service |
Stop | #systemctl stop [name-of-service].service |
Restart | #systemctl restart [name-of-service].service |
Reload | #systemctl reload [name-of-service].service |
Enable | #systemctl enable [name-of-service].service |
Disable | #systemctl disable [name-of-service].service |
Before we take practical examples of the above commands, let’s understand how to check the run-time and boot-time status of a service.
To check the current status of a service, we can use the following command.
Command | Description |
#systemctl status [name-of-service].service | Detailed status of the service including both types; run-time and boot-time |
#systemctl is-active [name-of-service].service | Display only run-time status of the command |
#systemctl is-enabled [name-of-service].service | Display only boot-time status of the command |
This tutorial is the fifth part of the article "Systemd in Linux Explained with Examples". Other parts of this article are the following.
Differences between SysVinit, Upstart and Systemd Systemd Units Explained with Types and States Systemd Unit Configuration Files Explained Systemd Target Units ExplainedPractical examples
The following command displays the full/detailed status of the sshd.service.
#systemctl status sshd.service
The following image shows the output of this command.
The following command displays the boot-time status of the sshd.service.
#systemctl is-enabled sshd.service
The following image shows the output of the above command.
The following command displays the run-time status of the sshd.service.
#systemctl is-active sshd.service
The following image shows the output of the above command before and after activating and deactivating the sshd.service.
The following command stops the sshd.service.
#systemctl stop sshd.service
The following image shows the output of the above command.
The following command starts the sshd.service again.
#systemctl start sshd.service
The following image shows the output of this command.
The following command restarts the sshd.service.
#systemctl restart sshd.service
The following image shows the output of this command.
As you can see in the above output, when we restart a service, the PID of the service changes.
The following command reloads the sshd.service.
#systemctl reload sshd.service
The following image shows the output of this command.
As you can see in the above output, when we reload a service, the PID of the service does not change.
The following command enables the sshd.service at the boot-time.
#systemctl enable sshd.service
The following image shows the output of the above command.
The following command disables the sshd.service at the boot-time.
#systemctl disable sshd.service
The following image shows the output of the above command.
That’s all for this tutorial. If you like this tutorial, please don’t forget to share it with friends through your favorite social network.