当前位置:首页>Linux>这个命令你会了,Linux你就会了一半了

这个命令你会了,Linux你就会了一半了

  • 2026-10-11 08:05:26
这个命令你会了,Linux你就会了一半了

📖 前言

在 Linux 系统中,systemctl 是 systemd 初始化系统的核心管理工具,用于管理系统服务、查看日志、设置开机自启等。自从各大主流发行版(如 CentOS 7+、Ubuntu 16.04+、Debian 8+)采用 systemd 以来,systemctl 已经取代了传统的 service 和 chkconfig 命令,成为每位运维和开发者的必备技能。

本文将全面介绍 systemctl 的常用命令、使用场景及实战技巧,帮助你快速掌握这一强大的系统管理工具。

(PS:文中所有的sudo都是基于普通用户下的提权操作,root用户可以不用加sudo命令哦)


📚 目录

  1. systemctl 基础概念

  2. 服务管理常用命令

  3. 查看服务状态

  4. 设置开机自启

  5. 管理系统单元(Unit)

  6. 实战案例

  7. 常见问题与技巧

  8. 总结


1. systemctl 基础概念

systemctl 是 systemd 架构中的控制命令,主要管理 Unit(单元)。Unit 包括:

类型后缀说明
Service.service系统服务(最常用)
Target.target运行级别(如 multi-user.target)
Socket.socket进程间通信套接字
Timer.timer定时任务(替代 cron)
Mount.mount挂载点

执行 systemctl 命令时,可以省略后缀(如 .service),系统会自动匹配。


2. 服务管理常用命令

以下命令默认操作 .service 类型的单元。

2.1 启动 / 停止 / 重启 / 重载

bash

# 启动服务
sudo systemctl start nginx

# 停止服务
sudo systemctl stop nginx

# 重启服务(先停再启)
sudo systemctl restart nginx

# 重载服务配置(不中断服务,仅部分服务支持)
sudo systemctl reload nginx

# 若服务已运行则重载,否则启动
sudo systemctl reload-or-restart nginx

2.2 启用 / 禁用开机自启

bash

# 设置开机自启
sudo systemctl enable nginx

# 取消开机自启
sudo systemctl disable nginx

# 查看是否已设置开机自启
systemctl is-enabled nginx

# 启用并立即启动(一步到位)
sudo systemctl enable --now nginx

2.3 屏蔽 / 取消屏蔽服务

屏蔽服务后,其他服务无法间接启动它,更安全地禁止服务。

bash

# 屏蔽服务(禁止手动和自动启动)
sudo systemctl mask nginx

# 取消屏蔽
sudo systemctl unmask nginx

3. 查看服务状态

3.1 查看单个服务详细信息

bash

systemctl status nginx

输出示例:

text

● nginx.service - A high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2025-03-10 10:22:30 CST; 2h ago
 Main PID: 1234 (nginx)
    Tasks: 3
   Memory: 12.5M
   CGroup: /system.slice/nginx.service
           ├─1234 nginx: master process
           └─1235 nginx: worker process

3.2 检查服务是否运行

bash

# 仅返回运行状态(静默模式,适合脚本)
systemctl is-active nginx && echo "Running" || echo "Stopped"

3.3 列出所有服务单元

bash

# 列出所有已加载的服务(包括 running 和 exited)
systemctl list-units --type=service

# 列出所有服务(包括未激活的)
systemctl list-units --type=service --all

# 列出失败的服务
systemctl --failed --type=service

4. 设置开机自启

4.1 管理运行目标(Target)

systemd 使用 target 来模拟传统的运行级别:

传统级别systemd target说明
0poweroff.target关机
1rescue.target单用户模式
3multi-user.target多用户无图形界面
5graphical.target多用户图形界面
6reboot.target重启

bash

# 查看当前默认启动目标
systemctl get-default

# 设置默认启动目标(例如改为命令行模式)
sudo systemctl set-default multi-user.target

# 临时切换运行级别(不重启)
sudo systemctl isolate rescue.target

