Linux centos7系统列出systemd下所有正在运行的服务
Linux系统提供各种系统服务(如进程管理、登录、syslog、cron等)和网络服务。
Linux支持不同的方法来管理服务(启动、停止、重启、在系统启动时的自动启动等),通常通过流程或服务管理器。
大多数现代Linux发行版现在使用相同的进程管理器:systemd。
systemctl命令是管理systemd的主要工具
Systemd是Linux的系统和服务管理器;
init进程的替代品,与SysV和LSB init脚本兼容。
systemctl将显示所有已加载的systemd单元
(有关systemd单元的更多信息,请阅读systemd文档)的列表,包括服务,并显示它们的状态(是否处于活动状态)
systemct
要列出系统上所有已加载的服务(无论是活动的,运行的,退出的还是失败的,请使用 list-units子命令和--type开关,其值为service。
//键入systemctl -- ##补充tab键就会出现下面的参数列表
[root@etcd2 conf]# systemctl -- --after --full --no-block --quiet --system --all --global --no-legend --recursive --type --before --help --no-pager --reverse --version --defaults --host --no-reload --root --fail --ignore-dependencies --no-wall --runtime --failed --kill-who --privileged --signal --force --no-ask-password --property --state [root@etcd2 conf]# systemctl -- --after --full --no-block --quiet --system --all --global --no-legend --recursive --type --before --help --no-pager --reverse --version --defaults --host --no-reload --root --fail --ignore-dependencies --no-wall --runtime --failed --kill-who --privileged --signal --force --no-ask-password --property --state
//tab键补全就会显示下面的命令列表
[root@etcd2 conf]# systemctl list-
list-dependencies list-jobs list-sockets list-timers list-unit-files list-units
[root@etcd2 conf]# systemctl --version
systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
[root@ht8 ~]# systemctl list-units --type=service UNIT LOAD ACTIVE SUB DESCRIPTION abrt-ccpp.service loaded active exited Install ABRT coredump hook abrt-oops.service loaded active running ABRT kernel log watcher abrtd.service loaded active running ABRT Automated Bug Reporting Tool atd.service loaded active running Job spooling tools auditd.service loaded active running Security Auditing Service calico-node.service loaded active running calico node ● cloud-set-guest-password.service loaded failed failed SYSV: Password Download Client crond.service loaded active running Command Scheduler dbus.service loaded active running D-Bus System Message Bus docker.service loaded active running Docker Application Container Engine getty@tty1.service loaded active running Getty on tty1 gssproxy.service loaded active running GSSAPI Proxy Daemon irqbalance.service loaded active running irqbalance daemon kmod-static-nodes.service loaded active exited Create list of required static device nodes for the current kernel kube-proxy.service loaded active running Kubernetes Kube-Proxy Server kubelet.service loaded active running Kubernetes Kubelet Server libstoragemgmt.service loaded active running libstoragemgmt plug-in server daemon lvm2-lvmetad.service loaded active running LVM2 metadata daemon lvm2-monitor.service loaded active exited Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress p lvm2-pvscan@8:0.service loaded active exited LVM2 PV scan on device 8:0 lvm2-pvscan@8:18.service loaded active exited LVM2 PV scan on device 8:18 network.service loaded active exited LSB: Bring up/down networking NetworkManager-wait-online.service loaded active exited Network Manager Wait Online NetworkManager.service loaded active running Network Manager 或者 [root@ht8 ~]# systemctl --type=service UNIT LOAD ACTIVE SUB DESCRIPTION abrt-ccpp.service loaded active exited Install ABRT coredump hook abrt-oops.service loaded active running ABRT kernel log watcher abrtd.service loaded active running ABRT Automated Bug Reporting Tool atd.service loaded active running Job spooling tools auditd.service loaded active running Security Auditing Service calico-node.service loaded active running calico node ● cloud-set-guest-password.service loaded failed failed SYSV: Password Download Client crond.service loaded active running Command Scheduler dbus.service loaded active running D-Bus System Message Bus docker.service loaded active running Docker Application Container Engine getty@tty1.service loaded active running Getty on tty1 gssproxy.service loaded active running GSSAPI Proxy Daemon irqbalance.service loaded active running irqbalance daemon kmod-static-nodes.service loaded active exited Create list of required static device nodes for the current kernel kube-proxy.service loaded active running Kubernetes Kube-Proxy Server kubelet.service loaded active running Kubernetes Kubelet Server libstoragemgmt.service loaded active running libstoragemgmt plug-in server daemon lvm2-lvmetad.service loaded active running LVM2 metadata daemon lvm2-monitor.service loaded active exited Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress p lvm2-pvscan@8:0.service loaded active exited LVM2 PV scan on device 8:0 lvm2-pvscan@8:18.service loaded active exited LVM2 PV scan on device 8:18 network.service loaded active exited LSB: Bring up/down networking NetworkManager-wait-online.service loaded active exited Network Manager Wait Online NetworkManager.service loaded active running Network Manager lines 1-25
要列出所有已加载但处于活动状态的服务,包括正在运行的服务和已退出的服务,可以添加--state选项,其值为active,如下所示。
[root@ht8 ~]# systemctl list-units --type=service --state=active UNIT LOAD ACTIVE SUB DESCRIPTION abrt-ccpp.service loaded active exited Install ABRT coredump hook abrt-oops.service loaded active running ABRT kernel log watcher abrtd.service loaded active running ABRT Automated Bug Reporting Tool atd.service loaded active running Job spooling tools auditd.service loaded active running Security Auditing Service calico-node.service loaded active running calico node crond.service loaded active running Command Scheduler dbus.service loaded active running D-Bus System Message Bus docker.service loaded active running Docker Application Container Engine getty@tty1.service loaded active running Getty on tty1 gssproxy.service loaded active running GSSAPI Proxy Daemon irqbalance.service loaded active running irqbalance daemon kmod-static-nodes.service loaded active exited Create list of required static device nodes for the current kernel kube-proxy.service loaded active running Kubernetes Kube-Proxy Server kubelet.service loaded active running Kubernetes Kubelet Server libstoragemgmt.service loaded active running libstoragemgmt plug-in server daemon lvm2-lvmetad.service loaded active running LVM2 metadata daemon lvm2-monitor.service loaded active exited Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress pol lvm2-pvscan@8:0.service loaded active exited LVM2 PV scan on device 8:0 lvm2-pvscan@8:18.service loaded active exited LVM2 PV scan on device 8:18 network.service loaded active exited LSB: Bring up/down networking NetworkManager-wait-online.service loaded active exited Network Manager Wait Online NetworkManager.service loaded active running Network Manager nginx-proxy.service loaded active running kubernetes apiserver docker wrapper lines 1-25
如果要快速浏览所有正在运行的服务(即所有已加载和正在运行的服务),请运行以下命令。
systemctl命令 说明 systemctl 列出所有的系统服务 [root@etcd2 system]# systemctl UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automoun sys-devices-pci0000:00-0000:00:07.1-ata1-host0-target0:0:0-0:0:0:0-block-sr0.device loaded active plugged VMware_Virtual_IDE_CDROM sys-devices-pci0000:00-0000:00:07.1-ata1-host0-target0:0:1-0:0:1:0-block-sda-sda1.device loaded active plugged VMware_Virtual_IDE_ sys-devices-pci0000:00-0000:00:07.1-ata1-host0-target0:0:1-0:0:1:0-block-sda-sda2.device loaded active plugged LVM PV dJcKYm-10WN- sys-devices-pci0000:00-0000:00:07.1-ata1-host0-target0:0:1-0:0:1:0-block-sda.device loaded active plugged VMware_Virtual_IDE_Hard_ sys-devices-pci0000:00-0000:00:15.0-0000:03:00.0-host2-port\x2d2:0-end_device\x2d2:0-target2:0:0-2:0:0:0-block-sdb.device loaded act .... //列出所有启动unit [root@etcd2 system]# systemctl list-units UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automoun sys-devices-pci0000:00-0000:00:07.1-ata1-host0-target0:0:0-0:0:0:0-block-sr0.device loaded active plugged VMware_Virtual_IDE_CDROM sys-devices-pci0000:00-0000:00:07.1-ata1-host0-target0:0:1-0:0:1:0-block-sda-sda1.device loaded active plugged VMware_Virtual_IDE_ sys-devices-pci0000:00-0000:00:07.1-ata1-host0-target0:0:1-0:0:1:0-block-sda-sda2.device loaded active plugged LVM PV dJcKYm-10WN- ..... //列出所有启动文件 [root@etcd2 system]# systemctl list-unit-files UNIT FILE STATE proc-sys-fs-binfmt_misc.automount static dev-hugepages.mount static dev-mqueue.mount static proc-fs-nfsd.mount static proc-sys-fs-binfmt_misc.mount static sys-fs-fuse-connections.mount static sys-kernel-config.mount static sys-kernel-debug.mount static tmp.mount disabled var-lib-nfs-rpc_pipefs.mount static brandbot.path disabled systemd-ask-password-console.path static systemd-ask-password-plymouth.path static systemd-ask-password-wall.path static session-153.scope static ... // 列出所有service类型的unit [root@etcd2 system]# systemctl list-units --type=service --all UNIT LOAD ACTIVE SUB DESCRIPTION abrt-ccpp.service loaded active exited Install ABRT coredump hook abrt-oops.service loaded active running ABRT kernel log watcher abrt-vmcore.service loaded inactive dead Harvest vmcores for ABRT abrt-xorg.service loaded inactive dead ABRT Xorg log watcher abrtd.service loaded active running ABRT Automated Bug Reporting Tool atd.service loaded active running Job spooling tools auditd.service loaded active running Security Auditing Service auth-rpcgss-module.service loaded inactive dead Kernel Module supporting RPCSEC_GSS brandbot.service loaded inactive dead Flexible Branding Service calico-node.service loaded active running calico node ● cloud-set-guest-password.service loaded failed failed SYSV: Password Download Client ... //列出 kube-proxy的服务 [root@etcd2 system]# systemctl list-units --type=service --all | grep kube-proxy kube-proxy.service loaded active running Kubernetes Kube-Proxy Server //列出所有target [root@etcd2 system]# systemctl list-units --type=target --all UNIT LOAD ACTIVE SUB DESCRIPTION basic.target loaded active active Basic System cryptsetup.target loaded active active Encrypted Volumes emergency.target loaded inactive dead Emergency Mode final.target loaded inactive dead Final Step getty.target loaded active active Login Prompts graphical.target loaded inactive dead Graphical Interface local-fs-pre.target loaded active active Local File Systems (Pre) local-fs.target loaded active active Local File Systems multi-user.target loaded active active Multi-User System .... [root@etcd2 system]# systemctl list-units --type=service --all UNIT LOAD ACTIVE SUB DESCRIPTION abrt-ccpp.service loaded active exited Install ABRT coredump hook abrt-oops.service loaded active running ABRT kernel log watcher abrt-vmcore.service loaded inactive dead Harvest vmcores for ABRT abrt-xorg.service loaded inactive dead ABRT Xorg log watcher abrtd.service loaded active running ABRT Automated Bug Reporting Tool atd.service loaded active running Job spooling tools [root@ht8 ~]# systemctl list-units --type=service --state=running UNIT LOAD ACTIVE SUB DESCRIPTION abrt-oops.service loaded active running ABRT kernel log watcher abrtd.service loaded active running ABRT Automated Bug Reporting Tool atd.service loaded active running Job spooling tools auditd.service loaded active running Security Auditing Service calico-node.service loaded active running calico node crond.service loaded active running Command Scheduler dbus.service loaded active running D-Bus System Message Bus docker.service loaded active running Docker Application Container Engine getty@tty1.service loaded active running Getty on tty1 gssproxy.service loaded active running GSSAPI Proxy Daemon irqbalance.service loaded active running irqbalance daemon kube-proxy.service loaded active running Kubernetes Kube-Proxy Server kubelet.service loaded active running Kubernetes Kubelet Server libstoragemgmt.service loaded active running libstoragemgmt plug-in server daemon lvm2-lvmetad.service loaded active running LVM2 metadata daemon NetworkManager.service loaded active running Network Manager nginx-proxy.service loaded active running kubernetes apiserver docker wrapper ntpd.service loaded active running Network Time Service polkit.service loaded active running Authorization Manager postfix.service loaded active running Postfix Mail Transport Agent rngd.service loaded active running Hardware RNG Entropy Gatherer Daemon rpcbind.service loaded active running RPC bind service rsyslog.service loaded active running System Logging Service loaded active running Network Time Service
一、service常用管理,kube-proxy服务为例
k8s注册到系统的服务一般有 kube-apiserver.service kube-controller-manager.service kubelet.service kube-proxy.service kube-scheduler.service nginx-proxy.service //cni网络 calico-node.service //引擎 docker.service //时间对于很多应用,例如:打卡,社交等必须的 ntpd.service ntpdate.service 下面是常用操作 // 开机启动 [root@ht23 net.d]#systemctl enable kube-proxy.service //关闭开机启动 [root@ht23 net.d]#systemctl disable kube-proxy.service //启动服务 [root@ht23 net.d]#systemctl start kube-proxy.service //停止服务 [root@ht23 net.d]#systemctl stop kube-proxy.service // 重启服务 [root@ht23 net.d]#systemctl restart kube-proxy.service
//查看服务状态
[root@ht23 net.d]# systemctl status kube-proxy.service
//查看服务是否激活状态 [root@ht23 net.d]#systemctl is-active kube-proxy.service active //查看是否开机启动 [root@ht23 net.d]#systemctl is-enabled kube-proxy.service enabled //杀死服务进程 systemctl kill kube-proxy.service
二、服务启动的配置文件,下面以自建的kube-proxy.service为例
配置文件主要都放在 /usr/lib/systemd/system 或/lib/systemd/system 目录.
[root@ht23 k8snode]# ls /usr/lib/systemd/system abrt-ccpp.service lvm2-lvmpolld.service runlevel2.target abrtd.service lvm2-lvmpolld.socket runlevel2.target.wants abrt-oops.service lvm2-monitor.service runlevel3.target abrt-pstoreoops.service lvm2-pvscan@.service runlevel3.target.wants abrt-vmcore.service machine.slice runlevel4.target abrt-xorg.service machines.target runlevel4.target.wants arp-ethers.service mdadm-grow-continue@.service runlevel5.target
......
kube-proxy.service配置文件举例:
# 查看 kube-proxy.service 服务对应的启动配置文件
[root@ht23 net.d]# systemctl cat kube-proxy.service
# /usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Kube-Proxy Server
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=network.target
[Service]
EnvironmentFile=-/etc/kubernetes/conf/config
EnvironmentFile=-/etc/kubernetes/conf/proxy
ExecStart=/usr/local/bin/hyperkube proxy \
$KUBE_LOGTOSTDERR \
$KUBE_LOG_LEVEL \
$KUBE_MASTER \
$KUBE_PROXY_ARGS
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target //WantedBy:表示该服务所在的 Target(服务组)
//保存后
cp 文件--->/usr/lib/systemd/system 或/lib/systemd/system下
systemctl daemon-reload //重载配置文件
Service启动配置文件讲解
EnvironmentFile:许多软件都有自己的环境参数文件,该字段指定文件路径 注意:/etc/profile 或者 /etc/profile.d/ 这些文件中配置的环境变量仅对通过 pam 登录的用户生效,而 systemd 是不读这些配置的。 systemd 是所有进程的父进程或祖先进程,它的环境变量会被所有的子进程所继承,如果需要给 systemd 配置默认参数可以在 /etc/systemd/system.conf 和 /etc/systemd/user.conf 中设置。
加载优先级 system.conf 最低,可能会被其他的覆盖。 Type:定义启动类型。可设置:simple,exec,forking,oneshot,dbus,notify,idle simple(设置了 ExecStart= 但未设置 BusName= 时的默认值):ExecStart 字段启动的进程为该服务的主进程 forking:ExecStart 字段的命令将以 fork() 方式启动,此时父进程将会退出,子进程将成为主进程 ExecStart:定义启动进程时执行的命令 上面的例子中,启动 sshd 执行的命令是 /usr/sbin/sshd -D $OPTIONS,其中的变量 $OPTIONS 就来自 EnvironmentFile 字段指定的环境参数文件。类似的,还有如下字段: ExecReload: 重启服务时执行的命令 ExecStop: 停止服务时执行的命令 ExecStartPre: 启动服务之前执行的命令 ExecStartPost:启动服务之后执行的命令 ExecStopPost: 停止服务之后执行的命令 RemainAfterExit:设为yes,表示进程退出以后,服务仍然保持执行 KillMode:定义 Systemd 如何停止服务,可以设置的值如下: control-group(默认值):当前控制组里面的所有子进程,都会被杀掉 process:只杀主进程 mixed:主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号 none:没有进程会被杀掉,只是执行服务的 stop 命令 Restart:定义了退出后,Systemd 的重启方式。可以设置的值如下: no(默认值):退出后不会重启 on-success:只有正常退出时(退出状态码为0),才会重启 on-failure:非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启 on-abnormal:只有被信号终止和超时,才会重启 on-abort:只有在收到没有捕捉到的信号终止时,才会重启 on-watchdog:超时退出,才会重启 always:不管是什么退出原因,总是重启 RestartSec:表示 Systemd 重启服务之前,需要等待的秒数
关于 Target,运行级别
# 查看默认 Target
[root@etcd2 system]# systemctl get-default
multi-user.target
multi-user.target表示默认的启动Target是multi-user.target。在这个组里的所有服务,都将开机启动。这就是为什么 systemctl enable 命令能设置开机启动的原因
# 查看 multi-user.target 包含的所有服务
[root@etcd2 system]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─abrt-ccpp.service
● ├─abrt-oops.service
● ├─abrt-vmcore.service
● ├─abrt-xorg.service
● ├─abrtd.service
● ├─atd.service
● ├─auditd.service
● ├─brandbot.path
● ├─calico-node.service
● ├─cloud-set-guest-password.service
● ├─crond.service
● ├─dbus.service
● ├─docker.service
● ├─etcd.service
● ├─irqbalance.service
● ├─kube-apiserver.service
......
# shutdown.target表示关机状态的
[root@etcd2 system]# systemctl list-dependencies shutdown.target
shutdown.target
● └─dracut-shutdown.service
# 常用的 Target 有两个:一个是 multi-user.target,表示多用户命令行状态;另一个是 graphical.target,表示图形用户状态,
[root@etcd2 system]# systemctl list-dependencies graphical.target //它依赖于 multi-user.target
graphical.target
● ├─cloud-set-guest-password.service
● ├─display-manager.service
● ├─network.service
● ├─systemd-update-utmp-runlevel.service
● ├─vmware-tools.service
● └─multi-user.target
● ├─abrt-ccpp.service
● ├─abrt-oops.service
● ├─abrt-vmcore.service
● ├─abrt-xorg.service
● ├─abrtd.service
● ├─atd.service
● ├─auditd.service
● ├─brandbot.path
● ├─calico-node.service
● ├─cloud-set-guest-password.service
● ├─crond.service
● ├─dbus.service
● ├─docker.service
● ├─etcd.service
● ├─irqbalance.service
● ├─kube-apiserver.service
● ├─kube-controller-manager.service
● ├─kube-proxy.service
.....
[root@etcd2 system]# systemctl status shutdown.target
● shutdown.target - Shutdown
Loaded: loaded (/usr/lib/systemd/system/shutdown.target; static; vendor preset: disabled)
Active: inactive (dead)
Docs: man:systemd.special(7)
这里补充下Linux下的7个运行级别:
0:系统停机状态,系统默认运行级别不能设置为0,否则不能正常启动,机器关闭。 1:单用户工作状态,root权限,用于系统维护,禁止远程登陆,就像Windows下的安全模式登录。 2:多用户状态,没有NFS支持。 3:完整的多用户模式,有NFS,登陆后进入控制台命令行模式。 4:系统未使用,保留一般不用,在一些特殊情况下可以用它来做一些事情。例如在笔记本电脑的电池用尽时,可以切换到这个模式来做一些设置。 5:X11控制台,登陆后进入图形GUI模式,XWindow系统。 6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动。运行init6机器就会重启。
标准的Linux运行级别为3或5
运行级别原理:
1.在目录/etc/rc.d/init.d下有许多服务器脚本程序,一般称为服务(service) 2.在/etc/rc.d下有9个文件,rc{n}.d对应系统的7个运行级别
[root@etcd2 system]# cat /etc/rc.d/
init.d/ rc0.d/ rc1.d/ rc2.d/ rc3.d/ rc4.d/ rc5.d/ rc6.d/ rc.local
3.rc{n}.d目录下都是一些符号链接文件,这些链接文件都指向init.d目录下的service脚本文件,命名规则为K+nn+服务名或S+nn+服务名,其中nn为两位数字。 4.系统会根据指定的运行级别进入对应的rcN.d目录,并按照文件名顺序检索目录下的链接文件:对于以K(Kill)开头的文件,系统将终止对应的服;对于以S(Start)开头的文件,系统将启动对应的服务 5.查看运行级别用:runlevel 6.进入其它运行级别用:initN,如果init3则进入终端模式,init5则又登录图形GUI模式 7.另外init0为关机,init6为重启系统
标准的Linux运行级别为3或5,如果是3的话,系统就在多用户状态;如果是5的话,则是运行着XWindow系统。不同的运行级别有不同的用处,也应该根据自己的不同情形来设置。例如,如果丢失了root口令,那么可以让机器启动进入单用户状态来设置。在启动后的lilo提示符下输入: init=/bin/shrw
这样就可以使机器进入运行级别1,并把root文件系统挂为读写。它会路过所有系统认证,让你使用passwd程序来改变root口令,然后启动到一个新的运行级。
建完配置文件设置自启动
# 添加或修改配置文件后,需要重新加载 systemctl daemon-reload # 设置自启动,实质就是在 /etc/systemd/system/multi-user.target.wants/ 添加服务文件的链接 systemctl enable zdy
其他请查看官方手册 https://www.freedesktop.org/software/systemd/man/systemd.service.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类