插件窝 干货文章 linux系统重启服务命令

linux系统重启服务命令

服务 sudo systemctl restart 396    来源:    2025-05-19

在 Linux 系统中,重启服务的命令取决于所使用的初始化系统(init system)。以下是常见的几种方式:


1. Systemd(现代主流系统:Ubuntu 16.04+/CentOS 7+)

# 重启服务
sudo systemctl restart <服务名>

# 示例:重启 Nginx
sudo systemctl restart nginx

相关操作: - 启动服务:sudo systemctl start <服务名> - 停止服务:sudo systemctl stop <服务名> - 查看状态:sudo systemctl status <服务名> - 设置开机自启:sudo systemctl enable <服务名> - 禁用开机自启:sudo systemctl disable <服务名>


2. SysVinit(旧版系统:Ubuntu 14.04/CentOS 6 等)

# 重启服务
sudo service <服务名> restart

# 或使用脚本路径
sudo /etc/init.d/<服务名> restart

# 示例:重启 Apache
sudo service httpd restart
# 或
sudo /etc/init.d/httpd restart

相关操作: - 启动服务:sudo service <服务名> start - 停止服务:sudo service <服务名> stop - 查看状态:sudo service <服务名> status


3. 其他工具

  • Upstart(旧版 Ubuntu)
    语法类似 SysVinit,但已被 systemd 取代: bash sudo restart <服务名>

4. 通用注意事项

  • 服务名:需替换为实际服务名称(如 nginxmysqlapache2 等)。
  • 权限:通常需要 sudo 或 root 权限。
  • 确认服务名
    可通过以下命令列出所有服务: bash systemctl list-unit-files --type=service # systemd service --status-all # SysVinit

5. 检查初始化系统

如果不确定系统使用的初始化系统,可通过以下命令确认:

ps -p 1 -o comm=
  • 输出 systemd → 使用 systemctl
  • 输出 init → 可能是 SysVinit 或 Upstart

示例场景

重启 Nginx(假设系统为 Ubuntu 18.04+)

sudo systemctl restart nginx
sudo systemctl status nginx  # 确认状态

根据你的系统选择对应的命令即可。如果有其他需求,欢迎补充说明!