Movatterモバイル変換


[0]ホーム

URL:


How-To Geek logo

How to List Linux Services With systemctl

The Linux terminal open on a laptop screen running Ubuntu 20.04.Credit: Hannah Stryker / How-To Geek
4
By Dave McKay
Updated 
Dave McKay first used computers when punched paper tape was in vogue, and he has been programming ever since. After over 30 years in the IT industry, he became a full-time technology journalist. His first published article, describing macros in 6502 assembly language, was published in the December 1985 edition of the UK magazine Personal Computer World. His work has appeared on How-To Geek since March 2019. Despite graduating from Sheffield University as an archaeologist, during his career he's worked as a freelance programmer, manager of an international software development team, project manager for IT installations, and a Data Protection Officer. Dave is a Linux evangelist and open source advocate. Since 1997, all of Dave's computers have run Linux. He is a frequent guest speaker at national conferences, usually talking about topics such as artificial intelligence, data protection legislation, and cybersecurity. Expect detailed how-to's, distribution reviews, and Linux-centric editorials. 
Sign in to yourHow-To Geek account
Jump links

Jump Links

follow
Follow
followed
Followed
Thread
Here is a fact-based summary of the story contents:
Try something different:

Summary

  • On most Linux devices, you can run "systemctl --type=service --state=running" to see running services.
  • Get detailed information about a service, SSH for example, with the command "systemctl status sshd".
  • To see all enabled unit files on your system, use "systemctl list-unit-files --state=enabled".

Your Linux computer relies on a lot of background tasks called services or daemons. On systemd-based distributions you have built-in commands that let you see which services are running, disabled, or failed.

What Are Services and Daemons?

Services anddaemons are background tasks that run without a user interface, don't require human interaction, and are usually started as the computer boots up.

At one time, services were launched byinit, which was the very first process to be launched. The details of the services were held in a collection of scripts located in the "/etc/init/d" directory. Onnon-systemd distributions that's still the case.

In the systemd world, services are launched bysystemd which is now the first process to be launched. The details of the services are stored in unit files located in the "/usr/lib/systemd" directory.

Linux mascot on a desk next to a laptop.
The 8 Types of Linux Terminal Programs: Do You Know Them All?

How to tell your filters from your TUIs.

4

According to its man page,systemd is a system and service manager. You canuse thesystemctl command to inspect and control different aspects of the systemd system, including services and daemons.

Because we're looking at systemd-specific commands here, the first thing you need to know is whether you're running asystemd-based distribution or not.

init Or systemd Based?

The vast majority of Linux distributions use systemd, including Arch, Red Hat, and Debian, and many of the distributions derived from them. That includes theUbuntu family of distributions,Fedora and its spins, and Manjaro and the otherArch-based distributions.

However, there are forks or flavors of some of these distributions that have been created specifically to avoid having to use systemd. Not only that, but there are other init systems that someone could choose to use instead of the one that came by default in their distribution, such asrunit ors6-linux-init.

If you have to administer a Linux computer that you didn't set up yourself, the only way to be certain if it is using systemd or not, is to check. We can do that by looking at the process tree with thepstree command. We only need to see the very top of the tree—we're looking for the very first process that runs, after all—so we'll pipe the output throughthehead command, and ask for the first five entries.

pstree | head -5

Using pstree piped through head to determine if a Linux installation is using systemd

We can see thatsystemd is the first process that is run after boot, so we're definitely on a systemd-based installation of Linux.

Using systemctl to List Services

The command to list services and daemons issystemctl. We can refine thesystemctl command with thetype andstate options. We're askingsystemctl to report on services that are in the running state.

systemctl --type=service --state=running

Using systemctl to list running services

A table of information is generated. If it is too wide or long for your terminal window it is displayed in your default file viewer, which is likely going to beless.

The output form a systemctl call displayed in the less file viewer

To see the right-hand end of the table press the Right Arrow key. To return to the usual view, press the Left Arrow key.

The right-hand section of output form a systemctl call displayed in the less file viewer

Press the Q key to exit from less. The columns that are displayed are:

  • Unit: The name of the service or daemon. The column is titled "Unit" because whatever is in this column was launched using informationsystemd found in a unit file.
  • Load: The load state of the service or daemon. It can be loaded, not-found, bad-setting, error, or masked.
  • Active: The overall state the service or daemon is in. It can be active, reloading, inactive, failed, activating, or deactivating.
  • SUB: The sub-state of the service or daemon. It can be dead, exited, failed, inactive, or running.
  • Description: A short description of the unit.

