良许Linux教程网 干货合集 通过Systemctl命令管理系统服务

通过Systemctl命令管理系统服务

systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。

Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。

通过Systemctl命令管理系统服务

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

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

[root@localhost ~]# systemctl start firewalld

与以前老版本的linux中的service命令相反,systemctl start命令不输出任何内容。 使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务 要停止服务,请使用systemctl stop [service-name]。例如,停止firewalld服务:

[root@localhost ~]# systemctl stop firewalld

使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务 要重新启动服务,请使用systemctl restart [service-name],例如:

[root@localhost ~]# systemctl restart firewalld

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

[root@localhost ~]# systemctl reload sshd
使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务

systemctl检查服务状态

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

[root@localhost ~]# systemctl status firewalld
使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务

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

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

[root@localhost ~]# systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务 同样,disable时取消引导时启用服务:

[root@localhost ~]# systemctl disable httpd.service

使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务 可以使用is-enabled选项检查开机是否启动该服务,请运行:

[root@localhost ~]# systemctl is-enabled httpd.service

使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务 输出的内容enabled表示开机时启动该服务,disabled表示开机时不启动该服务。

systemctl列出单元

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

[root@localhost ~]# systemctl list-units

使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务 要列出所有活动的服务,请运行:

[root@localhost ~]# systemctl list-units -t service
使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务

使用systemctl重启、关机系统

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

关机:

[root@localhost ~]# systemctl poweroff

重启:

[root@localhost ~]# systemctl reboot

系统休眠:

[root@localhost ~]# systemctl hibernate

使用systemclt管理远程系统

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

[root@localhost ~]# systemctl status httpd -H root@192.168.0.12

使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务-H选项,指定远程主机的用户名和密码。

管理Targets

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

0   runlevel0.target, poweroff.target
1  runlevel1.target, rescue.target
2,3,4 runlevel2.target, runlevel3.target,runlevel4.target, multi-user.target
5   runlevel5.target, graphical.target
6   runlevel6.target, reboot.target

如果想要查看当前的运行级别,可以使用如下命令:

[root@localhost ~]# systemctl get-default
multi-user.target

使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务 设置默认的运行级别为graphical,命令如下:

[root@localhost ~]# 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.

使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务 想要列出所有激活的target,可以使用下面命令:

[root@localhost ~]# systemctl list-units -t target
使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务

systemd工具的其他命令

journalctl日志收集

systemd有自己的日志系统,称为journald。它替换了sysVinit中的syslogd。

[root@localhost ~]# journalctl

使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务 要查看所有引导消息,请运行命令journalctl -b

[root@localhost ~]# journalctl -b

以下命令实时跟踪系统日志(类似于tail -f):

[root@localhost ~]# journalctl -f
使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务

查询系统启动过程的持续时间

[root@localhost ~]# systemd-analyze
Startup finished in 497ms (kernel) + 1.836s (initrd) + 6.567s (userspace) = 8.901s

使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务 最后显示系统启动时间为8.901秒。

查看服务的启动时间:

[root@localhost ~]# systemd-analyze blame
使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务

hostnamectl命令

查看主机名称:

[root@localhost ~]# hostnamectl
使用Systemctl命令来管理系统服务使用Systemctl命令来管理系统服务

以上就是良许教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

1ad5ad6eddc451da964a8888a6fd5266d0163210
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部