15%

全场主机优惠15%

测试技能,享折扣

使用代码:

Skills
开始使用
24.10.2024

在 Linux 命令行中重启服务:完整管理员指南

高效管理 Linux 服务是每位系统管理员必须掌握的最基本技能之一。无论是应用配置更改、从意外崩溃中恢复、排查行为异常的守护进程,还是简单地刷新正在运行的进程,了解如何从命令行重启服务对于保持基础设施的健康和响应能力至关重要。

本综合指南将带您了解重启 Linux 服务的所有主要方法——涵盖 systemdSysVinitUpstart——以及实际示例、状态检查技术、日志分析技巧和经验丰富的管理员每天依赖的最佳实践。

了解 Linux 服务和服务管理器

在 Linux 中,服务(也称为守护进程)是执行特定持续功能的后台进程。常见示例包括:

  • Apache / Nginx — Web 服务器守护进程
  • MySQL / PostgreSQL — 数据库服务器守护进程
  • SSH (sshd) — 安全 Shell 访问守护进程
  • Cron — 计划任务守护进程
  • Postfix / Dovecot — 邮件服务器守护进程

这些服务由 init 系统服务管理器控制,负责启动、停止、重启和监控它们。您在 Linux 上最常遇到的三种服务管理器是:

服务管理器使用于
systemdUbuntu 15.04+、Debian 8+、CentOS 7+、RHEL 7+、Fedora、AlmaLinux、Rocky Linux
SysVinit较旧的 Debian、较旧的 Ubuntu(15.04 之前)、旧版 RHEL/CentOS 6
UpstartUbuntu 9.10 至 14.10

绝大多数现代 Linux 服务器——包括运行在 VPS 托管独立服务器上的服务器——使用 systemd 作为默认服务管理器。但是,了解所有三种系统可确保您为遇到的任何环境做好准备。

方法 1:使用 systemd 重启服务(现代 Linux)

systemd 是几乎所有现代 Linux 发行版上的主流 init 系统。它通过 systemctl 命令行工具管理服务,为服务管理提供一致、强大的界面。

步骤 1:打开终端

访问服务器的命令行界面——直接访问、通过 SSH 或通过托管控制面板访问。

步骤 2:重启服务

使用 systemd 重启服务的基本语法为:

sudo systemctl restart <service-name>

常见实际示例:

# Restart the Apache web server
sudo systemctl restart apache2

# Restart Nginx
sudo systemctl restart nginx

# Restart MySQL / MariaDB
sudo systemctl restart mysql

# Restart the SSH daemon
sudo systemctl restart sshd

# Restart the Cron scheduler
sudo systemctl restart cron

# Restart Postfix mail server
sudo systemctl restart postfix

> 注意:在某些发行版上,服务名称可能略有不同。例如,Apache 在 Debian/Ubuntu 系统上可能是 apache2,但在 CentOS/RHEL/AlmaLinux 系统上是 httpd

步骤 3:验证服务是否成功重启

发出重启命令后,始终确认服务已正常恢复运行:

sudo systemctl status <service-name>

健康的 Apache 服务的示例输出:

sudo systemctl status apache2

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2025-01-13 10:45:22 UTC; 3s ago
    Process: 12345 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 12346 (apache2)

需要关注的关键指标:

  • Active: active (running) — 服务正常运行
  • Active: failed — 服务启动失败;立即检查日志
  • Loaded: enabled — 服务已配置为在启动时自动启动

其他有用的 systemd 命令

除了简单的重启之外,systemd 还提供了管理员经常使用的几个相关命令:

# Stop a service completely
sudo systemctl stop <service-name>

# Start a stopped service
sudo systemctl start <service-name>

# Reload configuration without a full restart (if supported)
sudo systemctl reload <service-name>

# Reload config or restart if reload isn't supported
sudo systemctl reload-or-restart <service-name>

# Enable a service to start automatically at boot
sudo systemctl enable <service-name>

# Disable a service from starting at boot
sudo systemctl disable <service-name>

# Check if a service is currently active
sudo systemctl is-active <service-name>

# Check if a service is enabled at boot
sudo systemctl is-enabled <service-name>

使用 journalctl 查看实时服务日志

systemd 与日志记录系统集成。要查看特定服务的日志:

# View all logs for a service
sudo journalctl -u apache2

# Follow logs in real time (like tail -f)
sudo journalctl -u apache2 -f

# View only the most recent 50 lines
sudo journalctl -u apache2 -n 50

# View logs since the last boot
sudo journalctl -u apache2 -b

当服务重启失败且需要快速诊断根本原因时,这非常有价值。

