00. 目录
01. 命令概述
systemctl命令是系统服务管理器指令,它实际上将 service
和chkconfig
这两个命令组合到一起。
Centos7之后从init完全换成了systemd的启动方式,systemd 启动服务的机制主要是通过 systemctl 的这个系统服务管理指令来处理。systemctl在用法上也囊括 service / chkconfig / setup / init 的大部分功能。
任务 | 旧指令 | 新指令 |
---|---|---|
使某服务自动启动 | chkconfig --level 3 httpd on | systemctl enable httpd.service |
使某服务不自动启动 | chkconfig --level 3 httpd off | systemctl disable httpd.service |
检查服务状态 | service httpd status | systemctl status httpd.service (服务详细信息) systemctl is-enabled httpd.service (仅显示是否 Active) |
显示所有已启动的服务 | chkconfig --list | systemctl list-units --type=service |
启动某服务 | service httpd start | systemctl start httpd.service |
停止某服务 | service httpd stop | systemctl stop httpd.service |
重启某服务 | service httpd restart | systemctl restart httpd.service |
某服务重新加载配置文件 | service httpd reload | systemctl reload httpd.service |
02. 命令格式
格式:systemctl [选项...] {命令} ...
03. 常用选项
-start 启动服务
-stop 停止服务
-restart 重启服务
-enable 使某服务开机自启
-disable 关闭某服务开机自启
-status 查看服务状态
-list-units –type=service 列举所有已启动服务
04. 参考示例
4.1 输出所有已经启动服务
[deng@localhost ~]$ systemctl
4.2 输出所有已经启动的单元
[deng@localhost ~]$ systemctl list-units
4.3 显示启动失败的服务
[deng@localhost ~]$ systemctl --failed
UNIT LOAD ACTIVE SUB DESCRIPTION
● network.service loaded failed failed LSB: Bring up/down networking
● postfix.service loaded failed failed Postfix Mail Transport Agent
4.4 查看所有已经安装的服务
[deng@localhost ~]$ systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-fs-nfsd.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
4.5 启动服务
[root@localhost ~]# systemctl start sshd
4.6 停止服务
[root@localhost ~]# systemctl stop sshd
4.7 重启服务
[root@localhost ~]# systemctl restart sshd
4.8 重新加载服务配置
[root@localhost ~]# systemctl reload sshd
4.9 查看服务状态
[root@localhost ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since 六 2019-08-17 20:56:41 CST; 1min 10s ago
4.10 查看服务是否开机启动
[root@localhost ~]# systemctl is-enabled sshd
enabled
[root@localhost ~]#
4.11 开机自动启动服务
[root@localhost ~]# systemctl enable sshd
[root@localhost ~]#
4.12 开机不启动服务
[root@localhost ~]# systemctl disable sshd
Removed symlink /etc/systemd/system/multi-user.target.wants/sshd.service.
[root@localhost ~]#
4.13 显示服务帮助信息
[root@localhost ~]# systemctl help sshd
4.14 重新加载systemd
[root@localhost ~]# systemctl daemon-reload
4.15 重新启动系统
[root@localhost ~]# systemctl reboot
4.16 关机
[root@localhost ~]# systemctl poweroff
4.17 休眠
[root@localhost ~]# systemctl hibernate
hibernate:休眠模式则是将系统状态保存到硬盘当中,保存完毕后,将计算机关机。当用户尝试唤醒系统时,系统会开始正常运行,然后将保存在硬盘中的系统状态恢复回来。因为数据需要从硬盘读取,因此唤醒的速度比较慢(如果你使用的是 SSD 磁盘,唤醒的速度也是非常快的)。
4.18 待机
[root@localhost ~]# systemctl suspend
suspend:暂停模式会将系统的状态保存到内存中,然后关闭掉大部分的系统硬件,当然,并没有实际关机。当用户按下唤醒机器的按钮,系统数据会从内存中回复,然后重新驱动被大部分关闭的硬件,所以唤醒系统的速度比较快。
4.19 混合休眠模式(同时休眠到硬盘并待机)
[root@localhost ~]# systemctl hybrid-sleep
4.20 相当于telinit 3 或 telinit 5
[root@localhost ~]# systemctl isolate graphical.target
graphical.target:就是文字界面再加上图形界面,这个 target 已经包含了下面的 multi-user.target。
multi-user.target:纯文本模式!
rescue.target:在无法使用 root 登陆的情况下,systemd 在开机时会多加一个额外的临时系统,与你原本的系统无关。这时你可以取得 root 的权限来维护你的系统。
emergency.target:紧急处理系统的错误,在无法使用 rescue.target 时,可以尝试使用这种模式!
shutdown.target:就是执行关机。
getty.target:可以设置 tty 的配置。
正常的模式是 multi-user.target 和 graphical.target 两个,救援方面的模式主要是 rescue.target 以及更严重的 emergency.target。如果要修改可提供登陆的 tty 数量,则修改 getty.target。
4.21 列出所有已经启动的服务
[root@localhost ~]# systemctl list-units --type=service
4.22 查看目前是否运行指定服务
[root@localhost ~]# systemctl is-active sshd
active
[root@localhost ~]#
4.23 列出unit的配置
[root@localhost ~]# systemctl show
4.24 查看服务的配置
[root@localhost ~]# systemctl show sshd.service
4.25 注销服务
[root@localhost ~]# systemctl mask sshd.service
Created symlink from /etc/systemd/system/sshd.service to /dev/null.
[root@localhost ~]#
4.26 反注销服务
[root@localhost ~]# systemctl unmask sshd.service
Removed symlink /etc/systemd/system/sshd.service.
[root@localhost ~]#
4.27 只查看服务类型的unit
[root@localhost ~]# systemctl list-units --type=service --all
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coredump hoo
abrt-oops.service loaded active running ABRT kernel log watcher
abrt-vmcore.service loaded inactive dead Harvest vmcores for ABRT
abrt-xorg.service loaded active running ABRT Xorg log watcher
abrtd.service loaded active running ABRT Automated Bug Report
accounts-daemon.service loaded active running Accounts Service
alsa-restore.service loaded inactive dead Save/Restore Sound Card S
alsa-state.service loaded active running Manage Sound Card State (
● apparmor.service not-found inactive dead apparmor.service
4.28 进入救援模式
[root@localhost ~]# systemctl rescue
4.29 进入紧急救援模式
[root@localhost ~]# systemctl emergency
4.30 本机 socket 的服务
[root@localhost ~]# systemctl list-sockets
05. 附录
参考:https://blog.csdn.net/skh2015java/article/details/94012643