How to View and Manage Systemd Services in Linux

Most modern Linux distributions, such as Ubuntu, Debian, CentOS, Rocky Linux, and Fedora, use the service manager systemd by default. It plays a crucial role in setting up the system upon booting and managing background services that maintain the system’s functionality. These services can be anything from a web server, a database server, networking components, a logging service, and many other important processes.

In this tutorial, we will explain how to list systemd services in Linux through different methods. Furthermore, you will find the commands that help you to list running services, check a service status, start and stop services in Linux, enable or disable them at boot time, as well as how to troubleshoot systemd services.

Understanding Systemd Services

In Linux, a service is a background program that executes a task without user intervention. They are also services that run indefinitely and contribute valuable functionality to the system. Common systemd services are: SSH service for remote connections, Web server like Apache or Nginx services, MySQL or PostgreSQL database services, and Networking and firewall services.

Unit files tell systemd how to start, stop, and what to do with services while the system is running. Each systemd service generally corresponds to a file ending with service extensions found inside catalog paths like: “/etc/systemd/system” and “/usr/lib/systemd/system”.

Getting a clear view of these services and controlling them helps administrators maintain system stability and performance.

How to View and Manage Systemd Services in Linux

One of the things you need to know as a Linux system administrator is how to check the status and manage systemd services. Depending on system requirements, administrators often have to start, stop, restart, enable, or disable services. For instance, if you install a web server, you would have to manually start the service and configure it for automatic execution at boot time.

Systemd uses the systemctl command as the tool to interact with it. This enables administrators to manage services, query the state of services, view logs, and control whether or not a service starts on boot.

View All Systemd Services

Viewing the services on a system is one of the first things you can do when managing services. The systemctl command offers multiple options for listing systemd services.

View All Active Services

To list all the active services on your system, use the following command.

systemctl list-units --type=service
View All Active Services

This command shows all currently loaded (active) service units in memory. It prints out critical information, including the service name, whether it is loaded or not, its state (active, inactive, etc.), and a brief description. The first column, LOAD, shows the service configuration loaded successfully, and ACTIVE indicates if it is running or not.

View All Services, Including Inactive Ones

Reading state-of-the-art services on the system occasionally requires administrators to view all services available in the system, including those that are not running. To show all the service unit files, run the command below:

systemctl list-unit-files --type=service
View All Services, Including Inactive Ones

This command will display all services where they are in the state of enabled, disabled, or static. It helps determine what services are set to start by themselves at boot.

Monitor the State of a Systemd Service

When troubleshooting a service or verifying whether it is running, administrators often need to check its current status.

Get the Status of a Specific Service

You can replace service-name with an actual service and run to see details about the particular service.

systemctl status service-name

For instance, to check the Apache2 service status, use the command below:

systemctl status apache2.service
Get the Status of a Specific Service

The output will show a couple of key details, such as: status of the service, the main process ID, logs recently related to the service, and the service startup time. This command is one of the most frequently used systemctl commands to troubleshoot services.

Starting and Stopping Systemd Services

Based on system requirements, administrators often need to start or stop services.

Start a Service

If you have a service installed but not running, you can run it manually with the command below.

sudo systemctl start service-name

To start the Apache web server service, run the following command:

sudo systemctl start apache2
Start a Service

Once you run that command, the service will just start running.

Stop a Service

To stop a service that is running, you can use the command below:

sudo systemctl stop service-name

To stop the Apache web server service, execute the command below:

sudo systemctl stop apache2
Stop a Service

The command kills a service by killing off its processes.

Restart and Reload Systemd Services

Reloading or restarting services is common practice when applying configuration changes.

Restart a Service

You can follow the command to run when configuration updates need a full service restart.

sudo systemctl restart service-name

For instance, to restart the Apache web server service, execute the command below:

sudo systemctl restart apache2
Restart a Service

Restart stops the service and then starts it again.

Reload a Service

Some services support reloading, where the changes can take effect without a full restart. In such cases, you can reload the service.

