Introduction
Using the systemctl
command, Linux provides fine-grained control over system services through systemd. Services can be turned on, off, restarted, reloaded, as well as enabled or disabled at boot.
This guide will show you how to start, stop, and restart services in Linux.
Prerequisites
- Access to a user account with sudo or root privileges.
- Access to the terminal.
- A Linux system (this tutorial uses Ubuntu 22.04).
What Is systemctl?
systemctl
is an essential command-line tool for system administrators and users on Linux systems utilizing systemd, the default init system for many Linux distributions.
systemctl
provides an efficient way to manage Linux services. Services in Linux are background processes crucial for the proper functioning of the operating system and different applications.
The systemctl
syntax is:
systemctl [options] [command] [unit]
The syntax consists of:
systemctl
. The command that interacts with systemd and manages services.[options]
. Optional arguments that modify thesystemctl
command behavior.[command]
. The action to be performed on the specified units.[unit]
. Units to be acted upon, like services, sockets, targets, etc. If no units are specified, the command applies to all units of the specified type.
systemctl vs. service
systemctl
and service
are command-line tools used to manage services in Linux. However, they are associated with different init systems. Init is the initialization process, the first process that starts when the computer boots up. It is responsible for initializing the system and starting other processes and services.
While systemctl
is used to manage Linux services that use systemd as their init system, service
is a legacy command-line tool used to manage services in Linux distributions that use traditional init systems like SysVinit or Upstart.
systemctl
provides more advanced features than the traditional service
command. However, both commands are available on modern Linux distributions for compatibility reasons.
How to Start a Service
To start a service in Linux, type in the following:
sudo systemctl start [service-name]
For instance, the command to start the Apache service is:
sudo systemctl start apache2
The command has no output. Verify the service is active with:
sudo systemctl status apache2
The output shows the service is active (running).
To configure a service to start when the system boots, use the command:
sudo systemctl enable [service-name]
For example, enable Apache to start with the system boot with:
sudo systemctl enable apache2
Note: Different Linux distributions sometimes have different names for one service.
How to Stop a Service
To stop an active service in Linux, use the following command:
sudo systemctl stop [service-name]
For example, to stop Apache, execute:
sudo systemctl stop apache2
The command has no output. Check whether the service stopped running:
sudo systemctl status apache2
The output shows the service is inactive (dead).
Another way to ensure the service is not active is to prevent it from starting on boot. Use the command:
sudo systemctl disable [service-name]
For example:
sudo systemctl disable apache2
How to Restart a Service
To restart the service in Linux, use the command:
sudo systemctl restart [service-name]
To restart Apache, use:
sudo systemctl restart apache2
The command has no output. Verify the status with:
sudo systemctl status apache2
How to Reload a Service
Restarting a service involves stopping it completely and then starting it again. On the other hand, reloading a service reloads its configuration files and applies any changes without stopping the service.
To reload a service, run:
sudo systemctl reload [service-name]
For instance, reload Apache with:
sudo systemctl reload apache2
The command has no output. Verify the service is running with:
sudo systemctl status apache2
The output shows the service is active and running.
Conclusion
After reading this article, you have learned about systemctl. You also know how to start, stop, restart, and reload a service in Linux.
Next, learn about important Linux commands.