如何查看 Linux 中所有正在运行的服务 | Linux 中国

致谢

编译自 | 
https://www.2daygeek.com/how-to-check-all-running-services-in-linux/ 
 作者 | Magesh Maruthamuthu
 译者 | jessie-pang 共计翻译:13 篇 贡献时间:215 天

有许多方法和工具可以查看 Linux 中所有正在运行的服务。大多数管理员会在 System V(SysV)初始化系统中使用 service service-name status 或 /etc/init.d/service-name status,而在 systemd 初始化系统中使用 systemctl status service-name

以上命令可以清楚地显示该服务是否在服务器上运行,这也是每个 Linux 管理员都该知道的非常简单和基础的命令。

如果你对系统环境并不熟悉,也不清楚系统在运行哪些服务,你会如何检查?

是的,我们的确有必要这样检查一下。这将有助于我们了解系统上运行了什么服务,以及哪些是必要的、哪些需要被禁用。

init(初始化initialization的简称)是在系统启动期间运行的第一个进程。init 是一个守护进程,它将持续运行直至关机。

大多数 Linux 发行版都使用如下的初始化系统之一:

◈ System V 是更老的初始化系统

◈ Upstart 是一个基于事件的传统的初始化系统的替代品

◈ systemd 是新的初始化系统,它已经被大多数最新的 Linux 发行版所采用

什么是 System V(SysV)

SysV(意即 System V) 初始化系统是早期传统的初始化系统和系统管理器。由于 sysVinit 系统上一些长期悬而未决的问题,大多数最新的发行版都适用于 systemd 系统。

什么是 Upstart 初始化系统

Upstart 是一个基于事件的 /sbin/init 的替代品,它控制在启动时的任务和服务的开始,在关机时停止它们,并在系统运行时监控它们。

它最初是为 Ubuntu 发行版开发的,但其是以适合所有 Linux 发行版的开发为目标的,以替换过时的 System-V 初始化系统。

什么是 systemd

systemd 是一个新的初始化系统以及系统管理器,它已成为大多数 Linux 发行版中非常流行且广泛适应的新的标准初始化系统。systemctl 是一个 systemd 管理工具,它可以帮助我们管理 systemd 系统。

方法一:如何在 System V(SysV)系统中查看运行的服务

以下命令可以帮助我们列出 System V(SysV) 系统中所有正在运行的服务。

如果服务很多,我建议使用文件查看命令,如 lessmore 等,以便得到清晰的结果。

 
  1. # service --status-all

  2. # service --status-all | more

  3. # service --status-all | less

 
  1. abrt-ccpp hook is installed

  2. abrtd (pid  2131) is running...

  3. abrt-dump-oops is stopped

  4. acpid (pid  1958) is running...

  5. atd (pid  2164) is running...

  6. auditd (pid  1731) is running...

  7. Frequency scaling enabled using ondemand governor

  8. crond (pid  2153) is running...

  9. hald (pid  1967) is running...

  10. htcacheclean is stopped

  11. httpd is stopped

  12. Table: filter

  13. Chain INPUT (policy ACCEPT)

  14. num  target     prot opt source               destination

  15. 1    ACCEPT     all      ::/0                 ::/0                state RELATED,ESTABLISHED

  16. 2    ACCEPT     icmpv6    ::/0                 ::/0

  17. 3    ACCEPT     all      ::/0                 ::/0

  18. 4    ACCEPT     tcp      ::/0                 ::/0                state NEW tcp dpt:80

  19. 5    ACCEPT     tcp      ::/0                 ::/0                state NEW tcp dpt:21

  20. 13   REJECT     all      ::/0                 ::/0                reject-with icmp6-adm-prohibited

  21.  

  22. Chain FORWARD (policy ACCEPT)

  23. num  target     prot opt source               destination

  24. 1    REJECT     all      ::/0                 ::/0                reject-with icmp6-adm-prohibited

  25.  

  26. Chain OUTPUT (policy ACCEPT)

  27. num  target     prot opt source               destination

  28.  

  29. iptables: Firewall is not running.

  30. irqbalance (pid 1826) is running...

  31. Kdump is operational

  32. lvmetad is stopped

  33. mdmonitor is stopped

  34. messagebus (pid  1929) is running...

  35. SUCCESS! MySQL running (24376)

  36. rndc: neither /etc/rndc.conf nor /etc/rndc.key was found

  37. named is stopped

  38. netconsole module not loaded

  39. Usage: startup.sh { start | stop }

  40. Configured devices:

  41. lo eth0 eth1

  42. Currently active devices:

  43. lo eth0

  44. ntpd is stopped

  45. portreserve (pid  1749) is running...

  46. master (pid  2107) is running...

  47. Process accounting is disabled.

  48. quota_nld is stopped

  49. rdisc is stopped

  50. rngd is stopped

  51. rpcbind (pid  1840) is running...

  52. rsyslogd (pid  1756) is running...

  53. sandbox is stopped

  54. saslauthd is stopped

  55. smartd is stopped

  56. openssh-daemon (pid  9859) is running...

  57. svnserve is stopped

  58. vsftpd (pid 4008) is running...

  59. xinetd (pid  2031) is running...

  60. zabbix_agentd (pid 2150 2149 2148 2147 2146 2140) is running...

