chkconfig原理及应用

第1节  chkconfig命令

    通过man chkconfig,得到的解释如下:

chkconfig - updates and queries runlevel information for system services

    该命令的主要作用是用来更新(启动或停止)和查询系统服务的运行信息

1)语法

   chkconfig [--add][--del][--list][系统服务]      #<==添加或删除chkconfig管理的系统服务

    chkconfig [--level <等级代号>][系统服务][on/off/reset]     #<==设置系统对应运行级别下服务的开机启动/关闭信息

2) 查看chkconfig管理的服务

  
[root@lt1 ~]# chkconfig --list    #<==查看chkconfig所控制的服务(on表示开机时启动)
abrt-ccpp      0:off1:off2:off3:off4:off5:off6:off
abrtd          0:off1:off2:off3:off4:off5:off6:off
acpid          0:off1:off2:off3:off4:off5:off6:off
atd            0:off1:off2:off3:off4:off5:off6:off
auditd         0:off1:off2:off3:off4:off5:off6:off
blk-availability0:off1:on2:off3:off4:off5:off6:off
cpuspeed       0:off1:on2:off3:off4:off5:off6:off
crond          0:off1:off2:on3:on4:on5:on6:off
haldaemon      0:off1:off2:off3:off4:off5:off6:off
htcacheclean   0:off1:off2:off3:off4:off5:off6:off
httpd          0:off1:off2:off3:off4:off5:off6:off
ip6tables      0:off1:off2:off3:off4:off5:off6:off
iptables       0:off1:off2:off3:off4:off5:off6:off
irqbalance     0:off1:off2:off3:off4:off5:off6:off
kdump          0:off1:off2:off3:off4:off5:off6:off
lvm2-monitor   0:off1:on2:off3:off4:off5:off6:off
mdmonitor      0:off1:off2:off3:off4:off5:off6:off
messagebus     0:off1:off2:off3:off4:off5:off6:off
netconsole     0:off1:off2:off3:off4:off5:off6:off
netfs          0:off1:off2:off3:off4:off5:off6:off
network        0:off1:off2:on3:on4:on5:on6:off
ntpd           0:off1:off2:off3:off4:off5:off6:off
ntpdate        0:off1:off2:off3:off4:off5:off6:off
postfix        0:off1:off2:off3:off4:off5:off6:off
psacct         0:off1:off2:off3:off4:off5:off6:off
quota_nld      0:off1:off2:off3:off4:off5:off6:off
rdisc          0:off1:off2:off3:off4:off5:off6:off
restorecond    0:off1:off2:off3:off4:off5:off6:off
rngd           0:off1:off2:off3:off4:off5:off6:off
rsyslog        0:off1:off2:on3:on4:on5:on6:off
saslauthd      0:off1:off2:off3:off4:off5:off6:off
smartd         0:off1:off2:off3:off4:off5:off6:off
sshd           0:off1:off2:on3:on4:on5:on6:off
svnserve       0:off1:off2:off3:off4:off5:off6:off
sysstat        0:off1:on2:on3:on4:on5:on6:off
udev-post      0:off1:on2:off3:off4:off5:off6:off

    3)使用范例

chkconfig --list        #<==列出所有的系统服务
chkconfig --list httpd   #<==列出某一个服务
chkconfig --add httpd        #<==增加httpd服务
chkconfig --del httpd        #<==删除httpd服务
chkconfig --level httpd 2345 on        #设置httpd在运行级别为2、3、4、5的情况下都是on(开启)的状态

第2节  runlevel运行级别

runlevel即将系统不同的运行状态用数字0~6来表达,各数字含义如下:

0:表示关机状态

1:表示单用户模式状态(一般系统出现问题的时候会进入该状态,如root密码丢失等)

2:表示多用户模式,但是没有NFS功能

3:表示多用户模式(生产环境中的服务器默认运行状态)

4:系统保留

5:表示图形化界面状态

6:表示重启状态

1)设置运行级别

    使用init命令,后面跟需要设置状态的数字。如下:

[root@lt1 ~]# init 3

2)查看当前运行状态

