chkconfig命令检查、设置系统的各类服务。这是Red Hat公司遵循GPL规则所开发的程序,它可查询操做系统在每个执行等级中会执行哪些系统服务,其中包括各种常驻服务。谨记chkconfig不是当即自动禁止或激活一个服务,它只是简单的改变了符号链接。html
用法: chkconfig [--list] [--type <type>] [name] chkconfig --add <name> chkconfig --del <name> chkconfig --override <name> chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>
chkconfig 处理的命令相似于咱们平时执行的 /etc/init.d/sshd restart这样的命令
每个运行级别(0-6)对应一个 /etc/rc.d/rc3.d/ 这样的 目录shell
[root@localhost ~]# chkconfig |grep sshd # 运行级别是2345的时候开启sshd服务
[root@localhost rc3.d]# ll /etc/rc.d/rc3.d/S55sshd #s表示start,s55表示开机启动的时候是第55个开启的服务
[root@localhost rc3.d]# grep 'chkconfig' /etc/init.d/sshd # chkconfig: 2345 55 25 # 注释脚本里有默认的开启关闭的参数,这里开始是55 注:利用yum,rqm安装的服务,启动命令都会自动放在init.d下面,而且接受chkconfig管理
本身写chkconfig管理的脚本,放在/etc/init.d目录下vim
vim /etc/init.d/FTLbash
# chkconfig: 345 77 69 # description: FTL is a protocol for secure remote shell access. \ # This serddvice starts up the OpenSSH server daemon. . /etc/init.d/functions case "$1" in start) action "FTL Linux is $1ing"/bin/true ;; esac
chmod +x /etc/init.d/FTL # 增长执行权限 chkconfig --add FTL # 添加到启动服务 chkconfig --list FTL # 查看启动服务,显示默认的345级别开, 默认修改/etc/rc3.d/ /etc/rc5.d/
ll /etc/rc3.d/ | grep FTL # 默认第77个开启服务
/etc/init.d/FTL start # 由于文件写了一个能够开启的函数
chkconfig管理脚本的要求:
1.执行 /etc/init.d/FTL start 格式执行正常服务
2.脚本开头增长以下内容;
# chkconfig: 345 77 69 【345是启动级别,77是第77个启动程序,69是第69个关闭程序】
# description: FTL is Coming
3.chkconfig 是针对程序是否须要机器后开启
# /etc/init.d/FTL start 让程序当前运行
4.能够参考/etc/rc.d/rc3.d/下的文件去写 ssh
界面设置自启动服务ide
ntsysv
显示某个服务,例如sshd函数
[root@localhost ~]# chkconfig --list sshd
显示当前系统开启的服务spa
[root@localhost ~]# chkconfig | egrep 3:on
关闭某个服务操作系统
[root@localhost ~]# chkconfig --list|grep FTL [root@localhost ~]# chkconfig FTL off [root@localhost ~]# chkconfig --list|grep FTL
开启/关闭服务的3级别3d
[root@localhost ~]# chkconfig --list|grep FTL [root@localhost ~]# chkconfig --level 3 FTL on 【开启3级别】 [root@localhost ~]# chkconfig --level 3 FTL off 【关闭3级别】
关闭咱们不经常使用的服务【Linux服务最小原则】
chkconfig |grep 3:on | awk '{print $1}' | grep -Ev "sshd|network|crond|sysstat|rsyslog" | xargs -I{} chkconfig {} off ==>for name in `chkconfig | grep 3:on |awk '{print $1}'` ; do chkconfig $name off; done; ==命令用反引号 tab键上 ==>chkconfig | awk '{print $1}' | grep -Ev "sshd|network|crond|sysstat|rsyslog" | sed -r 's#(.*)#chkconfig /1 off#g' | bash |bash 以前输出的只是字符串 |bash 以后是将内容交给bash处理 ==>chkconfig | awk '{print $1}' | grep -Ev "sshd|network|crond|sysstat|rsyslog" | awk '{print "chkconfig " $1 " off" }' | bash