zoukankan      html  css  js  c++  java
  • chkconfig原理

    chkconfig命令

    功能:检查、设定系统的各种服务

    updates and queries runlevel information for system services 

    Linux提供了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 [--level levels] [--type type] name
     

    参数说明:

    • --add 新增系统服务
    • --del 删除系统服务
    • --level 指定系统服务要在哪个执行等级中开启或关闭
    • --list 列出当前可从chkconfig指令管理的所有系统服务和等级代号
    • on/off/reset 在指定的执行登记,开启/关闭/重置该系统服务

    运行级别:

    具体内容查看/etc/inittab

    • 0:关机(请不要切换到此等级)
    • 1:单用户文本模式
    • 2:多用户文本模式,不具有网络档案系统(NFS)功能
    • 3:多用户文本模式,具有网络档案系统(NFS)功能
    • 4:备用
    • 5:多用户图形界面模式
    • 6:重新启动
     

    chkconfig命令执行示例:

    1. chkconfig --list :显示所有运行级系统服务的运行状态信息(on或off)。

    如果指定了name,那么只显示指定的服务在不同运行级的状态。

    [root@oldboy rc3.d]# chkconfig --list sshd
    sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off

    2. chkconfig [--level levels] name <on|off|reset>:设置某一服务在指定的运行级是被启动,停止还是重置。

    例如,要在3,4,5运行级停止nfs服务,则命令如下:

    chkconfig --level 345 nfs off

    [root@oldboy rc3.d]# chkconfig --levels 123456 sshd off
    [root@oldboy rc3.d]# chkconfig --list sshd             
    sshd            0:off   1:off   2:off   3:off   4:off   5:off   6:off
    
    
    [root@oldboy rc3.d]# chkconfig --level 2345 sshd on
    [root@oldboy rc3.d]# chkconfig --list sshd             
    sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off

    3. chkconfig --add name:增加一项新的服务。

    chkconfig确保每个运行级有一项启动(S)或者杀死(K)入口。

    如有缺少,则会从缺省的init脚本自动建立。

    4. chkconfig --del name:删除服务,并把相关符号连接从/etc/rc[0-6].d删除。

    chkconfig启动和关闭服务的原理:

    以sshd服务为例,sshd在运行级别为 3:on 的情况下,/etc/rc.d/rc3.d/ 目录下会生成 "S55sshd"指定到"/etc/init.d/sshd"文件的软连接。 

    • 其中"S55sshd"的"S"代表服务开启on;
    • 55 代表on的状态的文件编码
    • 25 代表off的状态的文件编码,“K”代表服务off;在3:off的情况下,会生成"K25sshd"指向/etc/init.d/sshd的软连接
     下面是在/etc/rc.d/rc3.d 目录下的S55sshd指向/etc/init.d/sshd服务的软连接:
    [root@oldboy rc3.d]# ls -l /etc/rc.d/rc3.d/ |grep sshd
    lrwxrwxrwx  1 root root 14 Sep  5 00:23 S55sshd -> ../init.d/sshd

    查看/etc/init.d/sshd文件的内容:

    • chkconfig:2345 55 25
      • 2345 是在chkconfig不指定--level的情况下,默认的运行级别
      • 55 是 on 的状态的下的 S55sshd 的软连接名称编码
      • 25 是 off 的状态下的 K25sshd 的软连接名称编码
    • processname:sshd 进程名称
    [root@oldboy rc3.d]# head /etc/init.d/sshd
    #!/bin/bash
    #
    # sshd          Start up the OpenSSH server daemon
    #
    # chkconfig: 2345 55 25  
    # description: SSH is a protocol for secure remote shell access. 
    #              This service starts up the OpenSSH server daemon.
    #
    # processname: sshd
    # config: /etc/ssh/ssh_host_key

    注意:  
    通过手动删除K25sshd 或 增加S55sshd 文件都可以实现chkconfig的sshd服务的on或off的设置。

    编写自定义服务chktestd

    在/etc/init.d/chktestd中手动写入服务:

    [root@oldboy rc3.d]# vim /etc/init.d/chktestd
    
    [root@oldboy rc3.d]# cat /etc/init.d/chktestd
    # chkconfig:23456 66 33
    # description: this is a test service for chkconfig learning.
    
    # processname:chktestd
    
    echo Celebrate!All hail China!

    在chkconfig的服务列表中手动添加chktestd服务:

    [root@oldboy rc3.d]# chkconfig --add chktestd  # 添加服务默认是开启默认level级别的服务
    [root@oldboy rc3.d]# chkconfig --list chktestd
    chktestd        0:off   1:off   2:on    3:on    4:on    5:on    6:on

    查看在/etc/rc.d/rc3.d中是否生成对应的S66chktestd:

    [root@oldboy rc3.d]# ls -l /etc/rc.d/rc3.d/|grep chktestd     
    lrwxrwxrwx  1 root root 18 Sep  5 00:51 S66chktestd -> ../init.d/chktestd

    查看服务chktestd的运行效果:

    [root@oldboy rc3.d]# chmod +x /etc/init.d/chktestd  # 给执行权限
    [root@oldboy rc3.d]# /etc/init.d/chktestd
    1122334455
    Celebrate!All hail China!
     
     
     
     
  • 相关阅读:
    如何实现Android欢迎页
    创建webapi的简单步骤
    IOS里Request的斗争(上)
    预告:关于接下来的部分,都是番外。
    利用Selenium制作python数据抓取,以及对Selenium资源介绍
    利用openpyxl来读取Excel数据
    在pycharm下快速添加插件
    C# 分布式缓存服务器方案
    2014中国省市数据库
    FormsAuthentication 登录兼容 IE11 保存cookie
  • 原文地址:https://www.cnblogs.com/zoe233/p/11919566.html
Copyright © 2011-2022 走看看