有許多方法和工具可以查看 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) 系統中所有正在運行的服務。
如果服務很多,我建議使用文件查看命令,如 less、more 等,以便得到清晰的結果。
# service --status-all或# service --status-all | more或# service --status-all | less
abrt-ccpp hook is installedabrtd (pid 2131) is running...abrt-dump-oops is stoppedacpid (pid 1958) is running...atd (pid 2164) is running...auditd (pid 1731) is running...Frequency scaling enabled using ondemand governorcrond (pid 2153) is running...hald (pid 1967) is running...htcacheclean is stoppedhttpd is stoppedTable: filterChain INPUT (policy ACCEPT)num target prot opt source destination1 ACCEPT all ::/0 ::/0 state RELATED,ESTABLISHED2 ACCEPT icmpv6 ::/0 ::/03 ACCEPT all ::/0 ::/04 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:805 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:216 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:227 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:258 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:20829 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:208610 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:208311 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:208712 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:1000013 REJECT all ::/0 ::/0 reject-with icmp6-adm-prohibitedChain FORWARD (policy ACCEPT)num target prot opt source destination1 REJECT all ::/0 ::/0 reject-with icmp6-adm-prohibitedChain OUTPUT (policy ACCEPT)num target prot opt source destinationiptables: Firewall is not running.irqbalance (pid 1826) is running...Kdump is operationallvmetad is stoppedmdmonitor is stoppedmessagebus (pid 1929) is running...SUCCESS! MySQL running (24376)rndc: neither /etc/rndc.conf nor /etc/rndc.key was foundnamed is stoppednetconsole module not loadedUsage: startup.sh { start | stop }Configured devices:lo eth0 eth1Currently active devices:lo eth0ntpd is stoppedportreserve (pid 1749) is running...master (pid 2107) is running...Process accounting is disabled.quota_nld is stoppedrdisc is stoppedrngd is stoppedrpcbind (pid 1840) is running...rsyslogd (pid 1756) is running...sandbox is stoppedsaslauthd is stoppedsmartd is stoppedopenssh-daemon (pid 9859) is running...svnserve is stoppedvsftpd (pid 4008) is running...xinetd (pid 2031) is running...zabbix_agentd (pid 2150 2149 2148 2147 2146 2140) is running...
執行以下命令,可以只查看正在運行的服務:
# service --status-all | grep running
crond (pid 535) is running...httpd (pid 627) is running...mysqld (pid 911) is running...rndc: neither /etc/rndc.conf nor /etc/rndc.key was foundrsyslogd (pid 449) is running...saslauthd (pid 492) is running...sendmail (pid 509) is running...sm-client (pid 519) is running...openssh-daemon (pid 478) is running...xinetd (pid 485) is running...
運行以下命令以查看指定服務的狀態:
# service --status-all | grep httpdhttpd (pid 627) is running...
或者,使用以下命令也可以查看指定服務的狀態:
# service httpd statushttpd (pid 627) is running...
使用以下命令查看系統啟動時哪些服務會被啟用:
# chkconfig --list
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:offhtcacheclean 0:off 1:off 2:off 3:off 4:off 5:off 6:offhttpd 0:off 1:off 2:off 3:on 4:off 5:off 6:offip6tables 0:off 1:off 2:on 3:off 4:on 5:on 6:offiptables 0:off 1:off 2:on 3:on 4:on 5:on 6:offmodules_dep 0:off 1:off 2:on 3:on 4:on 5:on 6:offmysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:offnamed 0:off 1:off 2:off 3:off 4:off 5:off 6:offnetconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:offnetfs 0:off 1:off 2:off 3:off 4:on 5:on 6:offnetwork 0:off 1:off 2:on 3:on 4:on 5:on 6:offnmb 0:off 1:off 2:off 3:off 4:off 5:off 6:offnscd 0:off 1:off 2:off 3:off 4:off 5:off 6:offportreserve 0:off 1:off 2:on 3:off 4:on 5:on 6:offquota_nld 0:off 1:off 2:off 3:off 4:off 5:off 6:offrdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:offrestorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:offrpcbind 0:off 1:off 2:on 3:off 4:on 5:on 6:offrsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:offsaslauthd 0:off 1:off 2:off 3:on 4:off 5:off 6:offsendmail 0:off 1:off 2:on 3:on 4:on 5:on 6:offsmb 0:off 1:off 2:off 3:off 4:off 5:off 6:offsnmpd 0:off 1:off 2:off 3:off 4:off 5:off 6:offsnmptrapd 0:off 1:off 2:off 3:off 4:off 5:off 6:offsshd 0:off 1:off 2:on 3:on 4:on 5:on 6:offudev-post 0:off 1:on 2:on 3:off 4:on 5:on 6:offwinbind 0:off 1:off 2:off 3:off 4:off 5:off 6:offxinetd 0:off 1:off 2:off 3:on 4:on 5:on 6:offxinetd based services:chargen-dgram: offchargen-stream: offdaytime-dgram: offdaytime-stream: offdiscard-dgram: offdiscard-stream: offecho-dgram: offecho-stream: offfinger: offntalk: offrsync: offtalk: offtcpmux-server: offtime-dgram: offtime-stream: off
方法二:如何在 System V(SysV)系統中查看運行的服務
另外一種在 Linux 系統上列出運行的服務的方法是使用 initctl 命令:
# initctl listrc stop/waitingtty (/dev/tty3) start/running, process 1740tty (/dev/tty2) start/running, process 1738tty (/dev/tty1) start/running, process 1736tty (/dev/tty6) start/running, process 1746tty (/dev/tty5) start/running, process 1744tty (/dev/tty4) start/running, process 1742plymouth-shutdown stop/waitingcontrol-alt-delete stop/waitingrcS-emergency stop/waitingreadahead-collector stop/waitingkexec-disable stop/waitingquit-plymouth stop/waitingrcS stop/waitingprefdm stop/waitinginit-system-dbus stop/waitingck-log-system-restart stop/waitingreadahead stop/waitingck-log-system-start stop/waitingsplash-manager stop/waitingstart-ttys stop/waitingreadahead-disable-services stop/waitingck-log-system-stop stop/waitingrcS-sulogin stop/waitingserial stop/waiting
方法三:如何在 systemd 系統中查看運行的服務
以下命令幫助我們列出 systemd 系統中所有服務:
# systemctlUNIT LOAD ACTIVE SUB DESCRIPTIONsys-devices-virtual-block-loop0.device loaded active plugged /sys/devices/virtual/block/loop0sys-devices-virtual-block-loop1.device loaded active plugged /sys/devices/virtual/block/loop1sys-devices-virtual-block-loop2.device loaded active plugged /sys/devices/virtual/block/loop2sys-devices-virtual-block-loop3.device loaded active plugged /sys/devices/virtual/block/loop3sys-devices-virtual-block-loop4.device loaded active plugged /sys/devices/virtual/block/loop4sys-devices-virtual-misc-rfkill.device loaded active plugged /sys/devices/virtual/misc/rfkillsys-devices-virtual-tty-ttyprintk.device loaded active plugged /sys/devices/virtual/tty/ttyprintksys-module-fuse.device loaded active plugged /sys/module/fusesys-subsystem-net-devices-enp0s3.device loaded active plugged 82540EM Gigabit Ethernet Controller (PRO/1000 MT Desktop Adapter)-.mount loaded active mounted Root Mountdev-hugepages.mount loaded active mounted Huge Pages File Systemdev-mqueue.mount loaded active mounted POSIX Message Queue File Systemrun-user-1000-gvfs.mount loaded active mounted /run/user/1000/gvfsrun-user-1000.mount loaded active mounted /run/user/1000snap-core-3887.mount loaded active mounted Mount unit for coresnap-core-4017.mount loaded active mounted Mount unit for coresnap-core-4110.mount loaded active mounted Mount unit for coresnap-gping-13.mount loaded active mounted Mount unit for gpingsnap-termius\x2dapp-8.mount loaded active mounted Mount unit for termius-appsys-fs-fuse-connections.mount loaded active mounted FUSE Control File Systemsys-kernel-debug.mount loaded active mounted Debug File Systemacpid.path loaded active running ACPI Events Checkcups.path loaded active running CUPS Schedulersystemd-ask-password-plymouth.path loaded active waiting Forward Password Requests to Plymouth Directory Watchsystemd-ask-password-wall.path loaded active waiting Forward Password Requests to Wall Directory Watchinit.scope loaded active running System and Service Managersession-c2.scope loaded active running Session c2 of user magiaccounts-daemon.service loaded active running Accounts Serviceacpid.service loaded active running ACPI event daemonanacron.service loaded active running Run anacron jobsapache2.service loaded active running The Apache HTTP Serverapparmor.service loaded active exited AppArmor initializationapport.service loaded active exited LSB: automatic crash report generationaptik-battery-monitor.service loaded active running LSB: start/stop the aptik battery monitor daemonatop.service loaded active running Atop advanced performance monitoratopacct.service loaded active running Atop process accounting daemonavahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stackcolord.service loaded active running Manage, Install and Generate Color Profilesconsole-setup.service loaded active exited Set console font and keymapcron.service loaded active running Regular background program processing daemoncups-browsed.service loaded active running Make remote CUPS printers available locallycups.service loaded active running CUPS Schedulerdbus.service loaded active running D-Bus System Message Buspostfix.service loaded active exited Postfix Mail Transport Agent
UNIT相應的 systemd 單元名稱LOAD相應的單元是否被加載到內存中ACTIVE該單元是否處於活動狀態SUB該單元是否處於運行狀態(LCTT 譯注:是較於 ACTIVE 更加詳細的狀態描述,不同的單元類型有不同的狀態。)DESCRIPTION關於該單元的簡短描述
以下選項可根據類型列出單元:
# systemctl list-units --type serviceUNIT LOAD ACTIVE SUB DESCRIPTIONaccounts-daemon.service loaded active running Accounts Serviceacpid.service loaded active running ACPI event daemonanacron.service loaded active running Run anacron jobsapache2.service loaded active running The Apache HTTP Serverapparmor.service loaded active exited AppArmor initializationapport.service loaded active exited LSB: automatic crash report generationaptik-battery-monitor.service loaded active running LSB: start/stop the aptik battery monitor daemonatop.service loaded active running Atop advanced performance monitoratopacct.service loaded active running Atop process accounting daemonavahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stackcolord.service loaded active running Manage, Install and Generate Color Profilesconsole-setup.service loaded active exited Set console font and keymapcron.service loaded active running Regular background program processing daemoncups-browsed.service loaded active running Make remote CUPS printers available locallycups.service loaded active running CUPS Schedulerdbus.service loaded active running D-Bus System Message Busfwupd.service loaded active running Firmware update daemongetty@tty1.service loaded active running Getty on tty1grub-common.service loaded active exited LSB: Record successful boot for GRUBirqbalance.service loaded active running LSB: daemon to balance interrupts for SMP systemskeyboard-setup.service loaded active exited Set the console keyboard layoutkmod-static-nodes.service loaded active exited Create list of required static device nodes for the current kernel
以下選項可幫助您根據狀態列出單位,輸出與前例類似但更直截了當:
# systemctl list-unit-files --type serviceUNIT FILE STATEaccounts-daemon.service enabledacpid.service disabledalsa-restore.service staticalsa-state.service staticalsa-utils.service maskedanacron-resume.service enabledanacron.service enabledapache-htcacheclean.service disabledapache-htcacheclean@.service disabledapache2.service enabledapache2@.service disabledapparmor.service enabledapport-forward@.service staticapport.service generatedapt-daily-upgrade.service staticapt-daily.service staticaptik-battery-monitor.service generatedatop.service enabledatopacct.service enabledautovt@.service enabledavahi-daemon.service enabledbluetooth.service enabled
運行以下命令以查看指定服務的狀態:
# systemctl | grep apache2apache2.service loaded active running The Apache HTTP Server
或者,使用以下命令也可查看指定服務的狀態:
# systemctl status apache2● apache2.service - The Apache HTTP ServerLoaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)Drop-In: /lib/systemd/system/apache2.service.d└─apache2-systemd.confActive: active (running) since Tue 2018-03-06 12:34:09 IST; 8min agoProcess: 2786 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS)Main PID: 1171 (apache2)Tasks: 55 (limit: 4915)CGroup: /system.slice/apache2.service├─1171 /usr/sbin/apache2 -k start├─2790 /usr/sbin/apache2 -k start└─2791 /usr/sbin/apache2 -k startMar 06 12:34:08 magi-VirtualBox systemd[1]: Starting The Apache HTTP Server...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 suppreMar 06 12:34:09 magi-VirtualBox systemd[1]: Started The Apache HTTP Server.Mar 06 12:39:10 magi-VirtualBox systemd[1]: Reloading The Apache HTTP Server.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 glMar 06 12:39:10 magi-VirtualBox systemd[1]: Reloaded The Apache HTTP Server.
執行以下命令,只查看正在運行的服務:
# systemctl | grep runningacpid.path loaded active running ACPI Events Checkcups.path loaded active running CUPS Schedulerinit.scope loaded active running System and Service Managersession-c2.scope loaded active running Session c2 of user magiaccounts-daemon.service loaded active running Accounts Serviceacpid.service loaded active running ACPI event daemonapache2.service loaded active running The Apache HTTP Serveraptik-battery-monitor.service loaded active running LSB: start/stop the aptik battery monitor daemonatop.service loaded active running Atop advanced performance monitoratopacct.service loaded active running Atop process accounting daemonavahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stackcolord.service loaded active running Manage, Install and Generate Color Profilescron.service loaded active running Regular background program processing daemoncups-browsed.service loaded active running Make remote CUPS printers available locallycups.service loaded active running CUPS Schedulerdbus.service loaded active running D-Bus System Message Busfwupd.service loaded active running Firmware update daemongetty@tty1.service loaded active running Getty on tty1irqbalance.service loaded active running LSB: daemon to balance interrupts for SMP systemslightdm.service loaded active running Light Display ManagerModemManager.service loaded active running Modem ManagerNetworkManager.service loaded active running Network Managerpolkit.service loaded active running Authorization Manager
使用以下命令查看系統啟動時會被啟用的服務列表:
# systemctl list-unit-files | grep enabledacpid.path enabledcups.path enabledaccounts-daemon.service enabledanacron-resume.service enabledanacron.service enabledapache2.service enabledapparmor.service enabledatop.service enabledatopacct.service enabledautovt@.service enabledavahi-daemon.service enabledbluetooth.service enabledconsole-setup.service enabledcron.service enabledcups-browsed.service enabledcups.service enableddisplay-manager.service enableddns-clean.service enabledfriendly-recovery.service enabledgetty@.service enabledgpu-manager.service enabledkeyboard-setup.service enabledlightdm.service enabledModemManager.service enablednetwork-manager.service enablednetworking.service enabledNetworkManager-dispatcher.service enabledNetworkManager-wait-online.service enabledNetworkManager.service enabled
systemd-cgtop 按資源使用情況(任務、CPU、內存、輸入和輸出)列出控制組:
# systemd-cgtopControl Group Tasks %CPU Memory Input/s Output/s/ - - 1.5G - -/init.scope 1 - - - -/system.slice 153 - - - -/system.slice/ModemManager.service 3 - - - -/system.slice/NetworkManager.service 4 - - - -/system.slice/accounts-daemon.service 3 - - - -/system.slice/acpid.service 1 - - - -/system.slice/apache2.service 55 - - - -/system.slice/aptik-battery-monitor.service 1 - - - -/system.slice/atop.service 1 - - - -/system.slice/atopacct.service 1 - - - -/system.slice/avahi-daemon.service 2 - - - -/system.slice/colord.service 3 - - - -/system.slice/cron.service 1 - - - -/system.slice/cups-browsed.service 3 - - - -/system.slice/cups.service 2 - - - -/system.slice/dbus.service 6 - - - -/system.slice/fwupd.service 5 - - - -/system.slice/irqbalance.service 1 - - - -/system.slice/lightdm.service 7 - - - -/system.slice/polkit.service 3 - - - -/system.slice/repowerd.service 14 - - - -/system.slice/rsyslog.service 4 - - - -/system.slice/rtkit-daemon.service 3 - - - -/system.slice/snapd.service 8 - - - -/system.slice/system-getty.slice 1 - - - -
同時,我們可以使用 pstree 命令(輸出來自 SysVinit 系統)查看正在運行的服務:
# pstreeinit-+-crond|-httpd---2*[httpd]|-kthreadd/99149---khelper/99149|-2*[mingetty]|-mysqld_safe---mysqld---9*[{mysqld}]|-rsyslogd---3*[{rsyslogd}]|-saslauthd---saslauthd|-2*[sendmail]|-sshd---sshd---bash---pstree|-udevd`-xinetd
我們還可以使用 pstree 命令(輸出來自 systemd 系統)查看正在運行的服務:
# pstreesystemd─┬─ModemManager─┬─{gdbus}│ └─{gmain}├─NetworkManager─┬─dhclient│ ├─{gdbus}│ └─{gmain}├─accounts-daemon─┬─{gdbus}│ └─{gmain}├─acpid├─agetty├─anacron├─apache2───2*[apache2───26*[{apache2}]]├─aptd───{gmain}├─aptik-battery-m├─atop├─atopacctd├─avahi-daemon───avahi-daemon├─colord─┬─{gdbus}│ └─{gmain}├─cron├─cups-browsed─┬─{gdbus}│ └─{gmain}├─cupsd├─dbus-daemon├─fwupd─┬─{GUsbEventThread}│ ├─{fwupd}│ ├─{gdbus}│ └─{gmain}├─gnome-keyring-d─┬─{gdbus}│ ├─{gmain}│ └─{timer}
方法四:如何使用 chkservice 在 systemd 系統中查看正在運行的服務
chkservice 是一個管理系統單元的終端工具,需要超級用戶權限。
# chkservice

要查看幫助頁面,請按下 ? ,它將顯示管理 systemd 服務的可用選項。

via: https://www.2daygeek.com/how-to-check-all-running-services-in-linux/
作者:Magesh Maruthamuthu 譯者:jessie-pang 校對:wxy
jessie-pang 🌟🌟🌟
編譯自:https://www.2daygeek.com/how-to-check-all-running-services-in-linux/ 作者: Magesh Maruthamuthu
原創:LCTT https://linux.cn/article-9918-1.html 譯者: jessie-pang
本文由 LCTT 原創翻譯,Linux中國首發。也想加入譯者行列,為開源做一些自己的貢獻么?歡迎加入 LCTT!
翻譯工作和譯文發表僅用於學習和交流目的,翻譯工作遵照 CC-BY-NC-SA 協議規定,如果我們的工作有侵犯到您的權益,請及時聯系我們。
歡迎遵照 CC-BY-NC-SA 協議規定轉載,敬請在正文中標注並保留原文/譯文鏈接和作者/譯者等信息。
文章僅代表作者的知識和看法,如有不同觀點,請樓下排隊吐槽 :D