[root@lt1 ~]# runlevel
3 3

第3节  chkconfig on /off 的原理

1)chkconfig首先只能对/etc/init.d/目录下的脚本进行管理

2)使用chkconfig on/off时,其实是在对应运行级别的初始化目录中(/etc/rc3.d/)生成的对应的软链接文件。下面以sshd服务为例:

    a.在runlevel下,sshd为off状态

[root@lt1 rc3.d]# chkconfig --list sshd
sshd           0:off1:off2:on3:off4:on5:on6:off

    b.通过查看,可以发现sshd服务此时的对应文件是k25sshd

[root@lt1 rc3.d]# ls -l /etc/rc3.d/* | grep "sshd"
lrwxrwxrwx  1 root root 14 Oct  2 07:58 /etc/rc3.d/K25sshd -> ../init.d/sshd

    c.把sshd服务在运行级别3设置为开机启动

[root@lt1 rc3.d]# chkconfig sshd --level 3 on
[root@lt1 rc3.d]# chkconfig --list sshd
sshd           0:off1:off2:on3:on4:on5:on6:off

    d.再查看/etc/rc3.d/下面对应的文件,发现变成了S55sshd文件

[root@lt1 rc3.d]# ls -l /etc/rc3.d/* | grep "sshd"
lrwxrwxrwx  1 root root 14 Oct  2 08:17 /etc/rc3.d/S55sshd -> ../init.d/sshd

    e.将S55sshd文件删除,在创建一个软链接文件K25sshd到/etc/init.d/sshd

[root@lt1 rc3.d]# rm -f /etc/rc3.d/S55sshd 
[root@lt1 rc3.d]# ln -s /etc/init.d/sshd /etc/rc3.d/K25sshd
[root@lt1 rc3.d]# ls -l /etc/rc3.d/* | grep "sshd"
lrwxrwxrwx  1 root root 16 Oct  2 08:21 /etc/rc3.d/K25sshd -> /etc/init.d/sshd

   f.此时再来查看sshd的运行级别,发现在3上居然是off了

[root@lt1 rc3.d]# chkconfig --list sshd
sshd           0:off1:off2:on3:off4:on5:on6:off

    g.enjoy,由此推断,chkconfig on/off就是控制/etc/rc*.d/下面的软链接文件

3)K25sshd和S55sshd是由谁来控制的?

    这两个文件的参数,其实是由sshd脚本的两行注释来控制的,如下所示。

[root@lt1 rc3.d]# sed -n '5,7p' /etc/init.d/sshd
# chkconfig: 2345 55 25   #<==2345表示控制的运行级别,55表示开机启动顺序,25表示关机关闭顺序
# description: SSH is a protocol for secure remote shell access. \   #<==注释可以随便写
#              This service starts up the OpenSSH server daemon.

第4节 如何将自己的脚本给chkconfig开机启动管理

1)在脚本前面插入两行

[root@lt1 script]# cat test 
#!/bin/bash

#chkconfig:- 66 22   #<=="-"表示所有运行级别;开机顺序为66;关机顺序为22
#description:this is ckconfig test script

echo "hello,my name is $HOSTNAME"

2)将脚本放在/etc/init.d/目录下(不要以.sh结尾)

[root@lt1 script]# cp test /etc/init.d/

3)给脚本赋予执行权限

[root@lt1 script]# chmod u+x /etc/init.d/test

4)纳入chkconfig管理

[root@lt1 script]# chkconfig --add test
[root@lt1 script]# chkconfig --list test
test           0:off1:off2:off3:off4:off5:off6:off

5)可以通过chkconfig on/off控制脚本是否开机启动

[root@lt1 script]# ls -lih /etc/rc3.d/ | grep "test"
137318 lrwxrwxrwx  1 root root 14 Oct  2 08:35 K22test -> ../init.d/test
[root@lt1 script]# chkconfig test --level 3 on
[root@lt1 script]# ls -lih /etc/rc3.d/ | grep "test"
137318 lrwxrwxrwx  1 root root 14 Oct  2 08:37 S66test -> ../init.d/test

enjoy!