systemctl命令概述与功能分类
systemctl作为一套集系统管理守护进程、工具与库于一体的解决方案,旨在替代原有的System V、service及chkconfig命令。其核心职责在于控制systemd系统与服务管理器。通过执行systemctl –help可查看其功能分类,主要包括:向systemd发送查询或控制指令、管理单元服务、处理服务文件相关事务、操作任务环境与快照、重载systemd配置,以及控制系统开关机流程。
单元与服务状态查询及管理
1. 列出所有可用单元
# systemctl list-unit-files
2. 列出所有正在运行的单元
# systemctl list-units
3. 列出所有处于失败状态的单元
# systemctl –failed
4. 检查特定单元(例如crond.service)是否已启用
# systemctl is-enabled crond.service
5. 列出所有服务
# systemctl list-unit-files –type=service
6. Linux中如何启动、重启、停止、重载服务并检查服务(如httpd.service)的状态
# systemctl start httpd.service
# systemctl restart httpd.service
# systemctl stop httpd.service
# systemctl reload httpd.service
# systemctl status httpd.service
注意:当使用systemctl的start、restart、stop和reload命令时,终端通常无输出;唯有status命令会打印详细状态信息。
7. 如何激活服务并在开机时启用或禁用服务(即设置系统在启动时自动启动mysql.service)
# systemctl is-active mysql.service
# systemctl enable mysql.service
# systemctl disable mysql.service
服务生命周期控制与屏蔽操作
8. 如何屏蔽(禁止启动)或取消屏蔽服务(如ntpdate.service)
# systemctl mask ntpdate.service
ln -s ‘/dev/null”/etc/systemd/system/ntpdate.service'
# systemctl unmask ntpdate.service
rm ‘/etc/systemd/system/ntpdate.service'
系统挂载点与套接口管理
9. 使用systemctl命令终止服务
# systemctl kill crond
10. 列出所有系统挂载点
# systemctl list-unit-files –type=mount
11. 挂载、卸载、重新挂载、重载系统挂载点,并检查其状态
# systemctl start tmp.mount
# systemctl stop tmp.mount
# systemctl restart tmp.mount
# systemctl reload tmp.mount
# systemctl status tmp.mount
12. 在启动时激活、启用或禁用挂载点(实现系统启动时自动挂载)
# systemctl is-active tmp.mount
# systemctl enable tmp.mount
# systemctl disable tmp.mount
13. 在Linux中屏蔽(禁止启用)或显示挂载点
# systemctl mask tmp.mount
ln -s ‘/dev/null”/etc/systemd/system/tmp.mount'
# systemctl unmask tmp.mount
rm ‘/etc/systemd/system/tmp.mount'
服务配置详情与依赖关系查看
14. 列出所有可用的系统套接口
# systemctl list-unit-files –type=socket
15. 检查某个服务的所有配置细节
# systemctl show mysql
系统救援、紧急模式与运行等级切换
16. 获取某个服务(httpd)的依赖项列表
# systemctl list-dependencies httpd.service
17. 启动救援模式
# systemctl rescue
18. 进入紧急模式
# systemctl emergency
19. 列出当前使用的运行等级
# systemctl get-default
20. 启动运行等级5,即图形界面模式
# systemctl isolate runlevel5.target
或者
# systemctl isolate graphical.target
21. 启动运行等级3,即多用户命令行模式
# systemctl isolate runlevel3.target
或者
# systemctl isolate multiuser.target
22. 设置多用户模式或图形模式为默认运行等级
# systemctl set-default runlevel3.target
# systemctl set-default runlevel5.target
23. 重启、停止、挂起、休眠系统,或使系统进入混合睡眠状态
# systemctl reboot
# systemctl halt
# systemctl suspend
# systemctl hibernate
# systemctl hybrid-sleep
对于不熟悉运行等级的用户,说明如下: Runlevel 0 : 关闭系统 Runlevel 1 : 救援与维护模式 Runlevel 3 : 多用户模式,无图形界面 Runlevel 4 : 多用户模式,无图形界面 Runlevel 5 : 多用户模式,带图形界面 Runlevel 6 : 关闭并重启机器