sudo systemctl reload service-name

For instance, to restart the Apache web server service, execute the command below:

sudo systemctl reload apache2
Reload a Service

That’s a reload of the service, which updates its configuration but does not stop it.

Enable or Disable Services at Boot

Administrators configure services to run on startup. To do so, admins need to enable or disable the particular services at boot time.

Enable a Service

To enable service at system startup, run the following command.

sudo systemctl enable service-name

For instance, enable the Nginx web server service, execute the command below:

sudo systemctl enable nginx
Enable a Service

When you enable a service, symbolic links are added to allow systemd to start the service at boot time.

Disable a Service

To disable a service from starting at boot, use the following command.

sudo systemctl disable service-name

For example, disable the Nginx web server service, run the command below:

sudo systemctl disable nginx
Disable a Service

The service is removed from the system startup sequence when it is disabled.

Check Failed Systemd Services

Misconfiguration or dependency issues sometimes prevent services from starting correctly. In such cases, administrators can quickly locate failed services in systemd. The following command lists all failed services on the system.

systemctl --failed
Check Failed Systemd Services

This command shows failed services during startup and their status. If you hit “Enter” and find the failed service, you can see its detailed status with a systemctl status command.

View Service Logs Using Journalctl

Systemd collects logging data into a journaling system that makes it easy for admins to view log entries about services.

View Logs for a Specific Service

The logs for excluding a specific service can be displayed using the command below.

journalctl -u service-name

For example, view the Apache web server service, execute the command below:

journalctl -u apache2
View Logs for a Specific Service

This command will show logs produced by the apache2 service.

View Recent Logs

Run the following command to see the latest logs for a service. Make sure to specify the service name, such as apache2:

journalctl -u service-name -n 20
View Recent Logs

This command displays the last 20 entries from the log related to the service. Logs are very useful for diagnosing service failures and configuration issues.

Reload Systemd Configuration

If a systemd unit file is created or modified, systemd needs to be reloaded so that it can reread its configuration and acknowledge the changes made. You can reload the systemd configuration using the following command.

sudo systemctl daemon-reload
Reload Systemd Configuration

This command reloads all systemd unit files without changing the system.

In this way, you will learn how to use these commands for service management using systemd. So that it can help a system administrator to manage Linux services efficiently while maintaining reliability.

Best Practices to Manage Systemd Services

Service management prevents loss of resource efficiency and performance. Follow these best practices when working with systemd services:

  • Spot needless processes with regular service system cleaning.
  • Disable unnecessary services that are not needed for the functioning of the system.
  • Track service logs for early detection of any potential issues.
  • Use descriptive service names and documents for custom services.
  • After making significant configuration changes, reboot the services.

By following these practices, we can make sure the system resources are used effectively and securely.

Conclusion

Systemd is a system and service manager used by many modern Linux distributions to manage the lifecycle of system and background processes. Knowing how to view and manage systemd services is key to ensuring system stability, debugging problems, and administering system behavior. With the systemctl command, an administrator can list services, check their status, start or stop them, restart after configuration changes, and enable or disable them at startup. General-purpose tools like journalctl help administrators to explore the logs of services and troubleshoot issues. 

FAQs

What is systemd in Linux?
What is the systemctl command?
How to see running services in Linux?
How to start or stop a service?
How to set a service to start on boot?
How to troubleshoot a failed service?

Related Post

use htop for advance server monitoring

How to Use htop for Advanced Server Monitorin...

When working on Linux, keeping track of system performa...

How to Install and Connect to Linux Server wi...

In the developing world of IT, remote access to servers...

Exploring Sed Command in Linux

The sed command stands for stream editor is a powerful ...

Exploring Linux ip Command with Examples

The Linux IP command is a powerful tool used for managi...

How to Set Environment Variables in ZSH

Environment variables are useful when working with scri...

How to Fix Broken Packages in Kali Linux

Kali Linux a security-focused distribution relies heavi...

Leave a Comment