1. systemctl 概述
systemctl 是 Linux 系统中用于控制 systemd 系统和服务管理器的命令行工具。作为现代 Linux 发行版的核心组件,systemd 取代了传统的 SysVinit 系统,而 systemctl 则是管理员与 systemd 交互的主要接口。
我第一次接触 systemctl 是在管理一台 CentOS 7 服务器时。当时我需要重启一个关键服务,但发现传统的 service 命令已经不再适用。通过 systemctl,我不仅能够启动/停止服务,还能查看详细的运行状态、设置开机自启等。这个工具的强大功能让我印象深刻。
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. systemctl 基础使用
2.1 服务管理基本操作
管理服务是 systemctl 最常用的功能。以下是一些基本命令:
code复制# 启动服务
sudo systemctl start nginx.service
# 停止服务
sudo systemctl stop nginx.service
# 重启服务
sudo systemctl restart nginx.service
# 重新加载配置(不重启服务)
sudo systemctl reload nginx.service
# 查看服务状态
sudo systemctl status nginx.service
在实际工作中,我发现 status 命令特别有用。它不仅显示服务是否运行,还会提供最近的日志片段,这在排查问题时非常方便。
2.2 服务启用与禁用
与传统 init 系统不同,systemd 使用"启用"(enable)和"禁用"(disable)来控制服务是否在启动时自动运行:
code复制# 启用服务开机自启
sudo systemctl enable nginx.service
# 禁用服务开机自启
sudo systemctl disable nginx.service
# 查看服务是否启用
sudo systemctl is-enabled nginx.service
注意:在修改服务配置后,通常需要执行
systemctl daemon-reload来重新加载配置。
