Linux命令


chkconfig命令​检查、设置系统的各种服务。这是Red Hat公司遵循GPL规则所开发的程序,它可查询操作系统在每一个执行等级中会执行哪些系统服务,其中包括各类常驻服务。谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接。

chkconfig常见命令参数

用法:   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原理通俗理解

     chkconfig 处理的命令类似于我们平时执行的    /etc/init.d/sshd restart这样的命令

           每一个运行级别(0-6)对应一个   /etc/rc.d/rc3.d/  这样的 目录

[root@localhost ~]# chkconfig |grep sshd               # 运行级别是2345的时候开启sshd服务

Linux chkconfig命令详解_bash

[root@localhost rc3.d]# ll /etc/rc.d/rc3.d/S55sshd #s表示start,s55表示开机启动的时候是第55个开启的服务

Linux chkconfig命令详解_启动服务_02

[root@localhost rc3.d]# grep 'chkconfig' /etc/init.d/sshd 
# chkconfig: 2345 55 25 # 注释脚本里有默认的开启关闭的参数,这里开始是55
注:利用yum,rqm安装的服务,启动命令都会自动放在init.d下面,并且接受chkconfig管理

Linux chkconfig命令详解_运行级别_03

自定义启动服务

自己写chkconfig管理的脚本,放在/etc/init.d目录下

vim /etc/init.d/FTL

# 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/

Linux chkconfig命令详解_bash_04

ll /etc/rc3.d/ | grep FTL           # 默认第77个开启服务

Linux chkconfig命令详解_Linux命令_05

/etc/init.d/FTL start                  # 因为文件写了一个可以开启的函数

Linux chkconfig命令详解_启动服务_06


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/下的文件去写


常用的命令展示

界面设置自启动服务

ntsysv

 Linux chkconfig命令详解_bash_07

显示某个服务,例如sshd

[root@localhost ~]# chkconfig --list  sshd

Linux chkconfig命令详解_Linux命令_08

显示当前系统开启的服务

[root@localhost ~]# chkconfig | egrep 3:on

Linux chkconfig命令详解_bash_09

关闭某个服务

[root@localhost ~]# chkconfig --list|grep FTL
[root@localhost ~]# chkconfig FTL off
[root@localhost ~]# chkconfig --list|grep FTL

Linux chkconfig命令详解_运行级别_10

开启/关闭服务的3级别

[root@localhost ~]# chkconfig --list|grep FTL
[root@localhost ~]# chkconfig --level 3 FTL on 【开启3级别】
[root@localhost ~]# chkconfig --level 3 FTL off 【关闭3级别】

Linux chkconfig命令详解_启动服务_11

关闭我们不常用的服务【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


作者:​​小a玖拾柒​​​ ​

-------------------------------------------

个性签名: 所有的事情到最後都是好的,如果不好,那說明事情還沒有到最後~

本文版权归作者【​​小a玖拾柒​​​】,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利!