方法 2:使用 SysVinit 重启服务(旧版 Linux)

SysVinit 是一种较旧的 init 系统,仍在旧版服务器、嵌入式系统和较旧的 Linux 发行版上使用。如果您正在维护较旧的服务器环境,您将使用 service 命令来管理守护进程。

步骤 1:打开终端

通过 SSH 或直接控制台访问连接到服务器。

步骤 2:重启服务

sudo service <service-name> restart

常见示例:

# Restart MySQL
sudo service mysql restart

# Restart Apache
sudo service apache2 restart

# Restart Nginx
sudo service nginx restart

# Restart the SSH daemon
sudo service ssh restart

# Restart Postfix
sudo service postfix restart

步骤 3:检查服务状态

sudo service <service-name> status

示例:

sudo service mysql status

● mysql.service - MySQL Community Server
   Active: active (running) since Mon 2025-01-13 10:47:00 UTC; 5s ago

其他 SysVinit 服务命令

# Stop a service
sudo service <service-name> stop

# Start a service
sudo service <service-name> start

# Reload configuration
sudo service <service-name> reload

您还可以使用位于 /etc/init.d/ 中的 init 脚本直接与 SysVinit 服务交互:

sudo /etc/init.d/apache2 restart
sudo /etc/init.d/mysql status

> 兼容性说明:在基于现代 systemd 的发行版上,service 命令仍作为兼容性包装器工作——它会自动将 service 命令在后台转换为 systemctl 调用。这意味着您可以在现代系统上安全地使用 service 命令,尽管 systemctl 因其附加功能而更受推荐。

方法 3:使用 Upstart 重启服务(Ubuntu 旧版)

Upstart 是 Ubuntu 从 9.10 版到 14.10 版的 init 系统,在 Ubuntu 15.04 中被 systemd 取代。如果您正在维护非常旧的 Ubuntu 服务器,您将使用 initctl 命令。

步骤 1:打开终端

步骤 2:重启服务

sudo initctl restart <service-name>

示例:

# Restart lighttpd
sudo initctl restart lighttpd

# Restart MySQL
sudo initctl restart mysql

步骤 3:检查服务状态

sudo initctl status <service-name>

示例:

sudo initctl status lighttpd

lighttpd start/running, process 4521

其他 Upstart 命令

# Stop a service
sudo initctl stop <service-name>

# Start a service
sudo initctl start <service-name>

# List all Upstart-managed jobs and their states
sudo initctl list

如何识别您的系统使用哪种服务管理器

如果您连接到不熟悉的服务器且不确定使用哪种 init 系统,请运行以下快速诊断命令:

# Check if systemd is running as PID 1
ps -p 1 -o comm=

# Or check the init system directly
cat /proc/1/comm

# Check systemd version (if installed)
systemctl --version

# Check if Upstart is present
initctl --version

# Check for SysVinit scripts
ls /etc/init.d/

如果 ps -p 1 -o comm= 返回 systemd,则您使用的是 systemd 系统。如果返回 init,则您可能使用的是 SysVinit 或 Upstart。

重启 Linux 服务的最佳实践

经验丰富的管理员遵循一套最佳实践,以在重启服务时最大限度地减少停机时间并避免配置错误:

1. 重启前始终验证配置

许多服务提供内置语法检查。在重启之前使用它,以避免因配置损坏而导致服务停止:

# Test Apache configuration
sudo apachectl configtest
# or
sudo apache2ctl -t

# Test Nginx configuration
sudo nginx -t

# Test SSH daemon configuration
sudo sshd -t

# Test MySQL configuration
sudo mysqld --validate-config

如果配置测试通过,则继续重启。如果失败,请先修复错误。

2. 尽可能使用 reload 而非 restart

重新加载在不中断活动连接的情况下应用配置更改,比完全重启的干扰性小得多:

# Gracefully reload Apache (zero downtime)
sudo systemctl reload apache2

# Gracefully reload Nginx
sudo systemctl reload nginx

仅在确实需要完整进程重启时才使用 restart(例如,软件更新后或不支持重新加载时)。

3. 重启失败后立即检查日志

如果服务无法恢复运行,请立即检查日志:

# systemd journal (most detailed)
sudo journalctl -u <service-name> -n 100 --no-pager

# Application-specific log files
sudo tail -n 50 /var/log/apache2/error.log
sudo tail -n 50 /var/log/nginx/error.log
sudo tail -n 50 /var/log/mysql/error.log
sudo tail -n 50 /var/log/syslog

4. 在远程服务器上重启 SSH 时要小心

如果您通过 SSH 连接到远程服务器并重启 SSH 守护进程,您可能会失去连接。降低此风险:

