news 2026/2/16 11:38:37

常见Systemctl语句

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
常见Systemctl语句

常见Systemctl语句

一、服务启动与停止

1. 启动服务

启动指定服务单元(基本用法)。

systemctl start nginx.service

2. 停止服务

停止运行中的服务(stop)。

systemctl stop nginx.service

3. 重启服务

重启服务,如果未运行则启动(restart)。

systemctl restart nginx.service

4. 重新加载配置

重新加载服务配置而不重启(reload)。

systemctl reload nginx.service

5. 尝试重启

仅在服务已运行时重启(try-restart)。

systemctl try-restart nginx.service

6. 尝试重新加载或重启

优先重新加载,失败则重启(try-reload-or-restart)。

systemctl try-reload-or-restart nginx.service

7. 隔离服务

隔离到指定单元,停止其他服务(isolate)。

systemctl isolate nginx.service

8. 紧急停止

立即杀死服务所有进程(kill --signal=9)。

systemctl kill --signal=9 nginx.service

9. 条件启动

如果服务未运行则启动(condrestart,脚本化)。

systemctl status nginx.service >/dev/null || systemctl start nginx.service

10. 批量启动

批量启动多个服务(start)。

systemctl start nginx.service mysql.service

二、服务启用与禁用

11. 启用服务

启用服务开机自启(enable)。

systemctl enable nginx.service

12. 禁用服务

禁用服务开机自启(disable)。

systemctl disable nginx.service

13. 重新启用

重新启用已启用的服务(reenable)。

systemctl reenable nginx.service

14. 掩码服务

掩码服务防止启动(mask)。

systemctl mask nginx.service

15. 取消掩码

取消服务掩码(unmask)。

systemctl unmask nginx.service

16. 启用并立即启动

启用并立即启动服务(enable --now)。

systemctl enable --now nginx.service

17. 禁用并立即停止

禁用并立即停止服务(disable --now)。

systemctl disable --now nginx.service

18. 链接单元

链接外部单元文件(link)。

systemctl link /path/to/custom.service

19. 预设启用

恢复预设启用状态(preset)。

systemctl preset nginx.service

20. 预设所有

预设所有单元到默认状态(preset-all)。

systemctl preset-all

三、系统状态与查询

21. 查看服务状态

查看服务详细状态(status)。

systemctl status nginx.service

22. 列出所有单元

列出所有加载的单元(list-units)。

systemctl list-units

23. 列出失败单元

列出失败的单元(list-units --failed)。

systemctl list-units --failed

24. 检查服务启用

检查服务是否启用(is-enabled)。

systemctl is-enabled nginx.service

25. 检查服务激活

检查服务是否激活(is-active)。

systemctl is-active nginx.service

26. 检查服务失败

检查服务是否失败(is-failed)。

systemctl is-failed nginx.service

27. 列出单元文件

列出所有单元文件状态(list-unit-files)。

systemctl list-unit-files

28. 列出依赖

列出单元的依赖关系(list-dependencies)。

systemctl list-dependencies nginx.service

29. 树状依赖

树状显示依赖(list-dependencies --plain)。

systemctl list-dependencies --plain nginx.service

30. 系统状态摘要

显示系统状态摘要(show)。

systemctl show

四、日志与调试

31. 查看服务日志

查看服务日志(journalctl -u)。

journalctl -u nginx.service

32. 实时监控日志

实时监控服务日志(-f)。

journalctl -u nginx.service -f

33. 过滤日志级别

过滤错误级别日志(-p err)。

journalctl -u nginx.service -p err

34. 日志时间过滤

过滤特定时间段日志(--since --until)。

journalctl -u nginx.service --since "2025-10-20" --until "2025-10-21"

35. 合并日志输出

合并多个单元日志(-u unit1 -u unit2)。

journalctl -u nginx.service -u mysql.service

36. 日志输出格式

以JSON格式输出日志(-o json)。

journalctl -u nginx.service -o json

37. 跟踪服务进程

跟踪服务进程树(-p)。

systemctl status nginx.service -l

