systemctl is a powerful service management tool under systemd for Linux distributions. By services, we mean a resource or unit, such as a software application that runs in the background without your intervention. These are also called daemons.
In this tutorial, we’ll learn how you can see any service, whether active, enabled, or loaded, using the systemctl command.
For demonstration purposes, we’ll be using Ubuntu 22.04. However, this will work on almost any modern Linux distribution.
Let’s get started!
Check If You’re Using systemd
The first thing you should do is check whether you’re using the systemd system manager or not. So, if you use a different Linux distribution in the future, you can know what system manager is being used before you use the systemctl command.
To see which system and service management system you’re using, use this command:
pstree | head -n 5
The pstree command displays all the system processes in a tree-like format. Since we’re only interested in seeing the system manager, we’re limiting it to only the first five lines here. As you can see, we have a systemd.
List All Services Using systemctl
The simplest way to list all services, no matter in what condition, is to run the below command:
systemctl list-units --type=service --all
As you can notice from the above screenshot, every single service on your system is being shown here. There are different categories, such as loaded, active, running, exited, etc. Adding the all flag to the command allows you to view every service on your system. This is convenient if you need to go through all existing services at once.
When you’re finished seeing the list, press the Q button to exit from that window.
However, sometimes, finding services of a particular category from this list will be time-consuming and cumbersome. You can also list services that meet a single or multiple criteria. For example, you can list only the enabled services. We’ll show you how to do that right now.
List Loaded Services Using systemctl
Loaded services are loaded into memory and running. These services may be loaded by a user manually or automatically during boot if configured that way.
To list only the loaded services, run this command:
systemctl list-units --type=service
You can also list services based on their current state. Let’s see how you can do that.
List Running Services Using systemctl
If you only want to see which services are currently running, then use this command:
systemctl list-units --type=service --state=running
These services are all loaded and running, as you can see under the LOAD and SUB columns.
List Enabled Services Using systemctl
Enabled services are configured so that they start functioning immediately after you boot up your system. You don’t need to start them whenever you start your device manually. To see the list of enabled services, run the following command:
systemctl list-unit-files --state=enabled
These services are all in the enabled state.
List Disabled Services Using systemctl
Similar to enabled services, you can view the list of disabled services, too. These services require the user to start themselves each time. To view these services, use this command:
systemctl list-unit-files --state=disabled
You can notice that all the services listed are disabled, as seen in the STATE column.
List Services with Other Sub-states
In the same way, you can view services that have a different state than the ones we’ve mentioned. For example, to list failed services, run this command:
systemctl --type=service --state=failed
Since there are no failed services in our case, the list is empty.
To check services with an exited state, use the below command:
systemctl --type=service --state=exited
These are services that were started, finished their execution, and are no longer running.
Let’s look at another example: masked services. Masked services are forcefully prevented from being started. To list masked services, run the below command:
systemctl list-unit-files --type=service --state=masked
This command also states the total number of masked unit files, which is 10 in our case.
List Services of Multiple Sub-states
What if you want to see services that are both failed and exited? Or perhaps any other combination of sub-states? You can do that by specifying all the states separated by a comma. See the below example code:
systemctl --type=service --state=failed,exited
In this method, you can view services having different combinations of states.
List Only One Service at a Time
So far, you’ve seen how to display a whole list of services that meet certain criteria. However, you can also display only one service and look at its details. To do so, push the service name as the parameter. Follow the format of this command:
systemctl status openvpn.service
As you may notice from the above screenshot, this method allows you to see a short description of a single service. You can see if the service is loaded or not, including the file path, its active status, PID, etc.
Check if a Service is Active or Enabled
If you want to know if a particular service is active or enabled without showing all the details, you can do that, too.
To see if a service is active or enabled, you need to input that service name after a special command like this:
systemctl is-active <service> systemctl is-enabled <service>
So, let’s see some examples to understand the commands further.
systemctl is-active openvpn.service systemctl is-enabled openvpn.service
From our example, the particular service we chose is active and enabled, as seen in the above picture.
This tutorial shows you different ways to list services and daemons of your Linux system using the systemctl command. With this new knowledge, you can better manage Linux services and your system resources. You can also troubleshoot issues or optimize system performance with systemctl.