使用Systemctl命令来管理系统服务
Systemctl是systemd用于管理系统和管理服务的工具。许多现代Linux发行版,如Ubuntu、Debian、Fedora、Linux Mint、OpenSuSE、Redhat都采用systemd作为默认的init系统。
使用systemctl,可以启动、停止、重新加载、重启服务、列出服务单元、检查服务状态、启用/禁用服务、管理运行级别和电源管理。在本文中将展示如何在Linux中使用systemctl命令来管理systemd服务。使用systemctl命令 Start/Stop/Restart/Reload 服务
使用systemctl启动服务时,命令格式:systemctl start 。例如,启动firewalld服务:# systemctl start firewalld与以前老版本的linux中的service命令相反,systemctl start命令不输出任何内容。
https://www.linuxprobe.com/wp-content/uploads/2020/12/image1-7.png
要停止服务,请使用systemctl stop 。例如,停止firewalld服务:# systemctl stop firewalldhttps://www.linuxprobe.com/wp-content/uploads/2020/12/image2-7.png
要重新启动服务,请使用systemctl restart ,例如:# systemctl restart firewalldhttps://www.linuxprobe.com/wp-content/uploads/2020/12/image3-7.png
要重新加载服务的配置(例如ssh)而不重新启动它,请使用systemctl reload ,例如:# systemctl reload sshdhttps://www.linuxprobe.com/wp-content/uploads/2020/12/image4-7.pngsystemctl检查服务状态
为了查看服务是否正在运行,我们可以使用systemctl status 来查看。# systemctl status firewalldhttps://www.linuxprobe.com/wp-content/uploads/2020/12/image5-6.png检查服务是否设置为开机启动
要在引导时启用服务,请使用systemctl enable ,例如:# systemctl enable httpd.service Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.https://www.linuxprobe.com/wp-content/uploads/2020/12/image6-6.png
同样,disable时取消引导时启用服务:# systemctl disable httpd.service https://www.linuxprobe.com/wp-content/uploads/2020/12/image7-4.png
可以使用is-enabled选项检查开机是否启动该服务,请运行:# systemctl is-enabled httpd.service https://www.linuxprobe.com/wp-content/uploads/2020/12/image8-2.png
输出的内容enabled表示开机时启动该服务,disabled表示开机时不启动该服务。systemctl列出单元
要列出所有激活的单元,使用list-units选项。# systemctl list-unitshttps://www.linuxprobe.com/wp-content/uploads/2020/12/image9-2.png
要列出所有活动的服务,请运行:# systemctl list-units -t service https://www.linuxprobe.com/wp-content/uploads/2020/12/image10-2.png使用systemctl重启、关机系统
像poweroff、shutdown命令一样,systemctl命令可以关闭系统,重启或进入休眠状态。关机:# systemctl poweroff 重启:# systemctl reboot 系统休眠:# systemctl hibernate使用systemclt管理远程系统
通常,上述所有systemctl命令都可以用于通过systemctl命令本身管理远程主机。这将使用ssh与远程主机进行通信。如下所示:# systemctl status httpd -H root@192.168.0.12https://www.linuxprobe.com/wp-content/uploads/2020/12/image11-2.png
-H选项,指定远程主机的用户名和密码。管理Targets
Systemd具有Targets的概念,这些Targets的目的与sysVinit系统中的运行级别相似。sysVinit中的运行级别主要是数字(0,1,2,-6)。以下是sysVinit中的运行级别及其对应的systemd中的target:0 runlevel0.target, poweroff.target1runlevel1.target, rescue.target2,3,4 runlevel2.target, runlevel3.target,runlevel4.target, multi-user.target5 runlevel5.target, graphical.target6 runlevel6.target, reboot.target如果想要查看当前的运行级别,可以使用如下命令:# systemctl get-default multi-user.targethttps://www.linuxprobe.com/wp-content/uploads/2020/12/image12-2.png
设置默认的运行级别为graphical,命令如下:# systemctl set-default graphical.target Removed symlink /etc/systemd/system/default.target.Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.https://www.linuxprobe.com/wp-content/uploads/2020/12/image13-2.png
想要列出所有激活的target,可以使用下面命令:# systemctl list-units -t targethttps://www.linuxprobe.com/wp-content/uploads/2020/12/image14-1.pngsystemd工具的其他命令
journalctl日志收集
systemd有自己的日志系统,称为journald。它替换了sysVinit中的syslogd。# journalctl https://www.linuxprobe.com/wp-content/uploads/2020/12/image15-1.png
要查看所有引导消息,请运行命令journalctl -b# journalctl -b以下命令实时跟踪系统日志(类似于tail -f):# journalctl -fhttps://www.linuxprobe.com/wp-content/uploads/2020/12/image16-1.png查询系统启动过程的持续时间
# systemd-analyzeStartup finished in 497ms (kernel) + 1.836s (initrd) + 6.567s (userspace) = 8.901shttps://www.linuxprobe.com/wp-content/uploads/2020/12/image17-1.png
最后显示系统启动时间为8.901秒。查看服务的启动时间:# systemd-analyze blamehttps://www.linuxprobe.com/wp-content/uploads/2020/12/image18-1.pnghostnamectl命令
查看主机名称:# hostnamectl https://www.linuxprobe.com/wp-content/uploads/2020/12/image19.png总结
在本文学习了systemctl命令来管理Linux发行版中的系统服务。本文原创地址:https://www.linuxprobe.com/systemctl-manage-service.html编辑:逄增宝,审核员:逄增宝
谢谢分享
页:
[1]