# Safer: reload SSH config without dropping connections
sudo systemctl reload sshd

# If you must restart, use a delayed command to give yourself time to reconnect
sudo shutdown -r +1  # schedules a reboot in 1 minute (use only if necessary)

5. 了解对正在运行的进程的影响

重启 MySQL 或 PostgreSQL 等数据库服务将终止所有活动的数据库连接。在重启关键服务之前,始终通知应用程序团队或为生产系统安排维护窗口。

6. 确保服务在重启后能够自动启动

手动重启服务后,验证它是否设置为在系统重启后自动启动:

sudo systemctl enable <service-name>

快速参考:服务管理命令速查表

操作systemdSysVinitUpstart
重启systemctl restart <svc>service <svc> restartinitctl restart <svc>
启动systemctl start <svc>service <svc> startinitctl start <svc>
停止systemctl stop <svc>service <svc> stopinitctl stop <svc>
重新加载systemctl reload <svc>service <svc> reloadinitctl reload <svc>
状态systemctl status <svc>service <svc> statusinitctl status <svc>
开机启用systemctl enable <svc>update-rc.d <svc> enableN/A
查看日志journalctl -u <svc>/var/log/syslog/var/log/upstart/<svc>.log

常见服务重启问题排查

重启后服务无法启动

症状:systemctl status 显示 Active: failed

诊断步骤:

# Get detailed failure reason
sudo systemctl status <service-name>
sudo journalctl -u <service-name> -n 50

# Check for port conflicts
sudo ss -tlnp | grep <port-number>
sudo netstat -tlnp | grep <port-number>

# Check file permissions on config files
ls -la /etc/<service-config-file>

服务重启但未应用新配置

原因:配置文件存在语法错误,或加载了错误的配置文件。

解决方案:

# Verify which config file the service is using
sudo <service-name> -V  # (works for Apache, Nginx, etc.)

# Re-validate config syntax
sudo nginx -t
sudo apachectl configtest

运行服务命令时出现”权限被拒绝”

原因:权限不足。

解决方案:始终使用 sudo 或切换到 root 用户:

sudo systemctl restart <service-name>
# or
su -c "systemctl restart <service-name>"

systemd 报告”找不到单元”

原因:服务名称不正确或服务未安装。

解决方案:

# Search for the correct service name
sudo systemctl list-units --type=service | grep <partial-name>

# Or list all available unit files
sudo systemctl list-unit-files | grep <partial-name>

在 AlexHost 基础设施上管理服务

如果您在 AlexHost 基础设施上运行服务,本指南中介绍的服务管理命令可直接应用于您的环境。AlexHost 的 VPS 托管计划在具有完整 systemd 支持的现代 Linux 发行版上运行,让您通过 SSH 完全控制您的服务堆栈。

对于偏好图形界面进行服务管理的管理员,AlexHost 提供 带 cPanel 的 VPS 和一系列 VPS 控制面板,提供基于 GUI 的服务管理以及完整的命令行访问。

如果您的工作负载需要最高性能和专用资源——例如,运行高流量 Web 服务器、数据库集群或邮件服务器——请考虑 AlexHost 的独立服务器,它为您提供完整的 root 访问权限和对服务配置的完全控制。

对于同时需要可靠电子邮件基础设施的 Web 应用程序团队,AlexHost 的电子邮件托管提供托管解决方案,消除了自行配置和维护 Postfix 和 Dovecot 等邮件服务器守护进程的复杂性。

如果您托管处理敏感用户数据的网站,将您的托管与 SSL 证书配合使用可确保加密连接——请记住,安装或续订 SSL 证书后,您需要重启 Web 服务器守护进程(Apache 或 Nginx)才能使更改生效。

结论

从命令行重启 Linux 服务是每位系统管理员——从初学者到专家——都必须掌握的基础技能。具体命令因 init 系统而异:

  • systemd(现代 Linux):sudo systemctl restart <service-name>
  • SysVinit(旧版 Linux):sudo service <service-name> restart
  • Upstart(旧版 Ubuntu):sudo initctl restart <service-name>

但除了了解命令之外,真正有效的服务管理意味着了解何时使用 reload 而非 restart、如何在应用更改之前验证配置、如何在出现问题时读取日志,以及如何确保服务在系统重启后继续运行。

掌握这些技术,您将具备保持 Linux 服务器平稳运行、最大限度减少停机时间以及快速自信地解决服务问题的能力——无论您是管理单个 VPS 还是一批独立服务器。

15%

全场主机优惠15%

测试技能,享折扣

使用代码:

Skills
开始使用