We can pipe the output ofsystemctl throughgrep if we want to focus on a single service. This command isolates the table entry for thessh service.

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

Using grep to isolate a single service from the results

So far, we've been filtering the contents of the table by providing thestate=running option. We can use any of the possible values of the sub-state instead: dead, exited, failed, inactive, or running.

Let's look for failed services:

systemctl --type=service --state=failed

Reporting on failed services with systemctl

Combinations of sub-states can be used. Type them as a comma-separated list. Make sure you don't include any whitespace between the options. Note that this finds services that match either state.

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

Looking for services that have either failed or exited with systemctl

Pressing the Right Arrow key to look at the off-screen columns show that we have a mixture of exited and failed services in the list.

A mixture of failed and exited services found by systemctl

By default,systemctl lists processes—services and daemons—that have been launched bysystemd becausesystemd found a unit file that contained a valid unit file for them. That's why the shorthand term for all of these process is "units."

There is an option to explicitly requestsystemctl to list units, but as it is the default action, it isn't often used.

These commands produce the same results.

sudo systemctl list-units --type=service --state=running

sudo systemctl --type=service --state=running

Using systemctl to List Unit Files

We can expand the scope of thesystemctl command by including thelist-unit-files option. This doesn't just report on services and daemons that have been launched, it also lists all the unit files installed on your computer.

systemctl list-unit-files --state=enabled

Listing unit files with systemctl

A colored table is displayed.

A list of unit files generated by systemctl, displayed in the less file browser

Removing thestate option removes the filtering. The output will contain all installed unit files, regardless of their state.

systemctl list-unit-files

Using systemctl to list unit files with no filtering

The output will contain many more entries than the results from the previous commands.

All the unit files listed by systemctl and displayed in the less file browser

On our test computer the results list is almost four times longer than the output of our previous commands.

If you do want to use thestate option, you can use multiple states with it as we saw earlier. The same rules apply. Provide the options as comma separated values and don't include any whitespace.

This command will list all unit files that are either disabled or failed to launch.

systemctl list-unit-files --state=disabled,failed

Using systemctl to look for unit files that match either of two states

A reduced number of results is shown, filtered according to the selections you made with the state option.

A mixture of disabled and failed unit files found by systemctl

Looking at One Service in Detail

If something about one service or daemon piques your interest and deserves a deeper dive, you can look at it in detail using the systemctl status option.

Let's have a look at the SSH daemon, sshd. All we need to do is use the status option and the name of the service or daemon.

systemctl status sshd

The details of a single service displayed by systemctl

This compact display shows:

  • The name of the service together with a short description. A color-coded dot shows whether it is running or not. Green means it is running, red means it isn't.
  • What was loaded, including the path to the unit file.
  • How long it has been running.
  • Where the documentation is located in theman manual.
  • The Process ID of the running instance.
  • How many concurrent instances of this service are running. Usually this will be one.
  • How much memory is being consumed.
  • How much CPU time has been consumed.
  • The control group the service belongs to.

Relevant entries from the system log are also shown. These are typically events such as the startup of the service. These can be informative if you're looking into a service or daemon that didn't launch correctly.

Server data on a monitor with Database MySQL.
5 Great Linux Utilities to Monitor Your System Resources in the Terminal

Because the core utilities don't do it all.

3

The Autonomic Systems

Services and daemons provide a lot of the automatic actions of your operating system, so they're vital. That means their health is vital too.

Getting a view on your services, daemons, and unit files is easy, and informative. It's also a valuable troubleshooting step if a service or daemon refuses to start.

Follow
Followed
Share
FacebookXWhatsAppThreadsBlueskyLinkedInRedditFlipboardCopy linkEmail
Readers like you help support How-To Geek. When you make a purchase using links on our site, we may earn an affiliate commission.Read More.
A MacBook surrounded by a gear symbol, a shield, an iCloud icon, and a password dots bar.
I made my Mac more secure by changing these 5 settings
A Chromebook keyboard with the search button as the center focus.
These 5 Chromebook tips save me tons of time in Google Docs
Two Linux penguins, one cheerful with a 'Love' button, the other confused with a 'Hate' button.
5 reasons people give up on Linux (and why it’s time to come back)
See More
The back of the OnePlus 15 sitting in grass and leaves.
The OnePlus 15 can finally be sold in the U.S.
A replacement battery for a Kindle third generation eReader.
It’s time to admit you can swap out internal rechargeable batteries yourself
Several smartphones arranged diagonally on a blue geometric background, each displaying a simple home screen with a solid black wallpaper
Black is the new best wallpaper for your phone
See More

[8]ページ先頭

©2009-2025 Movatter.jp