Linux chkconfig命令实例
2012-02-11 13:01
375 查看
Chkconfig命令用来设置,查看或更改配置开机自动启动的服务。下面根据七个实用的实例来说明Chkconfig命令的使用方法。
# vi check.sh
chkconfig network && echo "Network service is configured"
chkconfig junk && echo "Junk service is configured"
# ./check.sh
Network service is configured
你也可以指定检查该服务是否配置到指定的运行级。
# vi check1.sh
chkconfig network --level 3 && echo "Network service is configured for level 3"
chkconfig network --level 1 && echo "Network service is configured for level 1"
# ./check1.sh
Network service is configured for level 3
# chkconfig --list
abrtd 0:off 1:off 2:off 3:on 4:off 5:on 6:off
acpid 0:off 1:off 2:off 3:off 4:off 5:off 6:off
atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
...
可以使用grep过滤显示指定条件的服务。
下面的命令表示只显示运行级为3的服务。
chkconfig --list | grep 3:on
下面的命令表示只显示network服务的启动项状态。
chkconfig --list | grep network
下面的例子说明如何添加一个新服务(如iptables)到需要开机启动的服务列表。“chkconfig –add”命令也会自动地开启运行级2,3,4和5,如下:
# chkconfig --list | grep iptables
# chkconfig --add iptables
# chkconfig --list | grep iptables
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
# chkconfig --list | grep ip6tables
ip6tables 0:off 1:off 2:off 3:on 4:off 5:off 6:off
使用–del选项从启动列表删除它。
# chkconfig --del ip6tables
# chkconfig --list | grep ip6tables
下面的例子是为nfserver服务关闭运行级5.
# chkconfig --level 5 nfsserver off
你也可以同时关闭多个运行级,下面是关闭3和5运行级。
# chkconfig --level 35 nfsserver off
当chkconfig –add命令被执行,它会在对应的rc目录创建一个符合链接文件来启动和停止服务。
当chkconfig –del命令被执行,它会在对应的rc目录删除相应的符号链接。
下面的例子表明xinetd服务已经开启运行级3和5,所以xinetd会有两个文件在rc3.d目录和两个文件在rc5.d目录。以K开头的文件关机的时候用(K表示kill)。以S开头的文件开机的时候用(S表示start)。
# chkconfig --list | grep xinetd
xinetd 0:off 1:off 2:off 3:on 4:off 5:on 6:off
xinetd based services:
# cd /etc/rc.d/rc3.d
# ls | grep xinetd
K08xinetd
S14xinetd
# cd /etc/rc.d/rc5.d
# ls | grep xinetd
K08xinetd
S14xinetd
例如,如果nfsserver服务没有在启动项列表中,那nfsserver服务就没有文件在/etc/rc.d/rc*.d目录下。
# chkconfig --list | grep nfsserver
nfsserver 0:off 1:off 2:off 3:off 4:off 5:off 6:off
# ls /etc/rc.d/rc3.d | grep nfsserver
# ls /etc/rc.d/rc5.d | grep nfsserver
当你添加nfsserver服务之后,你将会在这些目录下看到符号链接。
# chkconfig --add nfsserver
nfsserver 0:off 1:off 2:off 3:on 4:off 5:on 6:off
# cd /etc/rc.d/rc3.d
# ls -l | grep nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver
# cd /etc/rc.d/rc5.d
# ls -l | grep nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver
当你使用–del或–level选项来关闭服务,在rcx.d目录下对应的符号链接文件将会被删除。
# chkconfig --level 5 nfsserver off
# ls /etc/rc.d/rc5.d | grep nfsserver
1、用Shell脚本检测服务系统启动项状态
当你只用服务名执行chkconfig命令时,如果该服务已经配置到系统启动项即返回真。下面的代码段是用来检查一个服务是否已经配置开机启动。# vi check.sh
chkconfig network && echo "Network service is configured"
chkconfig junk && echo "Junk service is configured"
# ./check.sh
Network service is configured
你也可以指定检查该服务是否配置到指定的运行级。
# vi check1.sh
chkconfig network --level 3 && echo "Network service is configured for level 3"
chkconfig network --level 1 && echo "Network service is configured for level 1"
# ./check1.sh
Network service is configured for level 3
2、查看当前服务系统启动项的状态
–list选项用来显示当前所有服务的系统启动项状态。# chkconfig --list
abrtd 0:off 1:off 2:off 3:on 4:off 5:on 6:off
acpid 0:off 1:off 2:off 3:off 4:off 5:off 6:off
atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
...
可以使用grep过滤显示指定条件的服务。
下面的命令表示只显示运行级为3的服务。
chkconfig --list | grep 3:on
下面的命令表示只显示network服务的启动项状态。
chkconfig --list | grep network
3、添加一个新服务到启动项
使用–add选项来添加一个指定服务到系统启动服务列表。下面的例子说明如何添加一个新服务(如iptables)到需要开机启动的服务列表。“chkconfig –add”命令也会自动地开启运行级2,3,4和5,如下:
# chkconfig --list | grep iptables
# chkconfig --add iptables
# chkconfig --list | grep iptables
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
4、从系统启动项列表删除一个服务
下面的例子表明ip6tables已经配置到启动项。# chkconfig --list | grep ip6tables
ip6tables 0:off 1:off 2:off 3:on 4:off 5:off 6:off
使用–del选项从启动列表删除它。
# chkconfig --del ip6tables
# chkconfig --list | grep ip6tables
5、为服务开启或关闭选定的运行级
有时候你可能不想从启动列表中删除整个服务,然而你可能仅仅想关闭指定的运行级。下面的例子是为nfserver服务关闭运行级5.
# chkconfig --level 5 nfsserver off
你也可以同时关闭多个运行级,下面是关闭3和5运行级。
# 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)。
# chkconfig --list | grep xinetd
xinetd 0:off 1:off 2:off 3:on 4:off 5:on 6:off
xinetd based services:
# cd /etc/rc.d/rc3.d
# ls | grep xinetd
K08xinetd
S14xinetd
# cd /etc/rc.d/rc5.d
# ls | grep xinetd
K08xinetd
S14xinetd
7、添加操作使rcx.d目录的变化
当你通过chkconfig命令添加一个新服务,默认的运行级会自动地为该服务开启,并且会在对应的rcx目录创建文件。例如,如果nfsserver服务没有在启动项列表中,那nfsserver服务就没有文件在/etc/rc.d/rc*.d目录下。
# chkconfig --list | grep nfsserver
nfsserver 0:off 1:off 2:off 3:off 4:off 5:off 6:off
# ls /etc/rc.d/rc3.d | grep nfsserver
# ls /etc/rc.d/rc5.d | grep nfsserver
当你添加nfsserver服务之后,你将会在这些目录下看到符号链接。
# chkconfig --add nfsserver
nfsserver 0:off 1:off 2:off 3:on 4:off 5:on 6:off
# cd /etc/rc.d/rc3.d
# ls -l | grep nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver
# cd /etc/rc.d/rc5.d
# ls -l | grep nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver
当你使用–del或–level选项来关闭服务,在rcx.d目录下对应的符号链接文件将会被删除。
# chkconfig --level 5 nfsserver off
# ls /etc/rc.d/rc5.d | grep nfsserver
相关文章推荐
- Linux chkconfig命令实例-【linux学习笔记】
- linux chkconfig命令参数及用法详解--linux系统服务设置命令
- Linux chkconfig命令
- Linux chkconfig命令
- Linux chkconfig 命令
- linux chkconfig命令参数及用法详解--linux系统服务设置命令
- linux chkconfig命令
- linux chkconfig命令参数及用法详解--linux系统服务设置命令
- Linux chkconfig命令
- linux 命令 /sbin/chkconfig
- Linux chkconfig 命令参数及用法详解 | linux系统服务设置命令
- linux 命令 /sbin/chkconfig
- linux chkconfig命令参数及用法详解
- Linux chkconfig命令入门
- Linux chkconfig命令
- linux chkconfig命令
- linux chkconfig命令 运行级别 service
- Linux chkconfig命令详解
- Linux chkconfig命令 【转】
- linux chkconfig命令参数及用法详解--linux系统服务设置命令