As a Linux administrator, we often need to start, stop, reload and do other actions to system services. For those new to Linux, here’s the how to tutorial for Ubuntu 20.04 Server or Desktop.
What does system services do:
System services are the software processes running silently in the background. They do various jobs, such as media streaming, web serving, and remote network login in the host server without graphical interface. However, they serve remote clients with web pages (using e.g., Apache, Nginx), online YouTube videos and or Spotify musics.
Start, Stop, or Restart Services in Ubuntu Linux:
Since Ubuntu 18.04, systemd daemon takes the place of
init.d to act as system and service manager. And other Linux Distributions, e.g, Debian, CentOS and Fedora, they all use systemd as system service manager.
Manage services via systemctl command:
Systemctl is the command line tool to control the systemd service manager. It provides following commands to do commonly actions.
NOTE: In the commands below, replace <Service_Name> with your serivce, e.g., SSH, apache, php, etc. "sudo" is required if you're working on Ubuntu Server with non-root user.
1. To start a system service:
sudo systemctl start <Service_Name>
2. To stop a service:
sudo systemctl stop <Service_Name>
3. To reload a service after changing its configuration:
sudo systemctl reload <Service_Name>
4. To restart a service (will start service if not yet running):
sudo systemctl restart <Service_Name>
5. And check the service status:
All the previous commands mostly do not output anything, so you may check the service status by running command:
sudo systemctl status <Service_Name>
To quit the command, press Ctrl+C on keyboard
As you see, the command is just “systemctl <Action> <Service_Name>”. Where “<Action>” is usually start, stop, reload, restart, and status.
There are more actions, such as try-restart will restart service only when it’s already running; reload-or-restart will reload if supported or restart (start if not yet running); try-reload-or-restart will reload if supported or restart (do nothing if not yet running); and kill, clean, is-action, is-failed and more.
Manage services via service command:
service is an “high-level” command to manage system services. It redirects to
systemctl in recent Linux systems.
This usage is a little bit different. It places service place in the first and then the actions.
service <Service_Name> <Action>
So, we can start, stop, and restart services (SSH for instance) via
service ssh start,
service ssh stop,
service ssh restart, and
service ssh status. Also, sudo is required for non-root user.
In short, Ubuntu and other Linux server may manage system services via
systemctl command: “
systemctl <Action> <Service_Name>“. By replacing <Action> with start, restart, stop, reload or status will do relevant action to the specified service.