Services are essentially programs running in the background outside the interactive control of the system users as they lack an interface. They run in the background to be used whenever they are needed.
Some of the commonly known services include Mysql, ssh, and Nginx. On Debian, services are managed with Systemd, which replaces System V to initialize the system and its components, including services that are key for the proper functioning of the operating system.
Some of the services in Debian and Ubuntu-based versions that run on every boot are start, restart, stop, and reload. This tutorial will cover all of them in detail.
Systemd vs. Init
Linux-based distro nowadays uses systemd instead of the good old init. In systemd, you can manage services using the systemctl command, while in init, you manage the service using the service commands.
You will also notice that even though your Linux system, in this case, Debian, uses systemd, it still has the flexibility of utilizing the service command intended for init systems. This is so because the service commands redirect to systemctl. Say it is backward compatibility introduced by systemd because sysadmins were habitual using the service command.
Start, stop, and restart services on Debian 11
Don’t worry about any managing services since this article will cover both the systemctl and service commands. Without further ado, let us get going.
Method 1: Using Systemctl
The systemctl command is a systemd daemon suite used to manage Linux services and daemons represented by the last “d” in the name systemd. This “d” came out because of the Unix daemons designed to be backward compatible with SysV init scripts and offer several features like the parallel startup of system services at boot time, on-demand activation of daemons, or dependency-based services control logic. Its objective is to unify all Linux-based distros’ configuration and general behaviors, replacing the old Unix SystemV and BSD init systems.
1. Listing all the services
Before looking at how to start, stop and restart services on our Debian machine using the systemctl command, we first need to know the services available on your system. As such, we will utilize the list command to list all the services in our Linux system:
systemctl list-unit-files --type service -all
As seen, the above command outputs the type of service and its state. The value of a service state can either be enabled, masked(inactive until mask is unset), disabled, generated, or static.
To only output running services, you can combine the systemctl command with the grep command, and you should be good to go.
sudo systemctl | grep running
The systemctl command is used to start, check status, stop, and restart services following the syntaxes in the following table:
|Check service status
|systemctl status (ServiceName)
|Start a service
|systemctl start (ServiceName)
|Stop a service
|sudo systemctl stop (ServiceName)
|Restart a service
|sudo systemctl restart(ServiceName)
2. Start a service
To start a service using the systemctl command, utilize the start option. This option is then followed by the name of the service you wish to start. As such, make use of the following syntax:
systemctl start (ServiceName)
After that, you will replace the (ServiceName) with the actual service you want to start, like in our case, we want to start the vsftpd service.
sudo systemctl start vsftpd.service
As seen above, the service is up and running.
3. Status of a service
As earlier seen in the above table, the syntax to check the status of the service is as follows:
systemctl status (ServiceName)
Therefore, we will utilize the syntax while replacing the (ServiceName) with appropriate values. To check the vsftpd service status, you can issue the following command:
sudo systemctl status vsftpd.service
In the snapshot below, you should see that the ssh service is active and properly functioning
4. Stopping a service
Now, let us discuss how to stop service. To do so, we will use the stop option of the systemctl command that is followed by the name of the service you want to stop. stopping a service follows the following syntax:
sudo systemctl stop (ServiceName)
For instance, to stop the currently running vsftpd service, run the following command:
sudo systemctl stop vsftpd.service
After running the stop command, you should see that the previously active button has switched to inactive(dead).
5. Restart a service
To restart a service using the systemctl command, we will utilize the “restart” option. Akin to the previous examples, this option is also followed by the name of the service you wish to restart, as shown in the following syntax:
sudo systemctl restart (ServiceName)
Then replace the (ServiceName) with the actual service you want to restart, in our case, “vsftpd.”
sudo systemctl restart vsftpd.service
And that is how services are restarted using Systemd.
You can visit this link to get more info on Systemd.
Method 2: Using the service command
The service command manages services under the /etc/init.d directory, although some distributions redirect the command to the earlier explained systemctl command. The service command is part of the init daemon commonly used in earlier distros of Linux systems to manage services.
Just like systemctl, the service command can also be used to check status, start, restart, and stop services following the syntax described in the table below:
|Restart a service
|systemctl (ServiceName) start
|Check service status
|sudo service (ServiceName) status
|Start a service
|sudo service (ServiceName) start
|Stop a service
|sudo service (ServiceName) stop
The following section displays examples of how to apply the syntaxes in the above table
1. Start service
Now let us discuss how to start a service. To attain this, we will use the start option of the service command. This option comes after the name of the service you want to execute. Here is the syntax:
sudo service (ServiceName) start
For instance, if we’re going to start a service called ssh, we will replace (ServiceName) with the actual service intended to start, like in our case, “vsftpd.”
sudo service vsftpd start
2. Check the status of a service
Make use of the following syntax to check the status of a service:
sudo service (ServiceName) status
If, for instance, we want to run a status check of a service like vsftpd using the service command, we will have to replace the syntax (ServiceName) with the name vsftpd as shown below:
sudo service vsftpd status
3. Stopping a service
Now let’s see how to stop service. To do this, we will use the stop option preceded by the name of the service we wish to stop. Here is the syntax:
sudo service (ServiceName) stop
After that, replace the (ServiceName) with the actual service you want to stop like, in our case, “vsftpd”:
sudo service vsftpd stop
As seen, the service now reads Inactive(dead) after running the stop command, meaning it is functioning as intended.
4. Restart a service
Let us try out the restart services using the service command that follows the following syntax:
sudo service (ServiceName) restart
sudo service vsftpd restart
As seen, the service commands function correctly as we wanted.
Additionally, you can utilize the following command to reload a service configuration without interrupting the service itself.
sudo service vsftpd reload
You can visit this link to get more insight into the service command.
How to enable a service at boot
In case you want to enable a Debian service to fire up when the system boots, then make use of the following syntax:
sudo systemctl enable SERVICE_NAME
sudo systemctl enable vsftpd.service
How to disable service at boot
You can also halt service from starting up during a system boot using the following syntax:
sudo systemctl disable SERVICE_NAME
sudo systemctl disable vsftpd.service
Services are vital components of any device; hence managing them is a must for any Linux level user. This guide shows that managing services is pretty straightforward and can be done via different methods. Be keen to remember that the ideal and recommended method to carry out the service’s task in Linux is the systemctl command that we earlier explained in the first section of this guide. Other methods like dealing with the /etc/init.d directory are obsolete.
This tutorial has explained the functionality of start, restart, stop and check the service status, but that does not limit you from learning additional functions from their official pages. Keep following FOSS Linux for more Linux-related guides.