How to List Services in Ubuntu Linux?

As an Ubuntu user, it’s important to have a good understanding of the services running on your system. This knowledge can help you manage your system effectively and troubleshoot any issues that may arise. Today, we’ll talk about learning how to list all services on this distribution.

In this article, we will explore different methods of listing services on Ubuntu system using the command line. We will start by looking at the ‘service’ command, which enables users to manage services running on the system by starting, stopping, and restarting them. We will also examine the ‘systemctl’ command, which is the primary tool for managing system services on Ubuntu or Debian. Additionally, we will discuss a combination of command-line tools that can be used to display services on Ubuntu. By the end of this article, you will have a clear understanding of how to list services running on Ubuntu using various command line tools.

Using the service command

As the name suggests, the service command lists the services as well as their status on the terminal. It allows users to start, stop, restart, and manage services running on the system. We run the following command to list them all:

service --status-all
List Services Service Command
List Services using ‘service’ command

Running the command for the first time may take a while since it collects the services from the '/etc/init.d/'directory. The symbols before the name of each service denote the status of the service. '[ + ]' represents a running service, whereas '[ - ]' means a stopped service.


1. Display only running services

It is fairly simple to extract only the services that are running using the service command, provided the user has the knowledge of the grep command. It is done by:

service --status-all | grep '\[ + \]'
List Services Running
List running services

The grep command is a Linux tool for capturing certain patterns or words from text. The pipe '|' symbol denotes the transfer of the output of one command to the input of the following command.


2. Extract only stopped services

Similarly, the above method can be used to list all the stopped services by:

service --status-all | grep '\[ - \]'
List Services Stopped
List stopped services

Listing services directly from /etc/init.d

After knowing the fact that, service command extracts services from /etc/init.d directory, we can list the contents of the directory to bypass the source code run every time for the service command.

ls /etc/init.d
List Services Ls Command
List Linux services directly from ‘/etc/init.d’

The ls command is used to list the contents of any directory on Linux. The green color for each filename denotes an executable file.

The only drawback of using this method to list services in Ubuntu is that we can not figure out the status of each service.


Using the systemctl command

systemctl stands from systemd control, which is responsible for handling system processes, system events, daemons, and booting the system. It is the primary tool used to control the systemd system and service manager.

In order to list all the available services, we run:

systemctl --type service --all
List Services Systemctl All
List all services using ‘systemctl’

systemctl provides a ton of information as compared to the service command, the reason being that systemctl is a primary command for the manager of system’s services, systemd.

Let us understand each column in the output:

  • UNITsystemd regards every resource it manages as a unit. Here, UNIT denotes a service name.\
  • LOAD – Whether the service was loaded into the memory after boot up.
  • ACTIVE – Whether the service is currently active or inactive.
  • SUB – The current state of the service.
  • DESCRIPTION – A short description of the particular service.

There can be few filters applied to the above command to fetch certain results.


1. List only the loaded and active services

In order to list only the active and loaded services, we will omit the -all option.

systemctl --type service
List Services Loaded Services
This command will list only Loaded Services

The output columns remain the same as before. Whenever we run the systemctl command, a short statistic is also presented at the end of the output.

List Services Stats
‘systemctl’ End Statistic

In the figure, we can find out the number of loaded units/services in our system.


2. List only active

To list all active running services, use the list-units option with the systemctl command, followed by the --type and --state options. We can use either of the terms for defining the state as active or running

To list only the running services, we run:

systemctl --type service --state running
List Services Systemctl Running
List only running services

Alternatively, we can also use active as the state instead of running, which will effectively give the same output

systemctl --type service --state active
Systemctl Type Service State Running
Viewing State Active

It is quite obvious that only the loaded and active services, would be running. Therefore, we can omit the --all option from the command. To find out the number of running services, we can scroll to the bottom of the output.


3. List all exited services

We can list the stopped services by:

systemctl --type service --all --state exited 
List Services Stopped Systemctl
List only stopped services

Similarly, using the active, inactive or loaded states, we can filter the list of services as per our need.


4. List all unit files

To list all unit files on our system, we can use the systemctl list-unit-files command line.

systemctl list-unit-files

This command lists all available unit files on your system and their current state, whether they are enabled or disabled.

systemctl list-unit-file
systemctl list-unit-file

5. List systemd unit files:

If you want to filter the list by unit type, you can use the --type option followed by the unit type, such as service.

systemctl --type=service list-unit-files

The command systemctl --type=service list-unit-files will list all systemd service unit files on our system.

systemctl --type=service list-unit-files
systemctl –type=service list-unit-files

6. List systemd service unit files by state (enabled/disabled)

To list only the systemd service unit files by their state (enabled or disabled), use the --state option with the systemctl command, followed by the state (enabled or disabled) you want to filter. The command syntax for enabled is as follows:

systemctl --type=service --state=enabled list-unit-files

The command syntax for disabled is as follows:

systemctl --type=service --state=disabled list-unit-files

7. List all dead services

To list all dead services, use the list-units option with the systemctl command, followed by the --type and --state options. The command syntax is as follows:

systemctl --type=service --state=dead list-units

This command will display a list of all dead services in the system. Dead services are services that have been stopped or terminated due to an error or other reasons.

List Services on Ubuntu Using a combination of command line tools

To display services on your Ubuntu system, you can employ several command-line techniques. Here are two distinct methods to achieve this:

1. List unit files using grep command

One way to display services is by using systemctl to show all existing unit files, then filtering the results with the grep command.

systemctl list-unit-files | grep <search-term>

Here’s an example, to display all unit files containing the word “apache” in their name, execute the following command

systemctl list-unit-files | grep apache

This will present all the unit files with “apache” in their name, such as apache2.service or apache2-ssl.service.

Show Unit Files Using Grep Command
Show Unit Files with ‘apache’ name Using Grep Command

2. List systemd service unit filesall service-type units without pager and filter for dead units

  • Another way to list services is by applying the systemctl command to show all “service” type units, then using the –no-pager option to disable the pager and filtering the results with the grep command to only display units in a “dead” state.
systemctl --no-pager list-units --type=service | grep dead

This will present all the service units in a “dead” state, such as cron.service or systemd-timesyncd.service.

Conclusion

In conclusion, the service and systemctl commands are powerful tools for listing and managing services on Ubuntu. While the service command provides a basic list of services, systemctl provides more detailed information, including the status of each service. By applying filters to the systemctl command, users can list only loaded, active, running, stopped, or dead services, depending on their needs. Additionally, the combination of command-line tools, such as systemctl and grep, can provide even more specific results, making it easier for users to manage their system’s services.

We hope this article provided enough information related to the listing of services in Ubuntu or other Linux distributions. Feel free to comment below if we missed any method.