Unix-based operating systems give you flexible tools to manage your OS and its related services smoothly. And it’s our responsibilities as administrators to monitor our systems and manage it and of course, keep it running by troubleshooting the problems that occur on our system services. So, in this post you will learn how to use systemctl on Linux.
What is systemctl?
The systemctl command is a utility which is responsible for examining and controlling the systemd system and service manager. The systemctl command is a new tool to control the systemd system and service. This is the replacement of old SysV init system management. Most of modern Linux operating systems are using this new tool. If you are working with CentOS 7, Ubuntu 16.04 or later or Debian 9 system. They have opted systemd now.
Keep in mind that most of the systemctl commands will not provide any output if they have been successful. However, if the systemctl command has failed to complete the task, you will get an error message stating that it failed.
What is a Service?
In the systemd utility, a service is referred to as a unit. A unit is any resource that the system knows how to act on and administrate. Hence, a unit is the principal object that the systemd tools know how to address. These assets are defined in a configuration file called a unit file.
How to use SYSTEMCTL
Since we discussed what a service is, thus you can check the state of any service as the following
Here we can notice that, The dot (“●“) uses color on supported terminals to summarize the unit state at a glance. White color indicates an “inactive” or “deactivating” state. Red color indicates a “failed” or “error” state. Green indicates an “active”, “reloading” or “activating” state.
And here’s some of the most important service status:
active (running): Service or daemon is running in the background
active (exited): Service successfully started from the config file. Typically one time services configuration read
inactive: Service is not running
enabled: Service is enabled at boot time
disabled: Service is disabled and will not start at Linux server boot time
To stop a service with systemctl
To stop a service doesn’t mean it will be off when the OS boots up, but it means it will be off while the system is up
As we can see, super privilege is a must for this action
To check the results, show the status again
Notice that the status still enabled although it’s inactive now
To start a service
Again, start a service means it will be active in the current boot only
Disable a service
Disable a service will make it not start at the boot of the kernel even it stays active before reboot
Notice that it’s active and yet disabled
Enable a service
Now, to enable a service which will make it start at the boot of the kernel regardless its current state (active, inactive)
Restart or Reload a service
Sometimes it’s recommended to restart the service if it hangs and this is a part of a troubleshooting
This will stop the service then start it agin
Occasionally, we do not need to restart a service to apply configuration changes. Instead, we can use the reload command to restart the service which implements any changes to the running service.
To check whether a service in enabled more or not
Similarly, you can check some other options
Server states
Finally, we can change the state of the server using the systemctl utility as it’s an advanced from init utility
To completely poweroff the server, use the following command
sudo systemctl poweroff
Reboot the server by using this command
sudo systemctl reboot
Conclusion
Managing your system as well as troubleshooting it is a high priority in the administration process.
Systemctl is a powerful utility that is easy to use with its flexibility and variant options.
To get more information about its options and capabilities, man up the command and see the official documentation.