How To - Restart a Linux Service Three Different Ways Using the Command Line
There are many ways to restart a service, depending on the flavour and version of Linux you are using. All three of these commands work in the latest version of Ubuntu at the time of writing this post. (Ver 24.04)
The three different commands are:
sudo service ServiceName restart
sudo /etc/init.d/service restart
sudo systemctl restart ServiceName
The service names of numbers 1 and 2 match, even though #2 is most likely using #3 behind the scenes.
Example: Restart Docker
sudo service docker restart
sudo /etc/init.d/docker restart
sudo systemctl restart docker
There are a few things I would like for you to notice with these commands. When you type sudo /etc/init.d/docker restart dIt shows it is using systemctl command, and the REAL service name is “docker.service”.
How do you find out what the full name of the service is?
Well, if you use the “service” command and type:
service --status-all
You will notice that the docker service is just called “docker”
[ + ] apparmor
[ + ] apport
[ - ] console-setup.sh
[ + ] cron
[ - ] cryptdisks
[ - ] cryptdisks-early
[ + ] dbus
[ + ] ddclient
[ + ] docker
[ + ] fail2ban
The rest of the services are removed for ease of reading.
If we go to “/etc/init.d/” and type “ls”, we can also see that the service for docker is “docker”.
So why does #2 say the service name is: “docker.service“
If you type this command and hold down the enter key:
systemctl
You will see that systemctl sees the docker service name as “docker.service”.
Why does #3 work without adding the “.service” at the end of the command?
This is due to the efficiencies within the command. systemctl will actually add “.service” to the command for you in the background.
I have read that some people say systemctl will auto-complete the command because there are no other services with a similar name. This is NOT true.
To prove this, if we type these commands they will NOT work
sudo systemctl restart do
sudo systemctl restart docker.ser
However, if I type:
sudo systemctl restart docker
sudo systemctl restart docker.service
both of these commands will work.