Like most operating systems, Linux distros also continuously run processes (both individual processes and groups of them) in the background. The term for these processes is “daemons.” They wait for clients to make requests and act accordingly.
The typical Linux distro runs a range of daemons to facilitate the working of the many services it offers.
Some essential services that run on Linux include system services such as syslog, process management, and cron. Network services such as domain name resolution, email, and file transfer are also essential.
There are many ways to manage (start, restart, auto-start, stop, etc.) daemons on Linux systems. But it’s typically done using a service or process manager.
Virtually all modern Linux machines employ the SystemD service manager by default. It is a suite of software tools for process management, working as a “drop-in replacement” for the init process.
SystemD is compatible with LSB and SysV init scripts and offers the systemctl command, primarily used to manage SystemD.
In this brief guide, we will walk you through using the command to list running services and other types of services in Linux.
How to List All Running Services Under Systemctl
Running systemctl without arguments gives an output of all the loaded SystemD “units.” Services are also units, and running the command shows their status. Run the following command and take a look at the output:
You can use the list-units subcommand along with the –type option, with the type set to service, to list all the services loaded on your machine:
|# systemctl list-units –type=service|
You can also use systemctl to list all the active loaded services with the –state option. As you’d expect, you would need to set the option to “active,” like so:
|# systemctl list-units –type=service –state=active|
Running the above command will list the running services and the exited services.
Now that you know some of the basics of the systemctl command, let’s move on to using it to return a list of all running services specifically. A running service is any service that is loaded and actively running.
If you’ve run the above commands, you will notice that sifting out running services from all other services can be challenging.
But systemctl makes things easy for you. You can view the running daemons on your machine using the command:
|# systemctl list-units –type=service –state=running|
If for any reason, you need to use the above command repeatedly, it makes sense to write an alias command. You must write this command in the ~/.bashrc file. The alias command will allow you to invoke the systemctl command above easily.
Begin by opening the file in vim like so:
|# vim ~/.bashrc|
When it opens, add this line under the list of aliases:
|alias running_services=’systemctl list-units –type=service –state=running’|
Saving the changes and closing the file will create the “running_services” command. You can run it whenever you want to view the list of loaded and running services on the server.
One of the most critical attributes of services is the port they use. You can use ss tools or netstat to determine which port a service is listening on. Run one of the following commands:
|# netstat -ltup | grep zabbix_agentd|
|# ss -ltup | grep zabbix_agentd|
The -l option instructs the command to print all listening sockets. The -u option shows every UDP connection, -t shows every TCP connection, and -n prints numeric port numbers. Finally, the -p option shows the application name.
When you run either command, you will see that the fifth column shows the port.
Almost every server has a firewall service running. If yours is, too, the service will control the traffic allowed and blocked for specific services and ports.
But here’s the nice thing:
The firewall-cmd command enables you to list the services or ports the firewall is working with. Depending on your Linux distro, you might have to use the ufw command instead of the firewall-cmd command.
Run the commands:
|# firewall-cmd –list-services [FirewallD]
# firewall-cmd –list-ports
$ sudo ufw status [UFW Firewall]
Listing All Services with Systemctl
While looking at the running services is often helpful, in some circumstances, you might have to look at all the services running on the server.
To list all the services – loaded or inactive – you can run the command:
|$ systemctl list-units –type=service –all|
Listing Enabled and Disabled Services with Systemctl
Checking the enabled services in a machine is as straightforward as running:
|$ systemctl list-unit-files –state=enabled|
Disabled services are the ones that do not activate or start automatically. Here’s the command to run when you want to see a list of the disabled services on your server:
|$ systemctl list-unit-files –state=disabled|
Starting a disabled service is easy; all you have to do is run the command:
|$ systemctl start service-name|
Checking Service Status with Systemctl
Systemctl has a “cup” command that will help you find more information about a service’s status. The command is a modular printing system that essentially turns your server into a print server. This way, it can display the service status.
Here’s how you can use the cups command to find information about an enabled or disabled service:
|$ systemctl status cups.service|
Where Are Systemctl Service Files?
The SystemD config files are stored across the system unit directories and user unit directories.
To find the location of these directories, you can use the pkg-config systemd command like so:
|$ pkg-config systemd –variable=systemdsystemunitdir
$ pkg-config systemd –variable=systemduserunitdir
All of SystemD’s unit files will be in these directories, and you can browse them freely.
Now that you’ve gone through this guide, you know how to use systemctl to list running services and other types of services effortlessly.
Make sure you bookmark this post or note down the purposes of every command. This way, you’ll have an easier time finding and running the right command when the time comes.