4.2 查看服务的依赖关系

bash

# 列出服务依赖
systemctl list-dependencies nginx

# 反向依赖(哪些服务依赖本服务)
systemctl list-dependencies --reverse nginx

5. 管理系统单元(Unit)

5.1 查看和编辑单元文件

bash

# 查看单元文件内容
systemctl cat nginx

# 编辑单元文件(会自动创建覆盖文件)
sudo systemctl edit nginx

# 编辑完整单元文件(直接修改原始文件,推荐使用 .d 目录)
sudo systemctl edit --full nginx

# 重新加载所有单元文件(修改后必须执行)
sudo systemctl daemon-reload

5.2 控制其他类型的单元

bash

# 管理 Timer(定时器)
sudo systemctl start backup.timer
sudo systemctl enable backup.timer

# 管理 Socket(套接字)
sudo systemctl status sshd.socket

# 管理 Mount(挂载点)
sudo systemctl start mnt-data.mount

6. 实战案例

案例1:部署一个自定义 Python Web 服务

创建服务文件 /etc/systemd/system/myapp.service:

ini

[Unit]
Description=My Python Web App
After=network.target

[Service]
Type=simple
User=www-data
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/python3 /opt/myapp/app.py
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

使用 systemctl 管理:

bash

# 重载配置
sudo systemctl daemon-reload

# 启动服务
sudo systemctl start myapp

# 设置开机自启
sudo systemctl enable myapp

# 查看日志
sudo journalctl -u myapp -f

案例2:查看系统启动耗时

bash

# 查看内核和用户空间启动时间
systemd-analyze

# 列出各服务启动耗时(优化启动速度)
systemd-analyze blame

# 绘制启动流程图(需要安装 graphviz)
systemd-analyze plot > boot.svg

案例3:定时备份服务(使用 Timer)

创建服务 /etc/systemd/system/backup.service:

ini

[Unit]
Description=Daily Backup

[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh

创建 Timer /etc/systemd/system/backup.timer:

ini

[Unit]
Description=Run backup daily at 2am

[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true

[Install]
WantedBy=timers.target

启用 timer:

bash

sudo systemctl enable backup.timer
sudo systemctl start backup.timer

7. 常见问题与技巧

❓ Q1:修改服务文件后不生效?

bash

sudo systemctl daemon-reload   # 必须执行此命令
sudo systemctl restart myservice

❓ Q2:如何限制服务的内存或 CPU?

在 [Service] 段添加:

ini

MemoryMax=512M
CPUQuota=50%

❓ Q3:服务启动失败如何查看详细日志?

bash

# 查看服务所有日志
sudo journalctl -u nginx

# 实时跟踪日志
sudo journalctl -u nginx -f

# 查看最近 100 行
sudo journalctl -u nginx -n 100

💡 技巧:命令别名简化操作

编辑 ~/.bashrc:

bash

alias sstart='sudo systemctl start'
alias sstop='sudo systemctl stop'
alias srestart='sudo systemctl restart'
alias sstatus='systemctl status'
alias senable='sudo systemctl enable'
alias sdisable='sudo systemctl disable'

8. 总结

systemctl 是 Linux systemd 系统的核心管理工具,掌握它可以让你:

  • ✅ 高效管理服务(启动/停止/重启/重载)

  • ✅ 灵活配置开机自启与运行级别

  • ✅ 深入查看服务状态与日志

  • ✅ 通过 Timer 替代传统 cron

  • ✅ 定制系统行为,优化启动速度

从传统 SysV init 迁移到 systemd 是 Linux 生态的发展趋势,熟练使用 systemctl 将大大提升你的运维和开发效率。

📌 最后提示:不同发行版的 systemd 版本可能略有差异,建议使用 systemctl --version 查看版本,并用 man systemctl 获取最权威的帮助。


👍 如果本文对你有帮助,欢迎点赞、收藏、评论!

🔗 更多 Linux 干货,请关注我的 CSDN 专栏。

最新文章

随机文章