Linux systemctl
is a command-line utility that is used to control and manage system services in Linux operating systems. It is a powerful tool that allows you to start, stop, restart, and check the status of services, as well as configure and manage them. In this blog post, we’ll take a deep dive into systemctl and explore its various features and options.
Starting and Stopping Services
One of the most common tasks that you’ll use systemctl for is starting and stopping services. The syntax for starting a service is:
systemctl start <service_name>
For example, to start the Apache web server, you would run:
systemctl start apache2
Stopping a service is just as simple. The syntax is:
systemctl stop <service_name>
For example, to stop the Apache web server, you would run:
systemctl stop apache2
Restarting Services
Sometimes, you may need to restart a service to apply changes or fix an issue. The syntax for restarting a service is:
systemctl restart <service_name>
For example, to restart the Apache web server, you would run:
systemctl restart apache2
Checking the Status of Services
It’s also important to be able to check the status of a service to see if it’s running or not. The syntax for checking the status of a service is:
systemctl status <service_name>
For example, to check the status of the Apache web server, you would run:
systemctl status apache2
Enabling and Disabling Services
Another useful feature of systemctl is the ability to enable and disable services. When a service is enabled, it will automatically start at boot. The syntax for enabling a service is:
systemctl enable <service_name>
For example, to enable the Apache web server, you would run:
systemctl enable apache2
Disabling a service is the opposite of enabling it. The syntax for disabling a service is:
systemctl disable <service_name>
For example, to disable the Apache web server, you would run:
systemctl disable apache2
Listing Services
You can also list all of the services on your system with systemctl. The syntax for listing services is:
systemctl list-units --type=service
This will return a list of all services on the system, along with their current status.
Reloading Services
Sometimes, you may need to reload a service without stopping and starting it again. This can be useful when a service has updated configuration files that need to be reloaded without interrupting its operation. The syntax for reloading a service is:
systemctl reload <service_name>
For example, to reload the Apache web server, you would run:
systemctl reload apache2
Managing Multiple Services
systemctl also allows you to manage multiple services at once. You can use wildcard characters to specify multiple services at once. For example, to stop all Apache services, you would run:
systemctl stop apache2*
This command would stop all services that start with “apache2”.
You can also use the –all flag to apply a command to all services. For example, to restart all services, you would run:
systemctl restart --all
Flags
systemctl also has a number of advanced options and features for more experienced users. For example, you can use the –quiet flag to suppress output messages when running a command. You can also use the –no-block flag to run a command in the background, allowing you to continue working in the terminal while the command is running.
You can also use systemctl to troubleshoot services by viewing their log files. The syntax for viewing a service’s log files is:
systemctl status <service_name> -l
This will show you the service’s status as well as its log files.
Remotely
Another advanced feature is the ability to use systemctl to manage services on remote systems. To do this, you will need to use the –host flag followed by the hostname or IP address of the remote system. For example, to check the status of the Apache service on a remote system with IP address 192.168.1.100, you would run:
systemctl --host=192.168.1.100 status apache2