Systemctl List Linux Services Under Systemd

Managing services is a fundamental skill for anyone who works with Linux. Systemd, the default init system for many Linux distributions, provides tools for this purpose. One of the most commonly used commands for service management is systemctl, which allows you to list, start, stop, and otherwise manage Linux services. This guide focuses on how to use systemctl to list Linux services under systemd.

Key Points to Consider

  • Systemd as Default: Most modern Linux distributions have adopted systemd as their default init system, making systemctl the standard command for service management.
  • Listing Services: The systemctl command provides various options to list Linux services, offering insights into their status, whether active, inactive, or failed.
  • Versatility: Beyond listing services, systemctl can also be used to enable, disable, start, stop, and restart services, providing a comprehensive toolset for service management.

Understanding how to list Linux services under systemd using systemctl is essential for system administration tasks and troubleshooting. The upcoming guide will offer a step-by-step walkthrough on how to effectively use systemctl for this purpose.

Systemd and Units

Systemd operates on the concept of units, which can be services, sockets, mount points, devices, and more. Units are defined using text files in the ini format. These files contain information about the unit, its settings, and commands to execute. The filename extensions define the unit file type. For instance, system service unit files have a .service extension.

The Systemctl Command

systemctl is a command-line utility used for controlling systemd and managing services. It is part of the systemd ecosystem and is available by default on all systems. To get a list of all loaded service units, you can use the following command:

sudo systemctl list-units --type service

The output of this command will contain several columns:

  • UNIT: The name of the service unit.
  • LOAD: Information about whether the unit file has been loaded into memory.
  • ACTIVE: The high-level unit file activation state, which can be active, reloading, inactive, failed, activating, or deactivating.
  • SUB: The low-level unit file activation state. The value of this field depends on the unit type.
  • DESCRIPTION: A short description of the unit file.

By default, the command lists only the loaded active units. To see loaded but inactive units too, pass the --all option:

sudo systemctl list-units --type service --all

If you want to see all installed unit files, not only the loaded ones, use:

sudo systemctl list-unit-files

Checking Service Status

To check the status of a service, use the systemctl status command:

sudo systemctl status <service_name>.service

Here, <service_name> is the name of the service unit you want to check. For example, to determine the current status of the nginx service, you would run:

sudo systemctl status nginx.service

You can omit the suffix .service. systemctl status nginx is the same as systemctl status nginx.service.

The command will print the following information:

  • Loaded: Whether the service unit has been loaded and the full path to the unit file. It also shows whether the unit is enabled to start on boot time.
  • Active: Whether the service is active and running.
  • Docs: The service documentation.
  • Process: Information about the service processes.
  • Main PID: The service PID.
  • Tasks: The number of tasks accounted for the unit and the tasks limit.
  • Memory: Information about used memory.
  • CGroup: Information about related Control Groups.

If you only want to check the service status, use the systemctl is-active command. For example, to verify that the nginx service is running, you would run:

systemctl is-active nginx.service

The command will show you the service status. If the service is active, the command returns an exit status of 0, which can be useful when using the command inside shell scripts.

Listing Services Based on Their State

The systemctl command allows you to filter the services based on their state. For instance, if you want to list all the services that are currently running, you can use the --state=running option:

systemctl --type=service --state=running

This command will display a table of information, including the service’s name, load, sub-state, and description. If you want to focus on a single service, you can pipe the output of systemctl through grep. This command isolates the table entry for the SSH service:

systemctl --type=service --state=running | grep ssh

You can also use different states to filter the services. For example, to look for failed services, you can use the --state=failed option:

systemctl --type=service --state=failed

Combinations of states can be used by typing them as a comma-separated list. Make sure you don’t include any whitespace between the options. This command will find services that match either state:

systemctl --type=service --state=failed,exited

Detailed View of a Service

To examine a service in more detail, you can use the systemctl status command followed by the service name. For example, to view the SSH daemon, sshd, in detail, you can use the following command:

systemctl status sshd

This command will display a compact view of the service, including its name, a short description, load state, how long it has been running, where the documentation is located, the process ID of the running instance, how many concurrent instances of this service are running, how much memory is being consumed, and the control group the service belongs to. Relevant entries from the system log are also shown, which can be informative if you investigate a service that didn’t launch correctly.

Understanding Unit Files

Unit files are integral to the operation of systemd. They contain the necessary information for launching services. These files are typically stored in the /usr/lib/systemd directory. However, you can also create custom unit files in the /etc/systemd/system directory.

To list all unit files on your system, you can use the list-unit-files option with the systemctl command:

systemctl list-unit-files

This command will list all unit files, their states (enabled, disabled, static, etc.), and whether they are linked to any services.

Enabling and Disabling Services

Systemd allows you to enable or disable services, determining whether they start automatically at boot time. To enable a service, use the enable command:

sudo systemctl enable <service_name>

To disable a service, use the disable command:

sudo systemctl disable <service_name>

Remember to replace <service_name> with the name of the service you want to enable or disable.

Starting and Stopping Services

You can also start or stop a service immediately without affecting its status at boot time. To start a service, use the start command:

sudo systemctl start <service_name>

To stop a service, use the stop command:

sudo systemctl stop <service_name>

Again, replace <service_name> with the service name you want to start or stop.

Checking Service Logs

Systemd comes with a logging system called the journal. It logs everything system-related, including services. To check the logs of a service, use the journalctl command:

journalctl -u <service_name>

Replace <service_name> with the name of the service whose logs you want to check. This command will display a list of all log entries related to the specified service.

Locating Systemctl Service Files

In the Linux operating system, the configuration files for systemd are stored in designated directories. These directories are categorized into System Unit directories and User Unit directories.

The System Unit directories contain the unit files for system services, while the User Unit directories hold the unit files for user-specific services. The exact location of these directories can vary depending on your Linux distribution and system configuration.

To find the location of the System Unit and User Unit directories on your system, you can use the pkg-config command with the systemd module. The pkg-config command provides metadata about installed libraries in the system, and in this case, we use it to get information about systemd.

To find the System Unit directory, run the following command:

pkg-config systemd --variable=systemdsystemunitdir

To find the User Unit directory, use this command:

pkg-config systemd --variable=systemduserunitdir

Conclusion and Final Thoughts

In this guide, we’ve delved into the essentials of using the systemctl command to list and manage services in Linux systems running systemd. We’ve explored the concept of units in systemd, how to check the status of services, and how to filter services based on their state. We’ve also discussed where systemd store their configuration files and how to locate them.

Mastering these commands and concepts is crucial for effective system administration and troubleshooting in Linux. Regular practice and application of these commands in your daily tasks will enhance your proficiency in managing services in Linux. Remember, understanding your system’s services is about listing them and knowing how to manage and troubleshoot them effectively. Keep exploring and learning, and you’ll become an authority in Linux system management.