当前位置: 首页 > 科技观察

使用Systemctl命令管理系统服务

时间:2023-03-17 10:54:16 科技观察

Systemctl是systemd的管理系统和管理服务的工具。许多现代Linux发行版,例如Ubuntu、Debian、Fedora、LinuxMint、OpenSuSE、Redhat使用systemd作为默认的初始化系统。使用systemctl,您可以启动、停止、重新加载、重新启动服务、列出服务单元、检查服务状态、启用/禁用服务、管理运行级别和电源管理。在本文中,我将展示如何使用systemctl命令在Linux中管理systemd服务。*使用systemctl命令Start/Stop/Restart/Reloadservice使用systemctl启动服务时,命令格式:systemctlstart[service-name]。例如启动firewalld服务:[root@localhost~]#systemctlstartfirewalld<以上代码可以复制粘贴,可以向左滑动>与老版本Linux中的service命令相反,systemctlstart命令做不输出任何东西。要停止服务,请使用systemctlstop[service-name]。例如停止firewalld服务:[root@localhost~]#systemctlstopfirewalld<以上代码可复制粘贴,可向左滑动>重启服务请使用systemctlrestart[service-name],例如:[root@localhost~]#systemctlrestartfirewalld<以上代码可以复制粘贴,可以向左滑动>不重启的情况下重新加载某个服务(如ssh)的配置,请使用systemctlreload[service-name],对于示例:[root@localhost~]#systemctlreloadsshd<以上代码可复制粘贴,向左滑动>*systemctl查看服务状态。要检查服务是否正在运行,我们可以使用systemctlstatus[service-name]来检查。[root@localhost~]#systemctlstatusfirewalld<以上代码可复制粘贴,可向左滑动>*检查服务是否设置为开机自启动开机时启用服务,请使用systemctlenable[service-name],例如:[root@localhost~]#systemctlenablehttpd.serviceCreatedsymlinkfrom/etc/systemd/system/multi-user.target.wants/httpd.serviceto/usr/lib/systemd/system/httpd.service.<以上代码可复制粘贴,可向左滑动>同理,禁用时取消开机时启用服务:[root@localhost~]#systemctldisablehttpd.service<以上代码可复制粘贴,即可向左滑动>启动后可以使用is-enabled选项查看服务是否启动,请运行:[root@localhost~]#systemctlis-enabledhttpd.service<以上代码可以复制粘贴,你可以向左滑动>输出内容enabled表示开机即启动服务,disabled表示开机时服务没有启动。*systemctllistunits要列出所有活动单元,请使用list-units选项。[root@localhost~]#systemctllist-units<上面的代码可以复制粘贴,可以向左滑动>要列出所有活跃的服务,请运行:[root@localhost~]#systemctllist-units-tservice<上面的代码可以复制粘贴,可以向左滑动>*使用systemctl重启和关闭系统与poweroff和shutdown命令一样,systemctl命令可以关闭系统、重启或进入休眠状态。关机:[root@localhost~]#systemctlpoweroff<以上代码可复制粘贴,可向左滑动>重启:[root@localhost~]#systemctlreboot<以上代码可复制粘贴,可向左滑动>系统休眠:[root@localhost~]#systemctlhibernate<以上代码可复制粘贴,可向左滑动>*使用systemclt管理远程系统通常,以上所有的systemctl命令都可以用来管理远程主机通过systemctl命令本身。这将使用ssh与远程主机通信。如下图:[root@localhost~]#systemctlstatushttpd-Hroot@192.168.0.12<以上代码可复制粘贴,可向左滑动>-H选项,指定远程主机的用户名和密码。*管理TargetsSystemd有Targets的概念,这些Targets的作用类似于sysVinit系统中的运行级别。sysVinit中的运行级别主要是数字(0,1,2,-6)。以下是sysVinit中的runlevels及其在systemd中对应的target:0runlevel0.target,poweroff.target1runlevel1.target,rescue.target2,3,4runlevel2.target,runlevel3.target,runlevel4.target,multi-user.target5runlevel5。target,graphical.target6runlevel6.target,reboot.target<以上代码可以复制粘贴,可以向左滑动>如果想查看当前运行级别,可以使用如下命令:[root@localhost~]#systemctlget-defaultmulti-user。target<以上代码可复制粘贴,可向左滑动>设置默认运行级别为图形化,命令如下:[root@localhost~]#systemctlset-defaultgraphical.targetRemovedsymlink/etc/systemd/system/default.target.Createdsymlinkfrom/etc/systemd/system/default.targetto/usr/lib/systemd/system/graphical.target.<以上代码可复制粘贴,可向左滑动>列出所有激活的targets,可以使用如下命令:[root@localhost~]#systemctllist-units-ttarget<以上代码可复制粘贴,向左滑动即可>*systemd工具的其他命令journalctl日志收集systemd有它自己的日志系统称为journald。它取代了sysVinit中的syslogd。[root@localhost~]#journalctl<以上代码可复制粘贴,可向左滑动>查看所有开机信息,请运行命令journalctl-b[root@localhost~]#journalctl-b下面的命令实时跟踪系统日志(类似tail-f):[root@localhost~]#journalctl-f<上面的代码可以复制并粘贴,可以向左滑动>查询系统启动过程的持续时间[root@localhost~]#systemd-analyzeStartupfinishedin497ms(kernel)+1.836s(initrd)+6.567s(userspace)=8.901s<以上代码可以复制粘贴,可以向左滑动>系统启动时间最终显示为8.901秒。查看服务启动时间:[root@localhost~]#systemd-analyzeblame<以上代码可复制粘贴,可向左滑动>hostnamectl命令查看主机名:[root@localhost~]#hostnamectl<以上代码可复制粘贴,可向左滑动>*小结在本文中,您学习了systemctl命令来管理Linux发行版中的系统服务。希望能帮到你。本文转载自微信公众号《Linux就该这么学》,可以通过以下二维码关注。转载本文,请联系Linux像这样学习公众号。