这篇文章主要介绍了systemctl命令如何使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇systemctl命令如何使用文章都会有所收获,下面我们一起来看看吧。

systemd是Linux系统最新的初始化系统(init),作用是提高系统的启动速度,尽可能启动较少的进程,尽可能更多进程并发启动,systemd对应的进程管理命令是systemctl。

使用systemctl命令 Start/Stop/Restart/Reload 服务

使用systemctl启动服务时,命令格式:systemctl start [service-name]。例如,启动firewalld服务:

[root@localhost~]#systemctlstartfirewalld

与以前老版本的linux中的service命令相反,systemctl start命令不输出任何内容。

要停止服务,请使用systemctl stop [service-name]。例如,停止firewalld服务:

[root@localhost~]#systemctlstopfirewalld

要重新启动服务,请使用systemctl restart [service-name],例如:

[root@localhost~]#systemctlrestartfirewalld

要重新加载服务的配置(例如ssh)而不重新启动它,请使用systemctl reload [service-name],例如:

[root@localhost~]#systemctlreloadsshd

systemctl检查服务状态

为了查看服务是否正在运行,我们可以使用systemctl status [service-name]来查看。

[root@localhost~]#systemctlstatusfirewalld

检查服务是否设置为开机启动

要在引导时启用服务,请使用systemctl enable [service-name],例如:

[root@localhost~]#systemctlenablehttpd.serviceCreatedsymlinkfrom/etc/systemd/system/multi-user.target.wants/httpd.serviceto/usr/lib/systemd/system/httpd.service.

同样,disable时取消引导时启用服务:

[root@localhost~]#systemctldisablehttpd.service

可以使用is-enabled选项检查开机是否启动该服务,请运行:

[root@localhost~]#systemctlis-enabledhttpd.service

输出的内容enabled表示开机时启动该服务,disabled表示开机时不启动该服务。

systemctl列出单元

要列出所有激活的单元,使用list-units选项。

[root@localhost~]#systemctllist-units

要列出所有活动的服务,请运行:

[root@localhost~]#systemctllist-units-tservice

使用systemctl重启、关机系统

poweroffshutdown命令一样,systemctl命令可以关闭系统,重启或进入休眠状态。

关机:

[root@localhost~]#systemctlpoweroff

重启:

[root@localhost~]#systemctlreboot

系统休眠:

[root@localhost~]#systemctlhibernate使用systemclt管理远程系统

通常,上述所有systemctl命令都可以用于通过systemctl命令本身管理远程主机。这将使用ssh与远程主机进行通信。如下所示:

[root@localhost~]#systemctlstatushttpd-Hroot@192.168.0.12

-H选项,指定远程主机的用户名和密码。

管理Targets

Systemd具有Targets的概念,这些Targets的目的与sysVinit系统中的运行级别相似。sysVinit中的运行级别主要是数字(0,1,2,-6)。以下是sysVinit中的运行级别及其对应的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

设置默认的运行级别为graphical,命令如下:

[root@localhost~]#systemctlset-defaultgraphical.targetRemovedsymlink/etc/systemd/system/default.target.Createdsymlinkfrom/etc/systemd/system/default.targetto/usr/lib/systemd/system/graphical.target.

想要列出所有激活的target,可以使用下面命令:

[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命令如何使用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“systemctl命令如何使用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。