插件窝 干货文章 Linux下systemctl、service与chkconfig命令的区别详解

Linux下systemctl、service与chkconfig命令的区别详解

systemctl 服务 Systemd chkconfig 549    来源:    2025-05-02

在Linux系统中,systemctlservicechkconfig都是服务管理工具,但设计背景和功能有显著差异。以下是深度对比分析:


1. 设计背景与所属体系

命令 所属初始化系统 主要发行版支持
systemctl Systemd (新一代) RHEL 7+/CentOS 7+, Ubuntu 15.04+, Debian 8+
service SysVinit (传统) 所有Linux系统(兼容模式)
chkconfig SysVinit RHEL 6/CentOS 6及更早版本

2. 核心功能对比

(1) 服务状态管理

  • systemctl

    systemctl start|stop|restart|reload|status <服务名>
    # 示例:systemctl status nginx
    
    • 支持更精细的操作(如reload-or-restart
    • 状态输出包含更多信息(内存、日志关联等)
  • service

    service <服务名> start|stop|status
    # 示例:service httpd status
    
    • 实际是SysVinit脚本的兼容性封装
    • 在Systemd系统中会转发请求给systemctl

(2) 服务自启配置

  • systemctl

    systemctl enable|disable <服务名>      # 设置开机自启
    systemctl is-enabled <服务名>         # 检查状态
    
    • 通过符号链接实现(/etc/systemd/system/下的target)
  • chkconfig

    chkconfig <服务名> on|off             # 开关自启
    chkconfig --list                      # 查看所有服务
    
    • 依赖/etc/init.d/脚本中的LSB注释头
    • 仅管理SysVinit运行级别(如rc3.d/

(3) 高级功能

  • systemctl支持bash systemctl mask <服务名> # 完全禁用服务(即使手动启动) systemctl daemon-reload # 重载单元文件 journalctl -u <服务名> # 查看服务日志

3. 关键差异点总结

特性 systemctl service chkconfig
依赖的初始化系统 Systemd SysVinit(兼容层) SysVinit
运行级别 Target(如multi-user) Runlevel(0~6) Runlevel(0~6)
配置文件位置 /usr/lib/systemd/ /etc/init.d/ /etc/init.d/
日志集成 通过journalctl 需手动查/var/log/ 无集成
并行启动 支持 不支持 不支持

4. 迁移与兼容性

  • Systemd系统上的兼容性

    • servicechkconfig在Systemd系统中仍可用,但实际是调用systemctl的兼容层。
    • 示例:在RHEL 8中执行chkconfig httpd on会被转为systemctl enable httpd
  • SysVinit到Systemd的过渡

    # 将SysVinit脚本转换为Systemd单元文件(需手动调整)
    systemd-sysv-convert /etc/init.d/old-service
    

5. 最佳实践建议

  1. 新系统优先使用systemctl

    # 查询所有服务状态
    systemctl list-units --type=service
    
  2. 旧系统维护时组合使用

    service network restart    # 重启网络
    chkconfig --del mysqld     # 移除旧服务
    
  3. 调试技巧

    • Systemd服务失败时:
      bash systemctl --failed # 查看失败服务 journalctl -xe # 检查详细日志

通过理解这些工具的设计哲学和底层机制,可以更高效地管理不同Linux环境下的服务。现代发行版应优先掌握systemctl,而维护传统系统时需熟悉SysVinit工具链。