
How to Start, Stop, and Restart Services on Debian
In Debian and other Linux systems, services are the building blocks of the processes running on your system. These services determine the state of the operation dependent on that service. For instance, the ssh service is responsible for all the operations related to the SSH server on your system. Similarly, the ufw service provides the backend support of the firewall on your Debian/Linux system.
These services can be started, stopped, and restarted to manage the services. Today’s post will dig into all the methods/commands that can be used to stop/start/restart services on Debian.
Note: All the commands demonstrated in this guide are practiced on Debian 11 and as a root user. If you are not a root user, then you must use the sudo keyword to execute commands.
How to Start, Stop, and Restart Services on Debian
Debian offers the service and the systemctl commands to manage most of the services on the system. However, the “/etc/init.d” script redirects the start/stop/restart operations to the specific command that refers to your init daemon.
A Linux system has an init daemon that directs which command to use for service management. The two popular init daemons are “init” and “systemd”. The “service” command refers to managing the services on a system equipped with the “init” daemon. At the same time, the systemctl refers to the systems having systemd as their daemon.
To check which init your system is equipped with, use the command:
$ stat /sbin/init |
---|
The yellow highlighted part shows that the “systemd” is the init system.
Method 1: Use the systemctl Command to Start, Stop, and Restart Services
The systemctl refers to having the “systemd” as its init daemons. The NetworkManager service will be used to practice the systemctl command.
Syntax of the systemctl Command:
$ sudo systemctl start/stop/restart <service-name> |
---|
Inserreplace the service name with the “<service-name>” in the above syntax alongside a flag (start/stop/restart).
Start a Service Using systemctl Command
The NetworkManager is started to start the service if the internet or the network connections are not responding effectively:
# systemctl start NetworkManager |
---|
Stop a Service Using systemctl Command
Similarly, if you want to stop the internet connection/ network connections, use the command:
# systemctl stop NetworkManager |
---|
Restart a Service Using systemctl Command
If the internet/network connection is not working properly, you must restart the NetworkManager. To restart the service, use the command as follows:
# systemctl restart NetworkManager |
---|
Check the Status of the Service Using systemctl Command
The status of the service can be obtained using the command provided below. It is recommended to make use of the status flag of the service command to check the status of the command:
# systemctl status NetworkManager |
---|
Method 2: Use the service Command to Start, Stop, and Restart Services
As discussed, the service command refers to the systems having init as their init daemon. In this section, we will use the NetworkManager service to demonstrate the working of the service command.
Syntax of the service Command:
$ sudo service <service-name> start/stop/restart |
---|
Put the service name in place of “<service-name>” and use the appropriate flag (start/stop/restart) to perform a specific operation on that service.
Start a Service Using service Command
The command below will start the NetworkManager’s service:
# service NetworkManager start |
---|
Stop a Service Using service Command
Use the stop flag with the NetworkManager’s service to stop the service as follows:
# service NetworkManager stop |
---|
The stop flag has inactivated the NetworkManager’s service.
Restart a Service Using service Command
The restart flag activates a dead service or refreshes the service, as the NetworkManager’s service in our case. The command to restart the NetworkManager’s service using the service command is as follows:
# service NetworkManager restart |
---|
Check the Status of the Service Using service Command
Check the status of the service to ensure that it has been activated or not:
# service NetworkManager status |
---|
Method 3: Use the /etc/init.d Script to Start, Stop, and Restart Services
The /etc/init.d script does follow a syntax to manage the services on the Debian system. This script redirects the command to the command that your init system depends on, i.e., systemctl. In this section, the cron service is taken as an example to show the working of the /etc/init.d script. The cron service manages the display manager’s related concerns:
Syntax of the /etc/init.d Script:
$ sudo /etc/init.d/<service-name> start/stop/restart |
---|
After the last forward slash, you must put the service name followed by an operation (start/stop/restart).
Start a Service Using /etc/init.d Script
The following command will restart the cron service:
# /etc/init.d/cron start |
---|
Stop a Service Using /etc/init.d Script
Stop the cron service via the command:
# /etc/init.d/cron stop |
---|
Restart a Service Using /etc/init.d Script
To restart any service using the “/etc/init.d” script, use the command:
# /etc/init.d/cron restart |
---|
Check the Status of the Service Using /etc/init.d Script
To check the service status using the “/etc/init.d/” script, use the command:
# /etc/init.d/cron status |
---|
Wrap Up
Debian offers the service and the systemctl commands alongside the “/etc/init.d/<service-name>” to start, stop, and restart the services. The service command uses the syntax “sudo systemctl start/stop/restart <service>” to manage the services, whereas the systemctl command practices the syntax “sudo service <service> start/stop/restart.” Similarly, the /etc/init.d/” script follows the syntax “/etc/init.d/<service-name>” to manage the services.
You have learned the potential methods to start, stop, and restart services on Debian. Interestingly, these commands also apply to other Debian/Debian-based Linux distributions. For more informative How-Tos guides on Linux/Debian, visit Linux Genie and subscribe to our mailing list for daily updates.