执行以下命令,可以只查看正在运行的服务:

 
  1. # service --status-all | grep running

 
  1. crond (pid 535) is running...

  2. httpd (pid 627) is running...

  3. mysqld (pid 911) is running...

  4. rndc: neither /etc/rndc.conf nor /etc/rndc.key was found

  5. rsyslogd (pid 449) is running...

  6. saslauthd (pid 492) is running...

  7. sendmail (pid 509) is running...

  8. sm-client (pid 519) is running...

  9. openssh-daemon (pid 478) is running...

  10. xinetd (pid 485) is running...

运行以下命令以查看指定服务的状态:

 
  1. # service --status-all | grep httpd

  2. httpd (pid 627) is running...

或者,使用以下命令也可以查看指定服务的状态:

 
  1. # service httpd status

  2. httpd (pid 627) is running...

使用以下命令查看系统启动时哪些服务会被启用:

 
  1. # chkconfig --list

 
  1. crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off

  2. htcacheclean    0:off   1:off   2:off   3:off   4:off   5:off   6:off

  3. httpd           0:off   1:off   2:off   3:on    4:off   5:off   6:off

  4. ip6tables       0:off   1:off   2:on    3:off   4:on    5:on    6:off

  5. iptables        0:off   1:off   2:on    3:on    4:on    5:on    6:off

  6. modules_dep     0:off   1:off   2:on    3:on    4:on    5:on    6:off

  7. mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

  8. xinetd          0:off   1:off   2:off   3:on    4:on    5:on    6:off

  9.  

  10. xinetd based services:

  11.        chargen-dgram:  off

  12.        chargen-stream: off

  13.        daytime-dgram:  off

  14.        daytime-stream: off

  15.        discard-dgram:  off

  16.        discard-stream: off

  17.        echo-dgram:     off

  18.        echo-stream:    off

  19.        finger:         off

  20.        ntalk:          off

  21.        rsync:          off

  22.        talk:           off

  23.        tcpmux-server:  off

  24.        time-dgram:     off

  25.        time-stream:    off

方法二:如何在 System V(SysV)系统中查看运行的服务

另外一种在 Linux 系统上列出运行的服务的方法是使用 initctl 命令:

 
  1. # initctl list

  2. rc stop/waiting

  3. tty (/dev/tty3) start/running, process 1740

  4. tty (/dev/tty2) start/running, process 1738

  5. tty (/dev/tty1) start/running, process 1736

  6. tty (/dev/tty6) start/running, process 1746

  7. tty (/dev/tty5) start/running, process 1744

  8. tty (/dev/tty4) start/running, process 1742

  9. plymouth-shutdown stop/waiting

  10. control-alt-delete stop/waiting

  11. rcS-emergency stop/waiting

  12. readahead-collector stop/waiting

  13. kexec-disable stop/waiting

  14. quit-plymouth stop/waiting

  15. rcS stop/waiting

  16. prefdm stop/waiting

  17. init-system-dbus stop/waiting

  18. ck-log-system-restart stop/waiting

  19. readahead stop/waiting

  20. ck-log-system-start stop/waiting

  21. splash-manager stop/waiting

  22. start-ttys stop/waiting

  23. readahead-disable-services stop/waiting

  24. ck-log-system-stop stop/waiting

  25. rcS-sulogin stop/waiting

  26. serial stop/waiting

方法三:如何在 systemd 系统中查看运行的服务

以下命令帮助我们列出 systemd 系统中所有服务:

 
  1. # systemctl

  2.  UNIT                                                                                     LOAD   ACTIVE SUB       DESCRIPTION                                                      

  3.  sys-devices-virtual-block-loop0.device                                                   loaded active plugged   /sys/devices/virtual/block/loop0                                

  4.  sys-devices-virtual-block-loop1.device                                                   loaded active plugged   /sys/devices/virtual/block/loop1                                                              

  5.  sys-devices-virtual-tty-ttyprintk.device                                                 loaded active plugged   /sys/devices/virtual/tty/ttyprintk                              

  6.  sys-module-fuse.device                                                                   loaded active plugged   /sys/module/fuse                                                

  7.  sys-subsystem-net-devices-enp0s3.device                                                  loaded active plugged   82540EM Gigabit Ethernet Controller (PRO/1000 MT Desktop Adapter)

  8.  -.mount                                                                                  loaded active mounted   Root Mount                                                      

  9.  dev-hugepages.mount                                                                      loaded active mounted   Huge Pages File System                                          

  10.  dev-mqueue.mount                                                                         loaded active mounted   POSIX Message Queue File System                                  

  11.  run-user-1000-gvfs.mount                                                                 loaded active mounted   /run/user/1000/gvfs                                              

  12.  run-user-1000.mount                                                                      loaded active mounted   /run/user/1000                                                  

  13.  snap-core-3887.mount                                                                     loaded active mounted   Mount unit for core                                                                                               

  14.  dbus.service                                                                             loaded active running   D-Bus System Message Bus                                        

  15.  postfix.service                                                                          loaded active exited    Postfix Mail Transport Agent                        

