Linux chkconfig 命令详解

一、简介

chkconfig 命令主要用来更新(启动或停止)和查询系统服务的运行级信息。谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接。

二、安装&启动

user@ae01:~$ sudo apt-get install chkconfig

三、语法&参数

user@ae01:~$ chkconfig --help
usage:
        chkconfig -A|--allservices              (together with -l: show all services)
        chkconfig -t|--terse [names]            (shows the links)
        chkconfig -e|--edit  [names]            (configure services)
        chkconfig -s|--set   [name state]...    (configure services)
        chkconfig -l|--list [--deps] [names]    (shows the links)
        chkconfig -c|--check name [state]       (check state)
        chkconfig -a|--add   [names]            (runs insserv)
        chkconfig -d|--del   [names]            (runs insserv -r)
        chkconfig -h|--help                     (print usage)
        chkconfig -f|--force ...                (call insserv with -f)

        chkconfig [name]           same as chkconfig -t
        chkconfig name state...    same as chkconfig -s name state
user@ae01:~$

 chkconfig 常用的参数有 -l、-a、-s、 -d 如果没有输入参数,chkconfig 默认使用参数 -A,显示前所有的服务并标明开启或关闭状态。如果加上服务名,相当于使用 -t 参数,那么就检查这个服务是否在当前运行级启动。如果是,返回true,否则返回false。如果在服务名后面指定了on,off或者reset,那么chkconfi 会改变指定服务的启动信息。on和off分别指服务被启动和停止,reset指重置服务的启动信息,无论有问题的初始化脚本指定了什么。on和off开关,系统默认只对运行级3,4,5有效,但是reset可以对所有运行级有效。

  • 显示所有运行级系统服务的运行状态信息(on或off)。
    chkconfig -l | [--list] [names]
    如果指定了names,那么只显示指定的服务在不同运行级的状态。
  • 增加新的自启动服务
    chkconfig -a | [--add] [names]

    在添加自启动服务之前,必须保证 /etc/init.d/ 目录下有相应的服务脚本。chkconfig确保每个运行级有一项启动(S)或者杀死(K)入口。如有缺少,则会从缺省的init脚本自动建立。

  • 修改自启动服务
    chkconfig -s | [--edit] [names state]

    state 可以是 on、off 以及 Linux 的操作环境等级(0123456),在操作过程中可能会因为依赖的关系导致执行失败,可以使用 -f 强制执行。

  • 删除自启动服务
    chkconfig -d|--del   [names]

    删除服务之后,相应的 /etc/rc[0-6].d/ 文件夹下的服务器的文件也会相应的删除

四、练习

  1. 查看 mysql 的具体自启动配置
    user@ae01:~$ chkconfig -l mysql
    mysql                     0:off  1:off  2:off  3:off  4:off  5:off  6:off
  2. 添加 mysql服务,使其能随系统启动而启动
    user@ae01:~$ chkconfig -a mysql

    Ubuntu 12.04 下执行该命令会返回错误  /sbin/insserv: No such file or directory 我们可以看出这是这可能是缺少文件 /sbin/insserv 造成的。对此可以先查找下 insserv 的文件位置

    user@ae01:~$ whereis insserv
    insserv: /bin/insserv /etc/insserv.conf /etc/insserv /usr/lib/insserv /usr/share/insserv /usr/share/man/man8/insserv.8.gz
    user@ae01:~$

    可以发现 ll 命令我们逐个查找,发现 insserv 文件的真正存在于 /usr/lib/insserv 目录下

    user@ae01:~$ ll /bin/insserv
    lrwxrwxrwx 1 root root 24 Oct 28 13:49 /bin/insserv -> /usr/lib/insserv/insserv*
    user@ae01:~$

    对此,我们可以在 /sbin 目录下创建一个 /usr/lib/insserv/insserv 的软连接

    user@ae01:~$ ln -s /usr/lib/insserv/insserv /sbin/insserv

    之后我们就可以使用 chkconfig -a 命令。

    mysql                     0:off  1:off  2:on  3:on  4:on  5:on  6:off
  3. 修改 mysql 服务,使其能随系统启动而启动
    user@ae01:~$ chkconfig -e mysql
    mysql off

    or

    user@ae01:~$ chkconfig -s mysql on
  4. 删除 mysql 自启动服务
    user@ae01:~$ chkconfig -d mysql
  5. 配置 mysql 自启动服务 Linux 运行级别为 2345
    user@ae01:~$ chkconfig -sf mysql 2345
  6. 手动配置 mysql 自启动服务 Linux 运行级别为 2345
    user@ae01:~$ ln -s /etc/init.d/mysql /etc/rc.d/rc2.d/S100mysql
    user@ae01:~$ ln -s /etc/init.d/mysql /etc/rc.d/rc3.d/S100mysql
    user@ae01:~$ ln -s /etc/init.d/mysql /etc/rc.d/rc4.d/S100mysql
    user@ae01:~$ ln -s /etc/init.d/mysql /etc/rc.d/rc5.d/S100mysql

五、扩展

  • Linux 的 7 种不同运行级别 (正常情况下一般为3或5)
    • 0:系统停机状态,系统默认运行级别不能设置为0,否则不能正常启动,机器关闭。  
    • 1:单用户工作状态,root权限,用于系统维护,禁止远程登陆,就像Windows下的安全模式登录。
    • 2:多用户状态,没有NFS支持。  
    • 3:完整的多用户模式,有NFS,登陆后进入控制台命令行模式。 
    • 4:系统未使用,保留一般不用,在一些特殊情况下可以用它来做一些事情。例如在笔记本电脑的电池用尽时,可以切换到这个模式来做一些设置。  
    • 5:X11控制台,登陆后进入图形GUI模式,XWindow系统。  
    • 6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动。运行init6机器就会重启。
原文地址:https://www.cnblogs.com/tannerBG/p/4063291.html