news 2026/4/18 8:27:18

常见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/4/17 3:03:00

UniVRM新手入门指南:5个常见问题与解决方案

UniVRM新手入门指南:5个常见问题与解决方案 【免费下载链接】UniVRM UniVRM is a gltf-based VRM format implementation for Unity. English is here https://vrm.dev/en/ . 日本語 はこちら https://vrm.dev/ 项目地址: https://gitcode.com/gh_mirrors/un/UniV…

作者头像 李华
网站建设 2026/4/17 22:20:59

信创环境下Llama-Factory与麒麟OS+飞腾CPU适配报告

信创环境下Llama-Factory与麒麟OS飞腾CPU适配实践 在政务、军工、金融等关键领域,人工智能模型的私有化部署正面临前所未有的挑战:既要满足高性能训练需求,又要确保软硬件全链路自主可控。传统依赖NVIDIA GPU和x86生态的大模型微调方案&#…

作者头像 李华
网站建设 2026/4/17 23:09:19

FSC认证三种标签深入了解

Forest Stewardship Council(FSC)拥有3种注册商标:“checkmark-and-tree”(首字母加树图形)标志、首字母“FSC”与全称“ForestStewardship Council”。深入了解FSC标签:FSC 100%:纯天然“森林之子” 含义:…

作者头像 李华
网站建设 2026/4/18 5:26:03

HoRain云--TCP vs UDP:网络编程实战指南

🎬 HoRain云小助手:个人主页 🔥 个人专栏: 《Linux 系列教程》《c语言教程》 ⛺️生活的理想,就是为了理想的生活! ⛳️ 推荐 前些天发现了一个超棒的服务器购买网站,性价比超高,大内存超划算!…

作者头像 李华