◈ UNIT 相应的 systemd 单元名称

◈ LOAD 相应的单元是否被加载到内存中

◈ ACTIVE 该单元是否处于活动状态

◈ SUB 该单元是否处于运行状态(LCTT 译注:是较于 ACTIVE 更加详细的状态描述,不同的单元类型有不同的状态。)

◈ DESCRIPTION 关于该单元的简短描述

以下选项可根据类型列出单元:

 
  1. # systemctl list-units --type service

  2.  UNIT                               LOAD   ACTIVE SUB     DESCRIPTION                                                      

  3.  accounts-daemon.service            loaded active running Accounts Service                                                

  4.  acpid.service                      loaded active running ACPI event daemon                                                                                 

  5.  kmod-static-nodes.service          loaded active exited  Create list of required static device nodes for the current kernel

以下选项可帮助您根据状态列出单位,输出与前例类似但更直截了当:

 
  1. # systemctl list-unit-files --type service

  2.  

  3. UNIT FILE                                  STATE  

  4. accounts-daemon.service                    enabled

  5. acpid.service                              disabled

  6. alsa-restore.service                       static  

  7. alsa-state.service                         static  

  8. alsa-utils.service                         masked  

  9. anacron-resume.service                     enabled

  10. anacron.service                            enabled

  11. apache-htcacheclean.service                disabled

  12. apache-htcacheclean@.service               disabled

  13. apache2.service                            enabled

  14. apache2@.service                           disabled

  15. apparmor.service                           enabled

  16. apport-forward@.service                    static  

  17. apport.service                             generated

  18. apt-daily-upgrade.service                  static  

  19. apt-daily.service                          static  

  20. aptik-battery-monitor.service              generated

  21. atop.service                               enabled

  22. atopacct.service                           enabled

  23. autovt@.service                            enabled

  24. avahi-daemon.service                       enabled

  25. bluetooth.service                          enabled

运行以下命令以查看指定服务的状态:

 
  1. # systemctl | grep apache2

  2.  apache2.service                                                                          loaded active running   The Apache HTTP Server

或者,使用以下命令也可查看指定服务的状态:

 
  1. # systemctl status apache2

  2. ● apache2.service - The Apache HTTP Server

  3.   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)

  4.  Drop-In: /lib/systemd/system/apache2.service.d

  5.           └─apache2-systemd.conf

  6.   Active: active (running) since Tue 2018-03-06 12:34:09 IST; 8min ago

  7.  Process: 2786 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS)

  8. Main PID: 1171 (apache2)

  9.    Tasks: 55 (limit: 4915)

  10.   CGroup: /system.slice/apache2.service

  11.           ├─1171 /usr/sbin/apache2 -k start

  12.           ├─2790 /usr/sbin/apache2 -k start

  13.           └─2791 /usr/sbin/apache2 -k start

  14.  

  15. Mar 06 12:34:08 magi-VirtualBox systemd[1]: Starting The Apache HTTP Server...

  16. Mar 06 12:34:09 magi-VirtualBox apachectl[1089]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.0.2.15. Set the 'ServerName' directive globally to suppre

  17. Mar 06 12:34:09 magi-VirtualBox systemd[1]: Started The Apache HTTP Server.

  18. Mar 06 12:39:10 magi-VirtualBox systemd[1]: Reloading The Apache HTTP Server.

  19. Mar 06 12:39:10 magi-VirtualBox apachectl[2786]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using fe80::7929:4ed1:279f:4d65. Set the 'ServerName' directive gl

  20. Mar 06 12:39:10 magi-VirtualBox systemd[1]: Reloaded The Apache HTTP Server.

执行以下命令,只查看正在运行的服务:

 
  1. # systemctl | grep running

  2.  acpid.path                                                                               loaded active running   ACPI Events Check                                                

  3.  cups.path                                                                                loaded active running   CUPS Scheduler                                                  

  4.  init.scope                                                                               loaded active running   System and Service Manager                                      

  5.  session-c2.scope                                                                         loaded active running   Session c2 of user magi                                                                                          

  6.  polkit.service                                                                           loaded active running   Authorization Manager                                

