Linux系统提供各种系统服务(如进程管理、登录、syslog、cron等)和网络服务(如远程登录、电子邮件、打印机、虚拟主机、数据存储、文件传输、域名解析) (使用 DNS)、动态 IP 地址分配(使用DHCP)等等)。
从技术上讲,服务是在后台连续运行的一个或一组进程(通常称为守护进程),等待传入的请求(尤其是来自客户端的请求)。
Linux 支持不同的方式来管理服务(启动、停止、重新启动、在系统启动时启用自动启动等),通常通过进程或服务管理器。大多数(如果不是全部)现代 Linux 发行版现在都使用相同的进程管理器:systemd。
什么是 Systemd?
Systemd是 Linux 的系统和服务管理器;init进程的直接替代品,与SysV和LSB初始化脚本兼容,并且systemctl 命令是管理systemd的主要工具。
为什么列出 Linux 中正在运行的服务?
出于多种原因,了解 Linux 系统上正在运行的服务至关重要。它使您能够监控资源利用率、解决问题并有效管理系统性能。Systemd 通过提供强大的命令来收集有关活动服务的信息,从而简化了这项任务。
在本指南中,我们将演示列出 Linux 中 Systemd 下所有正在运行的服务的过程,为所有经验级别的用户提供全面的演练。
列出 Linux 中 SystemD 下正在运行的服务
当您运行不带任何参数的systemctl 命令时,它将显示所有已加载的 systemd 单元的列表(有关 systemd 单元的更多信息,请阅读 systemd 文档),包括服务,并显示其状态(无论是否活动)。
# 系统控制
![](https://www.alaica.com/wp-content/uploads/linux-276.png)
列出 systemctl 中的所有单位
要列出系统上所有已加载的服务(无论是活动的、正在运行的、已退出的还是失败的),请使用list-units子命令并--type
使用 service 值进行切换。
# systemctl list-units --type=service 或者 # systemctl --type=服务
![](https://www.alaica.com/wp-content/uploads/linux-277.png)
要列出所有已加载但处于活动状态的服务(包括正在运行的服务和已退出的服务),您可以添加--state
值为active的选项,如下所示。
# systemctl list-units --type=service --state=active 或者 # systemctl --type=service --state=active
![](https://www.alaica.com/wp-content/uploads/linux-278.png)
列出 systemctl 中正在运行的服务
但要快速浏览所有正在运行的服务(即所有已加载且正在运行的服务),请运行以下命令。
# systemctl list-units --type=service --state=running 或者 # systemctl --type=service --state=running
![](https://www.alaica.com/wp-content/uploads/linux-279.png)
Let’s explore the key terms related to Systemd units and their status:
- Unit – A unit could be a service, a socket, a device, or various other entities.
- Load – It indicates whether the unit is loaded or not. A unit can be loaded but not necessarily active.
- Active – It shows whether the unit is actively running or whether it has encountered issues and is in a failed or inactive state.
- SUB – It provides additional details about the specific state of the unit. For services, it might indicate whether the service is running (running), stopped (exited), or encountering issues (failed).
- Description – It helps users identify and understand the purpose of the unit without delving into the detailed configuration files.
Create an Alias for systemctl
If you frequently use the previous command, you can create an alias command in your ~/.bashrc file as shown, to easily invoke it.
# vim ~/.bashrc
Then add the following line under the list of aliases as shown in the screenshot.
alias running_services='systemctl list-units --type=service --state=running'
![](https://www.alaica.com/wp-content/uploads/linux-280.png)
Save the changes in the file and close it. From now onwards, use the “running_services” command to view a list of all loaded, actively running services on your server.
# running_services #use the Tab completion
![](https://www.alaica.com/wp-content/uploads/linux-281.png)
Find a Port a Process is Listening On
Besides, an important aspect of services is the port they use. To determine the port a daemon process is listening on, you can use the netstat or ss command as shown.
Where the flag -l
means print all listening sockets, -t
displays all TCP connections, -u
shows all UDP connections, -n
means print numeric port numbers (instead of application names) and -p
means show the application name.
# netstat -ltup | grep zabbix_agentd OR # ss -ltup | grep zabbix_agentd
The fifth column shows the socket: Local Address:Port. In this case, the process zabbix_agentd is listening on port 10050.
![](https://www.alaica.com/wp-content/uploads/linux-282.png)
列出正在运行的防火墙服务
此外,如果您的服务器正在运行防火墙服务,该服务控制如何阻止或允许进出选定服务或端口的流量,您可以使用firewall-cmd或ufw 命令列出已在防火墙中打开的服务或端口(取决于您使用的 Linux 发行版),如图所示。
# 防火墙-cmd --list-services [FirewallD] # 防火墙-cmd --list-ports $ sudo ufw status [UFW 防火墙]
![](https://www.alaica.com/wp-content/uploads/linux-283.png)
目前为止就这样了!在本指南中,我们演示了如何在 Linux 中查看systemd下正在运行的服务。我们还介绍了如何检查端口服务正在侦听以及如何查看系统防火墙中打开的服务或端口。
您还有什么补充或疑问吗?如果是,请使用下面的评论表联系我们。