使用 Systemctl 命令 来管理系统服务

使用systemctl,可以启动、停止、重新加载、重启服务、列出服务单元、检查服务状态、启用/禁用服务、管理运行级别和电源管理。在本文中将展示如何在Linux中使用systemctl命令来管理systemd服务。
systemctl start [service-name]
。例如,启动firewalld服务:[root@localhost ~]# systemctl start firewalld
service
命令相反,systemctl start命令不输出任何内容。
systemctl stop [service-name]
。例如,停止firewalld服务:[root@localhost ~]# systemctl stop firewalld

systemctl restart [service-name]
,例如:[root@localhost ~]# systemctl restart firewalld

systemctl reload [service-name]
,例如:[root@localhost ~]# systemctl reload sshd

systemctl status [service-name]
来查看。[root@localhost ~]# systemctl status firewalld

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.

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

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

enabled
表示开机时启动该服务,disabled
表示开机时不启动该服务。list-units
选项。[root@localhost ~]# systemctl list-units

[root@localhost ~]# systemctl list-units -t service

poweroff
、shutdown
命令一样,systemctl命令可以关闭系统,重启或进入休眠状态。[root@localhost ~]# systemctl poweroff
[root@localhost ~]# systemctl reboot
[root@localhost ~]# systemctl hibernate
[root@localhost ~]# systemctl status httpd -H root@192.168.0.12

-H
选项,指定远程主机的用户名和密码。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

[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.

[root@localhost ~]# systemctl list-units -t target

[root@localhost ~]# journalctl

journalctl -b
[root@localhost ~]# journalctl -b
[root@localhost ~]# journalctl -f

[root@localhost ~]# systemd-analyzeStartup finished in 497ms (kernel) + 1.836s (initrd) + 6.567s (userspace) = 8.901s
<以上代码可复制粘贴,可往左滑>

[root@localhost ~]# systemd-analyze blame

[root@localhost ~]# hostnamectl

END
赞 (0)