使用以下命令查看系统启动时会被启用的服务列表:

 
  1. # systemctl list-unit-files | grep enabled

  2. acpid.path                                 enabled

  3. cups.path                                  enabled

  4. accounts-daemon.service                    enabled

  5. anacron-resume.service                     enabled

  6. anacron.service                            enabled

  7. apache2.service                            enabled

  8. apparmor.service                           enabled

  9. atop.service                               enabled

  10. atopacct.service                           enabled

  11. autovt@.service                            enabled

  12. avahi-daemon.service                       enabled

  13. bluetooth.service                          enabled

  14. console-setup.service                      enabled

  15. cron.service                               enabled

  16. cups-browsed.service                       enabled

  17. cups.service                               enabled

  18. display-manager.service                    enabled

  19. dns-clean.service                          enabled

  20. friendly-recovery.service                  enabled

  21. getty@.service                             enabled

  22. gpu-manager.service                        enabled

  23. keyboard-setup.service                     enabled

  24. lightdm.service                            enabled

  25. ModemManager.service                       enabled

  26. network-manager.service                    enabled

  27. networking.service                         enabled

  28. NetworkManager-dispatcher.service          enabled

  29. NetworkManager-wait-online.service         enabled

  30. NetworkManager.service                     enabled

systemd-cgtop 按资源使用情况(任务、CPU、内存、输入和输出)列出控制组:

 
  1. # systemd-cgtop

  2.  

  3. Control Group                                              Tasks   %CPU   Memory  Input/s Output/s

  4. /                                                              -      -     1.5G        -        -

  5. /init.scope                                                    1      -        -        -        -

  6. /system.slice                                                153      -        -        -        -

  7. /system.slice/ModemManager.service                             3      -        -        -        -

  8. /system.slice/NetworkManager.service                           4      -        -        -        -

  9. /system.slice/accounts-daemon.service                          3      -        -        -        -

  10. /system.slice/acpid.service                                    1      -        -        -        -

  11. /system.slice/apache2.service                                 55      -        -        -        -

  12. /system.slice/aptik-battery-monitor.service                    1      -        -        -        -

  13. /system.slice/atop.service                                     1      -        -        -        -

  14. /system.slice/system-getty.slice                               1      -        -        -        -

同时,我们可以使用 pstree 命令(输出来自 SysVinit 系统)查看正在运行的服务:

 
  1. # pstree

  2. init-+-crond

  3.     |-httpd---2*[httpd]

  4.     |-kthreadd/99149---khelper/99149

  5.     |-2*[mingetty]

  6.     |-mysqld_safe---mysqld---9*[{mysqld}]

  7.     |-rsyslogd---3*[{rsyslogd}]

  8.     |-saslauthd---saslauthd

  9.     |-2*[sendmail]

  10.     |-sshd---sshd---bash---pstree

  11.     |-udevd

  12.     `-xinetd

我们还可以使用 pstree 命令(输出来自 systemd 系统)查看正在运行的服务:

 
  1. # pstree

  2. systemd─┬─ModemManager─┬─{gdbus}

  3.        │              └─{gmain}

  4.        ├─NetworkManager─┬─dhclient

  5.        │                ├─{gdbus}

  6.        │                └─{gmain}

  7.        ├─accounts-daemon─┬─{gdbus}

  8.        │                 └─{gmain}

  9.        ├─acpid

  10.        ├─agetty

  11.        ├─anacron

  12.        ├─apache2───2*[apache2───26*[{apache2}]]

  13.        ├─aptd───{gmain}

  14.        ├─aptik-battery-m

  15.        ├─atop

  16.        ├─atopacctd

  17.        ├─avahi-daemon───avahi-daemon

  18.        ├─colord─┬─{gdbus}

  19.        │        └─{gmain}

  20.        ├─cron

  21.        ├─cups-browsed─┬─{gdbus}

  22.        │              └─{gmain}

  23.        ├─cupsd

  24.        ├─dbus-daemon

  25.        ├─fwupd─┬─{GUsbEventThread}

  26.        │       ├─{fwupd}

  27.        │       ├─{gdbus}

  28.        │       └─{gmain}

  29.        ├─gnome-keyring-d─┬─{gdbus}

  30.        │                 ├─{gmain}

  31.        │                 └─{timer}

方法四:如何使用 chkservice 在 systemd 系统中查看正在运行的服务

chkservice 是一个管理系统单元的终端工具,需要超级用户权限。

 
  1. # chkservice

要查看帮助页面,请按下 ? ,它将显示管理 systemd 服务的可用选项。


via: https://www.2daygeek.com/how-to-check-all-running-services-in-linux/

作者:Magesh Maruthamuthu[2] 译者:jessie-pang 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值