7个Linux chkconfig命令实例

Chkconfig命令用来设置,查看或更改配置开机自动启动的服务。下面根据七个实用的实例来说明Chkconfig命令的使用方法。

1、用Shell脚本检测服务系统启动项状态

当你只用服务名执行chkconfig命令时,如果该服务已经配置到系统启动项即返回真。下面的代码段是用来检查一个服务是否已经配置开机启动。

  1. # vi check.sh
  2. chkconfig network && echo "Network service is configured"
  3. chkconfig junk && echo "Junk service is configured"
  4.  
  5. # ./check.sh
  6. Network service is configured

你也可以指定检查该服务是否配置到指定的运行级。

  1. # vi check1.sh
  2. chkconfig network --level 3 && echo "Network service is configured for level 3"
  3. chkconfig network --level 1 && echo "Network service is configured for level 1"
  4.  
  5. # ./check1.sh
  6. Network service is configured for level 3

2、查看当前服务系统启动项的状态

–list选项用来显示当前所有服务的系统启动项状态。

  1. # chkconfig --list
  2. abrtd   0:off   1:off   2:off   3:on    4:off   5:on    6:off
  3. acpid   0:off   1:off   2:off   3:off   4:off   5:off   6:off
  4. atd     0:off   1:off   2:off   3:on    4:on    5:on    6:off
  5. ...

可以使用grep过滤显示指定条件的服务。
下面的命令表示只显示运行级为3的服务。

  1. chkconfig --list | grep 3:on

下面的命令表示只显示network服务的启动项状态。

  1. chkconfig --list | grep network

3、添加一个新服务到启动项

使用–add选项来添加一个指定服务到系统启动服务列表。
下面的例子说明如何添加一个新服务(如iptables)到需要开机启动的服务列表。“chkconfig –add”命令也会自动地开启运行级2,3,4和5,如下:

  1. # chkconfig --list | grep iptables
  2. # chkconfig --add iptables
  3. # chkconfig --list | grep iptables
  4. iptables       0:off   1:off   2:on    3:on    4:on    5:on    6:off

4、从系统启动项列表删除一个服务

下面的例子表明ip6tables已经配置到启动项。

  1. # chkconfig --list | grep ip6tables
  2. ip6tables       0:off   1:off   2:off   3:on   4:off   5:off   6:off

使用–del选项从启动列表删除它。

  1. # chkconfig --del ip6tables
  2. # chkconfig --list | grep ip6tables

5、为服务开启或关闭选定的运行级

有时候你可能不想从启动列表中删除整个服务,然而你可能仅仅想关闭指定的运行级。
下面的例子是为nfserver服务关闭运行级5.

  1. # chkconfig --level 5 nfsserver off

你也可以同时关闭多个运行级,下面是关闭3和5运行级。

  1. # chkconfig --level 35 nfsserver off

6、rc.d子目录下的脚本文件

无论你什么时候使用chkconfig命令添加或删除一个服务,它都会在/etc/rc.d子目录执行某些动作。

  • 当chkconfig –add命令被执行,它会在对应的rc目录创建一个符合链接文件来启动和停止服务。
  • 当chkconfig –del命令被执行,它会在对应的rc目录删除相应的符号链接。

下面的例子表明xinetd服务已经开启运行级3和5,所以xinetd会有两个文件在rc3.d目录和两个文件在rc5.d目录。以K开头的文件关机的时候用(K表示kill)。以S开头的文件开机的时候用(S表示start)。

  1. # chkconfig --list | grep xinetd
  2. xinetd                    0:off  1:off  2:off  3:on   4:off  5:on   6:off
  3. xinetd based services:
  4.  
  5. # cd /etc/rc.d/rc3.d
  6. # ls | grep xinetd
  7. K08xinetd
  8. S14xinetd
  9.  
  10. # cd /etc/rc.d/rc5.d
  11.  
  12. # ls | grep xinetd
  13. K08xinetd
  14. S14xinetd

7、添加操作使rcx.d目录的变化

当你通过chkconfig命令添加一个新服务,默认的运行级会自动地为该服务开启,并且会在对应的rcx目录创建文件。
例如,如果nfsserver服务没有在启动项列表中,那nfsserver服务就没有文件在/etc/rc.d/rc*.d目录下。

  1. # chkconfig  --list | grep nfsserver
  2. nfsserver                 0:off  1:off  2:off  3:off  4:off  5:off  6:off
  3. # ls /etc/rc.d/rc3.d | grep nfsserver
  4. # ls /etc/rc.d/rc5.d | grep nfsserver

当你添加nfsserver服务之后,你将会在这些目录下看到符号链接。

  1. # chkconfig --add nfsserver
  2. nfsserver                 0:off  1:off  2:off  3:on   4:off  5:on   6:off
  3.  
  4. # cd  /etc/rc.d/rc3.d
  5. # ls -l | grep nfsserver
  6. lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
  7. lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver
  8.  
  9. # cd /etc/rc.d/rc5.d
  10. # ls -l | grep nfsserver
  11. lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
  12. lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver

当你使用–del或–level选项来关闭服务,在rcx.d目录下对应的符号链接文件将会被删除。

  1. # chkconfig --level 5 nfsserver off
  2. # ls /etc/rc.d/rc5.d  | grep nfsserver
标签:Linux 发布于:2019-10-05 23:15:24