38. 重置失败计数

重置服务失败计数(reset-failed)。

systemctl reset-failed nginx.service

39. 日志大小限制

查看日志大小(journalctl --disk-usage)。

journalctl --disk-usage

40. 清空旧日志

清空旧日志到指定大小(--vacuum-size)。

journalctl --vacuum-size=100M

五、高级配置与资源控制

41. 编辑单元覆盖

编辑服务覆盖配置(edit)。

systemctl edit nginx.service

42. 恢复单元配置

恢复单元默认配置(revert)。

systemctl revert nginx.service

43. 设置环境变量

设置服务环境变量覆盖(set-property)。

systemctl set-property nginx.service Environment="VAR=value"

44. 设置CPU配额

设置服务CPU配额(set-property CPUQuota)。

systemctl set-property nginx.service CPUQuota=50%

45. 设置内存限制

设置服务内存限制(set-property MemoryMax)。

systemctl set-property nginx.service MemoryMax=512M

46. 实例化单元

启动模板实例化服务(@instance)。

systemctl start myapp@web.service

47. 套接字激活

启用套接字激活单元(enable)。

systemctl enable myapp.socket

48. 用户模式服务

用户模式下启动服务(--user)。

systemctl --user start user-service.service

49. 系统引导目标

切换到多用户目标(default.target)。

systemctl isolate multi-user.target

50. 分析引导时间

分析系统引导时间(analyze)。

systemd-analyze blame
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/14 11:49:32

5分钟生成电影级运镜:Motion LoRA技术让静态图片“动“起来

5分钟生成电影级运镜:Motion LoRA技术让静态图片"动"起来 【免费下载链接】Motion-Lora-Camera-Push-In-Wan-14B-720p-I2V 项目地址: https://ai.gitcode.com/hf_mirrors/lovis93/Motion-Lora-Camera-Push-In-Wan-14B-720p-I2V 导语 只需一张静态…

作者头像 李华
网站建设 2026/2/13 16:13:40

OpCore Simplify:告别繁琐配置,开启智能Hackintosh新纪元

OpCore Simplify:告别繁琐配置,开启智能Hackintosh新纪元 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify 还在为复杂的Hackint…

作者头像 李华
网站建设 2026/2/15 11:14:58

Python航天轨道计算实战:从基础原理到星际任务规划

Python航天轨道计算实战:从基础原理到星际任务规划 【免费下载链接】poliastro poliastro - :rocket: Astrodynamics in Python 项目地址: https://gitcode.com/gh_mirrors/po/poliastro 在现代航天工程中,精确的轨道计算是连接理论探索与实际应用…

作者头像 李华
网站建设 2026/2/15 2:43:32

Agent路径选择总是不合理?,一文搞懂博弈论在游戏AI中的决策优化

第一章:游戏 AI 的 Agent 行为决策在现代电子游戏中,AI 控制的 Agent 不仅需要感知环境变化,还必须基于当前状态做出合理的行为决策。这类决策系统通常依赖于行为树、有限状态机或效用理论等模型,以模拟接近人类的反应逻辑。行为决…

作者头像 李华
网站建设 2026/2/15 9:44:35

14、文件操作、命令与内置命令详解

文件操作、命令与内置命令详解 1. 文件操作中的扩展通配模式 在文件操作里,扩展通配模式能让我们更灵活地匹配文件。下面是几种常见的扩展通配模式: - ?(pattern-list) :该模式匹配给定模式的零次或一次出现。例如, ?(john|paul)2 能匹配 john2 、 paul2 和…

作者头像 李华
网站建设 2026/2/13 8:05:28

如何在macOS上免费搭建企业级远程存储:iSCSI Initiator终极指南

如何在macOS上免费搭建企业级远程存储:iSCSI Initiator终极指南 【免费下载链接】iSCSIInitiator iSCSI Initiator for macOS 项目地址: https://gitcode.com/gh_mirrors/is/iSCSIInitiator 想要为你的Mac电脑轻松扩展存储空间吗?macOS iSCSI Ini…

作者头像 李华