作者:ANNIE炫可_315 | 来源:互联网 | 2023-05-19 10:00
在Centos中systemctl是设置系统服务的命令,即service,它融合之前service和chkconfig的功能于一体。可以使用它永久性或只在当前会话中启用禁用服务
在Centos 中 systemctl 是设置系统服务的命令,即 service , 它融合之前service和chkconfig的功能于一体。
可以使用它永久性或只在当前会话中启用/禁用服务。
CentOS 7 的 /etc/rc.d/rc.local 是没有执行权限的, 系统建议创建 systemd service 启动服务。
![](https://img1.php1.cn/3cd4a/24de0/696/c5538437adbbb427.jpeg)
查看服务列表状态:
systemctl list-units --type=service
![](https://img1.php1.cn/3cd4a/24de0/696/31ea394f40b284ac.jpeg)
systemctl list-unit-files 列出所有已经安装的 服务 及 状态 (可为人所读, 内容简略、清晰):
![](https://img1.php1.cn/3cd4a/24de0/696/2bcb78950cdfc430.jpeg)
systemctl 可以列出正在运行的服务状态,如图:(基本不为人所读, 内容复杂、全面)
![](https://img1.php1.cn/3cd4a/24de0/696/5a920f4114e951ea.jpeg)
systemd-cgls 以树形列出正在运行的进程,它可以递归显示控制组内容。如图:
![](https://img1.php1.cn/3cd4a/24de0/696/bb3e8a73a9b40122.jpeg)
![](https://img1.php1.cn/3cd4a/24de0/696/60a89526d27ceaab.jpeg)
2、如何启动/关闭、启用/禁用服务?
启动一个服务:
systemctl start postfix.service
关闭一个服务:
systemctl stop postfix.service
重启一个服务:
systemctl restart postfix.service
显示一个服务的状态:
systemctl status postfix.service
在开机时启用一个服务:systemctl enable postfix.service
在开机时禁用一个服务:systemctl disable postfix.service
查看服务是否开机启动: systemctl is-enabled postfix.service
查看已启动的服务列表: systemctl list-unit-files | grep enabled
查看启动失败的服务列表: systemctl --failed
PS:使用命令 systemctl is-enabled postfix.service 得到的值可以是enable、disable或static,这里的 static 它是指对应的 Unit 文件中没有定义[Install]区域,因此无法配置为开机启动服务。
说明:启用服务就是在当前“runlevel”的配置文件目录 /etc/systemd/system/multi-user.target.wants 里,建立 /usr/lib/systemd/system 里面对应服务配置文件的软链接;
禁用服务就是删除此软链接,添加服务就是添加软连接。
如图:
![](https://img1.php1.cn/3cd4a/24de0/696/71eb6eee0859cef0.jpeg)
![](https://img1.php1.cn/3cd4a/24de0/696/d05d9e3b41befbc0.jpeg)
打开某个 service 文件, 查看一下其中内容:
![](https://img1.php1.cn/3cd4a/24de0/696/62602cb8240caf78.jpeg)
![](https://img1.php1.cn/3cd4a/24de0/696/67bcfe3819dc25d5.jpeg)