Red Hat/CentOS检查并列出运行服务Linux命令
如何在Fedora/RHEL/CentOS Linux服务器中列出所有当前正在运行的服务?
如何使用基于systemd的CentOS/RHEL 7.x和RHEL/CentOS 8.x检查服务的状态?
在Fedora/RHEL/CentOS Linux系统下,有多种方法和工具可以找到并列出所有正在运行的服务。
红帽/CentOS检查并列出运行服务命令
请注意,基于Systemd的系统(例如CentOS/RHEL 7.x/8.x和最新版本的fedora)使用systemctl命令列出正在运行的服务
在CentOS/RHEL 6.x或更早版本上使用service命令列出正在运行的服务
对于CentOS/RHEL 6.x和更早的版本(预系统),语法如下:
service --status-all service --status-all | more service --status-all | grep ntpd service --status-all | less
打印任何服务的状态
要打印apache(httpd)服务的状态:
service httpd status
sshd服务的显示状态:
service sshd status
列出所有已知服务(通过SysV配置)
chkconfig --list
列出服务及其开放端口
netstat -tulpn
开启/关闭服务
ntsysv chkconfig service off chkconfig service on chkconfig httpd off chkconfig ntpd on
ntsysv是用于配置运行级别服务的简单界面,这些服务也可以通过chkconfig进行配置。
默认情况下,它配置当前运行级别。
只需键入ntsysv并选择要运行的服务即可。
使用systemctl(RHEL/CentOS 7.x/8.x)的Red Hat/CentOS列表运行服务
如果您正在使用基于系统的Linux发行版,例如Fedora Linux v22/23/24/26/27/28/29/30/31或RHEL/CentOS Linux 7.x/8.x。
尝试以下命令以使用systemctl命令列出正在运行的服务。
它控制系统的系统和服务管理器。
要列出CentOS/RHEL 7.x +上的系统服务,请使用
语法为:
systemctl systemctl | more systemctl | grep httpd systemctl list-units --type service systemctl list-units --type mount
列出所有服务:
systemctl list-unit-files
输出示例:
列出安装在基于CentOS/RHEL 7 systemd的系统上的所有单元及其当前状态
要查看与特定服务(cgroup)关联的进程,可以使用systemd-cgtop命令。
像top命令一样,systemd-cgtop根据其服务列出正在运行的进程:
systemd-cgtop
输出示例:
Path Tasks %CPU Memory Input/s Output/s / 85 0.3 240.1M - /system.slice/NetworkManager.service 2 - - - /system.slice/auditd.service 1 - - - /system.slice/crond.service 1 - - - /system.slice/dbus.service 1 - - - /system.slice/lvm2-lvmetad.service 1 - - - /system.slice/polkit.service 1 - - - /system.slice/postfix.service 3 - - - /system.slice/...tty.slice/[email protected] 1 - - - /system.slice/systemd-journald.service 1 - - - /system.slice/systemd-logind.service 1 - - - /system.slice/systemd-udevd.service 1 - - - /system.slice/tuned.service 1 - - - /system.slice/wpa_supplicant.service 1 - - - /user.slice/user-0.slice/session-2.scope 1 - - - /user.slice/user-1000.slice/session-1.scope 4 - - -
要仅在CentOS/RHEL 7.x +上列出SysV服务,请使用(不包括本机systemd服务)
chkconfig --list
输出示例:
在systemd上列出基于Sysv的服务
如何使用systemd检查服务状态
假设您要检查名为sshd的服务的状态,请运行
$ systemctl status sshd.service
验证服务状态
可以验证在CentOS/RHEL 7.x +上名为sshd的服务是否正在运行(活动)或未运行(不活动):
systemctl is-active sshd systemctl is-enabled sshd systemctl is-active nginx systemctl is-enabled nginx systemctl is-enabled httpd
列出RHEL/CentOS 8.x服务
我们使用以下命令查找有关当前加载的服务:
$ sudo systemctl list-units --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION _____________________________________________________________________________________________________ console-getty.service loaded active running Console Getty crond.service loaded active running Command Scheduler dbus.service loaded active running D-Bus System Message Bus dracut-shutdown.service loaded active exited Restore /run/initramfs on shutdown NetworkManager-wait-online.service loaded active exited Network Manager Wait Online NetworkManager.service loaded active running Network Manager rsyslog.service loaded active running System Logging Service systemd-journal-flush.service loaded active exited Flush Journal to Persistent Storage systemd-tmpfiles-setup-dev.service loaded active exited Create Static Device Nodes in /dev systemd-tmpfiles-setup.service loaded active exited Create Volatile Files and Directories systemd-udev-trigger.service loaded active exited udev Coldplug all Devices systemd-udevd.service loaded active running udev Kernel Device Manager systemd-update-utmp.service loaded active exited Update UTMP about System Boot/Shutdown systemd-user-sessions.service loaded active exited Permit User Sessions LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type. 18 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'.
键入systemctl命令,不显示任何选项,以显示已加载的和活动的单元/服务:
$ sudo systemctl
使用grep命令/egrep命令过滤掉:
$ sudo systemct | egerep 'httpd|php-fpm|mysqld|nginx' $ sudo systemctl list-units --type=service | grep nginx
列出服务/单元依赖性
语法为:
# systemctl list-dependencies {service-name} # systemctl list-dependencies nginx.service # systemctl list-dependencies php-fpm.service # systemctl list-dependencies sshd.service # systemctl list-dependencies crond.service
crond.service ● ├─system.slice ● └─sysinit.target ● ├─dev-hugepages.mount ● ├─dev-mqueue.mount ● ├─dracut-shutdown.service ● ├─import-state.service ● ├─kmod-static-nodes.service ● ├─ldconfig.service ● ├─loadmodules.service ● ├─proc-sys-fs-binfmt_misc.automount ● ├─selinux-autorelabel-mark.service ● ├─sys-fs-fuse-connections.mount ● ├─local-fs.target ● │ └─systemd-remount-fs.service ● └─swap.target