As a Linux system administrator, having a strong grasp of services management is critical. Ubuntu, one of the most popular Linux distributions, relies heavily on services running in the background to function properly. Knowing how to list these services using the various tools available can help diagnose problems and fine-tune your system.

In this comprehensive guide, we will cover the ins and outs of listing services in Ubuntu. We‘ll explore the key commands and utilities that allow you to see which services are running, view details about specific services, and control service states. Let‘s dive in!

Understanding Services in Ubuntu

Services in Ubuntu refer to long-running processes that manage critical system resources and functions in the background. Some common examples include:

  • sshd: manages all SSH remote connections
  • cron: handles scheduled tasks
  • networking: controls network interfaces and connectivity
  • cups: manages printing services

Services are managed by Ubuntu‘s init system, which has changed over the years:

  • Upstart (Ubuntu versions prior to 15.04)
  • systemd (Ubuntu 15.04 onwards, default init system currently)

Now let‘s look at how to list these services using various methods.

Listing All Services with systemctl

On current Ubuntu versions with systemd, the systemctl command is the primary tool for service management and introspection.

To see a list of all loaded services, simply run:

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

This will give an output similar to:

UNIT                                 LOAD   ACTIVE SUB       DESCRIPTION
accounts-daemon.service              loaded active running   Accounts Service
acpid.service                        loaded active running   ACPI event daemon
alsa-restore.service                 loaded active exited    Save/Restore Sound Card State
anacron.service                      loaded active running   Run anacron jobs
apache2.service                      loaded active running   The Apache HTTP Server
apport.service                       loaded active running   LSB: automatic crash report generation
atd.service                          loaded active running   Deferred execution scheduler
avahi-daemon.service                 loaded active running   Avahi mDNS/DNS-SD Stack

This shows all loaded services, their current active status, if they have a process currently running, and a short description.

Use the --state= parameter to filter by inactive, active, or other states:

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

Getting Unit Info for One Service

To see in-depth information on a single service unit, use systemctl status:

sudo systemctl status ssh

Output:

● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: en
     Active: active (running) since Tue 2022-12-13 22:31:38 UTC; 1 weeks 3 day
Triggers: ● ssh.service

This provides the full service name, if its enabled on boot, when it was activated, and more. Very useful!

Listing All Unit Files on Disk

While systemctl list-units shows currently loaded services, the systemctl list-unit-files command will display all available unit files on disk:

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

This lists every possible service, with information if they are enabled or disabled:

UNIT FILE              STATE   
apache2.service        enabled
apport.service         enabled
atd.service            enabled
avahi-daemon.service   enabled
bluetooth.service      enabled
cron.service           enabled
...

Filtering Services by Status

You can combine systemctl with grep to filter services by status or other criteria.

For example, to see all running services:

sudo systemctl list-units --type=service --all | grep running

Or to find failed services:

sudo systemctl list-units --type=service --all | grep failed

Viewing Processes and Their Services

While the above commands let you introspect systemd services specifically, for a process-level view you can use the pstree utility.

Running pstree with no arguments will display all processes with their child processes underneath in a visual tree:

     init─┬─NetworkManager─┬─dhclient
         │                └─2*[{NetworkManager}]
         ├─2*[agetty]
         ├─apache2───10*[apache2───6*[{apache2}]]
         ├─atd
         ├─cron
         ├─dbus-daemon
         ├─dhcpcd───6*[{dhcpcd}]
         ├─login───bash
         ├─lvmetad
         ├─mysqld───5*[{mysqld}]
         ├─polkitd───6*[{polkitd}]
         ├─rpcbind
         ├─rsyslogd───3*[{rsyslogd}]
         ├─sshd───sshd───bash───pstree
         ├─systemd-journal
         ├─systemd-logind
         ├─systemd-udevd
         └─upowerd

Any processes related to managing services will be shown, like apache2 and sshd here.

Using pstree -p will additionally print the PID (process ID) alongside each process. Very helpful to match PID files to running processes.

Checking Resource Usage with systemd-cgtop

To see services ranked by their resource usage (CPU, memory, disk, etc), use the systemd-cgtop utility:

$ systemd-cgtop
Path                                                       Tasks   %CPU   Memory  Input/s Output/s

/init.scope                                                 144    0.5   236.4M        -        -     
/system.slice/docker.service                                 22    0.1      1.4G        -        -     
/system.slice/java.service                                   89    0.1   427.2M        -        -     
/system.slice/-.mount                                         1    0.0    132.0K        0        0
/system.slice/cups.service                                   16    0.0    200.6M        -        -     
/system.slice/irqbalance.service                             1    0.0     46.1M        -        -     

This displays resource consumption by systemd control group, allowing you to identify services using the most resources.

Listing Services on Remote Ubuntu Servers

Need to check services on an Ubuntu server you manage through SSH?

You can SSH into the remote host, then run the systemctl and pstree commands shown above directly on that server.

Or even better – use ssh to remotely execute those commands:

ssh admin@server ‘systemctl list-units --type=service‘

This will run systemctl on the remote server but print the output locally. Very handy when managing multiple Ubuntu servers!

Managing Services Based on Status

Once you identify a service that is stopped, failed, or otherwise needing intervention – most management can also be done via systemctl.

Such as stopping, starting, restarting services:

sudo systemctl stop apache2
sudo systemctl start mysql 
sudo systemctl restart cron

Or disabling/enabling them from starting on system boot:

sudo systemctl disable nfs-server
sudo systemctl enable docker

See man systemctl for additional service management capabilities.

Conclusion

I hope this guide has provided both breadth and depth on the topic of listing and viewing services within Ubuntu Linux.

Key takeways include:

  • Using systemctl to see loaded units, unit files, filter by state
  • Viewing process trees with pstree
  • Checking resource usage by control group
  • Remotely running commands over SSH
  • Controlling services based on their status

Having access to observe and control what services are running is imperative for any Linux sysadmin. Master these tools for Ubuntu/systemd and you‘ll have an immense amount of power at your fingertips!

Let me know in the comments if you have any other favorite tricks for services introspection in Linux!

Similar Posts