close
close
linux list all running services

linux list all running services

4 min read 16-10-2024
linux list all running services

How to List All Running Services in Linux: A Comprehensive Guide

Knowing which services are running on your Linux system is crucial for troubleshooting issues, understanding resource usage, and optimizing performance. This guide will explore different methods to list all running services in Linux, explaining their functionalities and providing practical examples.

The Basics: Understanding Services and Processes

Before diving into the commands, let's understand the difference between services and processes:

  • Services: Background programs that provide specific functionalities, like web servers, databases, or network services. They typically run continuously and are managed by systemd (in modern Linux distributions).
  • Processes: Individual instances of running programs, including services. A single service might have multiple processes running concurrently.

Method 1: The systemctl Command (Systemd)

The systemctl command is the standard way to manage services in modern Linux distributions that use systemd.

To list all active services:

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

Explanation:

  • systemctl: The command used for systemd service management.
  • list-units: Lists all units managed by systemd.
  • --type=service: Filters the output to show only services.
  • --state=running: Shows only services that are currently running.

Output:

The command will display a list of running services along with their status, including the service name, unit type, and active state.

Example:

● sshd.service     loaded active running  SSH server
● cron.service       loaded active running  Periodic Command Scheduler
● networkd-dispatcher.service loaded active running Network Dispatcher Service
● ntp.service        loaded active running Network Time Protocol (NTP) client

Additional Information:

  • You can use systemctl status <service-name> to see detailed information about a specific running service.
  • Use systemctl restart <service-name> to restart a service.
  • Explore the systemctl documentation for more commands and options.

Method 2: The ps Command

The ps (process status) command provides a more general approach to listing processes, including those associated with running services.

To list all running processes:

ps aux

Explanation:

  • ps: The command to display process information.
  • aux: Options to display all processes in a detailed format.
  • -a: Display all processes.
  • -u: Display the user ID and username of the process owner.
  • -x: Display processes associated with terminals, even if they have exited.

Output:

The output shows a table with process details like process ID (PID), user, CPU usage, memory usage, start time, and command name.

Example:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  16648  1324 ?        Ss   09:46   0:00 /sbin/init
root         2  0.0  0.0  16648  1324 ?        Ss   09:46   0:00 [kthreadd]
root         3  0.0  0.0  16648  1324 ?        Ss   09:46   0:00 [ksoftirqd/0]
root         5  0.0  0.0  16648  1324 ?        Ss   09:46   0:00 [kworker/0:0H]
root         7  0.0  0.0  16648  1324 ?        Ss   09:46   0:00 [rcu_sched]
root         8  0.0  0.0  16648  1324 ?        Ss   09:46   0:00 [rcu_bh]
[...]
www-data   999  0.0  0.0  12140  1088 ?        Ss   10:12   0:00 /usr/sbin/apache2
root      1000  0.0  0.0  12420  1040 ?        Ss   10:13   0:00 /usr/sbin/mysqld

Filtering ps Output:

  • You can use the grep command to filter the output of ps based on keywords. For example: ps aux | grep apache2 would show processes related to Apache web server.
  • Use ps -ef to display a more user-friendly format with process IDs, usernames, and full command paths.

Method 3: The netstat Command (Network Services)

The netstat command is particularly useful for listing network services that are listening for connections.

To list all network services:

netstat -a -t -n

Explanation:

  • netstat: The command to display network connections and listening ports.
  • -a: Show all connections and listening sockets.
  • -t: Display TCP connections only.
  • -n: Display numerical addresses and ports instead of hostnames.

Output:

The output will show a table with information about active connections and listening ports, including the protocol, local address, foreign address, and state.

Example:

Active Connections
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0  0.0.0.0:22            0.0.0.0:*              LISTEN
tcp        0      0  127.0.0.1:5432          0.0.0.0:*              LISTEN
tcp        0      0  0.0.0.0:80            0.0.0.0:*              LISTEN
udp        0      0  0.0.0.0:53            0.0.0.0:*              LISTEN

Additional Information:

  • Use netstat -p to display the process ID (PID) associated with each connection or listening port.
  • Explore netstat options to filter by specific protocols, states, or addresses.

Method 4: The lsof Command (File Descriptors)

The lsof (list open files) command provides detailed information about files opened by running processes, including network sockets.

To list all open files and network sockets:

lsof -i

Explanation:

  • lsof: The command to list open files.
  • -i: Filter the output to show only network files.

Output:

The command displays a table with information about open files and network sockets, including the process ID (PID), process name, file type, and associated network addresses and ports.

Example:

COMMAND     PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
apache2     999 www-data   10u  IPv4              16777216      0t0  TCP *:80 (LISTEN)
mysqld      1000 root    10u  IPv4              16777216      0t0  TCP *:3306 (LISTEN)
ssh         1111 user    10u  IPv4              16777216      0t0  TCP *:22 (LISTEN)

Additional Information:

  • Use lsof -p <PID> to display information about the open files of a specific process.
  • Explore lsof options to filter based on file type, process name, or other criteria.

Choosing the Right Method

Each method has its own strengths and weaknesses. The best method for you depends on your specific needs:

  • systemctl: Ideal for managing systemd services and understanding their running status.
  • ps: Offers a general overview of all running processes, useful for identifying potentially problematic or resource-intensive processes.
  • netstat: Best for investigating network connections and services listening on specific ports.
  • lsof: Provides detailed information about all open files and network sockets, including the processes that opened them.

Conclusion

Understanding how to list running services in Linux is an essential skill for system administrators and developers. By using the commands and techniques described in this guide, you can gain valuable insight into the processes running on your system, diagnose problems, and optimize performance. Remember to explore the documentation for each command to discover their full range of capabilities.

Related Posts


Latest Posts