Centos6 init解析及chkconfig开机启动原理

init解析及chkconfig:

init是Centos6系统的第一个进程,位于:/sbin/init

配置文件位于/etc/inittab:定义了系统启动的6个运行级别以及默认运行级别

# cat -n /etc/inittab 
 1# inittab is only used by upstart for the default runlevel.
 2#
 3# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
 4#
 5# System initialization is started by /etc/init/rcS.conf    # 系统初始化使用的配置文件
 6#
 7# Individual runlevels are started by /etc/init/rc.conf    # 个人运行级别使用的配置文件
 8#
 9# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
10#
11# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
12# with configuration in /etc/sysconfig/init.
13#
14# For information on how to write upstart event handlers, or how
15# upstart works, see init(5), init(8), and initctl(8).
16#
17# Default runlevel. The runlevels used are:
18#   0 - halt (Do NOT set initdefault to this)
19#   1 - Single user mode
20#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
21#   3 - Full multiuser mode
22#   4 - unused
23#   5 - X11
24#   6 - reboot (Do NOT set initdefault to this)
25# 
26id:5:initdefault:    # 系统启动默认的运行级别

/etc/init目录下保存着inittab配置文件的分片配置文件

如上第5行,/etc/init/rcS.conf配置文件:

# more /etc/init/rcS.conf 
# rcS - runlevel compatibility
#
# This task runs the old sysv-rc startup scripts.
#
# Do not edit this file directly. If you want to change the behaviour,
# please create a file rcS.override and put your changes there.
start on startup
stop on runlevel
task
# Note: there can be no previous runlevel here, if we have one it's bad
# information (we enter rc1 not rcS for maintenance).  Run /etc/rc.d/rc
# without information so that it defaults to previous=N runlevel=S.
console output
pre-start script
for t in $(cat /proc/cmdline); do
case $t in
emergency)
start rcS-emergency
break
;;
esac
done
end script
exec /etc/rc.d/rc.sysinit    # 执行系统初始化脚本
post-stop script
if [ "$UPSTART_EVENTS" = "startup" ]; then
[ -f /etc/inittab ] && runlevel=$(/bin/awk -F ':' '$3 == "initdefault" && $1 !~ "^#" { print $2 }' /etc/inittab)
--More--(79%)

系统初始化脚本:/etc/rc.d/rc.sysinit,完成以下任务:

1、激活udev和selinux;

2、根据/etc/sysctl.conf文件,来设定内核参数;

3、设定时钟时钟;

4、装载键盘映射;

5、启用交换分区;

6、设置主机名;

7、根文件系统检测,并以读写方式重新挂载;

8、激活RAID和LVM设备;

9、启用磁盘配额;

10、根据/etc/fstab,检查并挂载其它文件系统;

11、清理过期的锁和PID文件;


再看上面第7行,/etc/init/rc.conf配置文件:

# cat /etc/init/rc.conf 
# rc - System V runlevel compatibility
#
# This task runs the old sysv-rc runlevel scripts.  It
# is usually started by the telinit compatibility wrapper.
#
# Do not edit this file directly. If you want to change the behaviour,
# please create a file rc.override and put your changes there.
start on runlevel [0123456]
stop on runlevel [!$RUNLEVEL]
task
export RUNLEVEL
console output
exec /etc/rc.d/rc $RUNLEVEL    # 执行/etc/rc.d/rc对应某个运行级别的脚本

由此我们再看看/etc/rc.d/rc脚本的某些片段:

59# First, run the KILL scripts.    # 先执行KILL脚本
60for i in /etc/rc$runlevel.d/K* ; do    # etc下的rc对应运行级别.d下的以K开头的脚本
61
62# Check if the subsystem is already up.
63subsys=${i#/etc/rc$runlevel.d/K??}
64[ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] || continue
65check_runlevel "$i" || continue
66
67# Bring the subsystem down.
68[ -n "$UPSTART" ] && initctl emit --quiet stopping JOB=$subsys
69$i stop
70[ -n "$UPSTART" ] && initctl emit --quiet stopped JOB=$subsys
71done
72
73# Now run the START scripts.    # 现在执行START脚本
74for i in /etc/rc$runlevel.d/S* ; do    # etc下的rc对应运行级别.d下的以S开头的脚本
75
76# Check if the subsystem is already up.
77subsys=${i#/etc/rc$runlevel.d/S??}
78[ -f /var/lock/subsys/$subsys ] && continue
79[ -f /var/lock/subsys/$subsys.init ] && continue
80check_runlevel "$i" || continue
81    
82# If we're in confirmation mode, get user confirmation
83if [ "$do_confirm" = "yes" ]; then
84confirm $subsys
85rc=$?
86if [ "$rc" = "1" ]; then
87continue
88elif [ "$rc" = "2" ]; then
89do_confirm="no"
90fi
91fi
92
93update_boot_stage "$subsys"
94# Bring the subsystem up.
95[ -n "$UPSTART" ] && initctl emit --quiet starting JOB=$subsys
96if [ "$subsys" = "halt" -o "$subsys" = "reboot" ]; then
97export LC_ALL=C
98exec $i start
99fi
   100$i start
   101[ -n "$UPSTART" ] && initctl emit --quiet started JOB=$subsys
   102done
   103[ "$do_confirm" = "yes" ] && rm -f /var/run/confirm
   104exit 0

上面的脚本大致意思为,先按照etc下的rc对应运行级别.d下的以K开头的脚本按顺序(for循环)执行KILL(关闭K开头的服务),然后按照etc下的rc对应运行级别.d下的以S开头的脚本按顺序(for循环)执行START(开启S开头的服务)。例如,假如当前运行级别为5,那么表示在/etc/rc5.d/目录下以K开头的脚本按顺序关闭,S开头的脚本按顺序开启。


由此我们再看看/etc/rc5.d/下存放的脚本:

# ll /etc/rc.d/rc5.d
total 0
lrwxrwxrwx. 1 root root 15 Jun  3 19:32 K01numad -> ../init.d/numad
lrwxrwxrwx. 1 root root 16 Jun  3 19:32 K01smartd -> ../init.d/smartd
lrwxrwxrwx. 1 root root 17 Jun  3 19:30 K02oddjobd -> ../init.d/oddjobd
lrwxrwxrwx. 1 root root 16 Jun  3 19:32 K10psacct -> ../init.d/psacct
lrwxrwxrwx. 1 root root 19 Jun  3 19:30 K10saslauthd -> ../init.d/saslauthd
lrwxrwxrwx. 1 root root 17 Jun  3 23:08 K50dnsmasq -> ../init.d/dnsmasq
lrwxrwxrwx. 1 root root 20 Jun  3 19:29 K50netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 13 Jun  3 19:30 K60nfs -> ../init.d/nfs
lrwxrwxrwx. 1 root root 20 Jun  3 19:30 K69rpcsvcgssd -> ../init.d/rpcsvcgssd
lrwxrwxrwx. 1 root root 17 Jun  3 19:29 K73winbind -> ../init.d/winbind
lrwxrwxrwx. 1 root root 14 Jun  3 19:30 K74ntpd -> ../init.d/ntpd
lrwxrwxrwx. 1 root root 18 Jun  4 03:14 K75cgconfig -> ../init.d/cgconfig
lrwxrwxrwx. 1 root root 17 Jun  3 19:30 K75ntpdate -> ../init.d/ntpdate
lrwxrwxrwx. 1 root root 19 Jun  3 19:32 K75quota_nld -> ../init.d/quota_nld
lrwxrwxrwx. 1 root root 16 Jun  3 19:30 K76ypbind -> ../init.d/ypbind
lrwxrwxrwx. 1 root root 24 Jun  4 03:14 K84wpa_supplicant -> ../init.d/wpa_supplicant
lrwxrwxrwx. 1 root root 15 Jun  3 19:30 K86cgred -> ../init.d/cgred
lrwxrwxrwx. 1 root root 21 Jun  3 19:29 K87restorecond -> ../init.d/restorecond
lrwxrwxrwx. 1 root root 14 Jun  4 03:14 K88sssd -> ../init.d/sssd
lrwxrwxrwx. 1 root root 15 Jun  3 19:29 K89rdisc -> ../init.d/rdisc
lrwxrwxrwx. 1 root root 22 Jun  3 23:08 K92pppoe-server -> ../init.d/pppoe-server
lrwxrwxrwx. 1 root root 14 Jun  3 19:32 K99rngd -> ../init.d/rngd
lrwxrwxrwx. 1 root root 17 Jun  3 19:32 S01sysstat -> ../init.d/sysstat
lrwxrwxrwx. 1 root root 22 Jun  3 23:11 S02lvm2-monitor -> ../init.d/lvm2-monitor
lrwxrwxrwx. 1 root root 19 Jun  3 19:30 S08ip6tables -> ../init.d/ip6tables
lrwxrwxrwx. 1 root root 18 Jun  3 19:29 S08iptables -> ../init.d/iptables
lrwxrwxrwx. 1 root root 17 Jun  3 19:29 S10network -> ../init.d/network
lrwxrwxrwx. 1 root root 16 Jun  3 19:32 S11auditd -> ../init.d/auditd
lrwxrwxrwx. 1 root root 21 Jun  3 19:28 S11portreserve -> ../init.d/portreserve
lrwxrwxrwx. 1 root root 17 Jun  3 19:30 S12rsyslog -> ../init.d/rsyslog
lrwxrwxrwx. 1 root root 18 Jun  3 19:32 S13cpuspeed -> ../init.d/cpuspeed
lrwxrwxrwx. 1 root root 20 Jun  3 19:32 S13irqbalance -> ../init.d/irqbalance
lrwxrwxrwx. 1 root root 17 Jun  3 19:28 S13rpcbind -> ../init.d/rpcbind
lrwxrwxrwx. 1 root root 19 Jun  3 19:30 S15mdmonitor -> ../init.d/mdmonitor
lrwxrwxrwx. 1 root root 15 Jun  3 19:32 S20kdump -> ../init.d/kdump
lrwxrwxrwx. 1 root root 20 Jun  3 23:06 S22messagebus -> ../init.d/messagebus
lrwxrwxrwx. 1 root root 24 Jun  3 23:08 S23NetworkManager -> ../init.d/NetworkManager
lrwxrwxrwx. 1 root root 17 Jun  4 03:14 S24nfslock -> ../init.d/nfslock
lrwxrwxrwx. 1 root root 17 Jun  4 03:14 S24rpcgssd -> ../init.d/rpcgssd
lrwxrwxrwx. 1 root root 26 Jun  3 23:06 S25blk-availability -> ../init.d/blk-availability
lrwxrwxrwx. 1 root root 14 Jun  3 19:29 S25cups -> ../init.d/cups
lrwxrwxrwx. 1 root root 15 Jun  3 19:29 S25netfs -> ../init.d/netfs
lrwxrwxrwx. 1 root root 15 Jun  3 19:32 S26acpid -> ../init.d/acpid
lrwxrwxrwx. 1 root root 19 Jun  3 19:29 S26haldaemon -> ../init.d/haldaemon
lrwxrwxrwx. 1 root root 19 Jun  3 19:29 S26udev-post -> ../init.d/udev-post
lrwxrwxrwx. 1 root root 16 Jun  3 19:30 S28autofs -> ../init.d/autofs
lrwxrwxrwx. 1 root root 14 Jun  4 05:51 S55sshd -> ../init.d/sshd
lrwxrwxrwx. 1 root root 17 Jun  3 19:30 S80postfix -> ../init.d/postfix
lrwxrwxrwx. 1 root root 19 Jun  3 19:30 S82abrt-ccpp -> ../init.d/abrt-ccpp
lrwxrwxrwx. 1 root root 15 Jun  3 19:30 S82abrtd -> ../init.d/abrtd
lrwxrwxrwx. 1 root root 15 Jun  3 19:30 S90crond -> ../init.d/crond
lrwxrwxrwx. 1 root root 13 Jun  3 19:29 S95atd -> ../init.d/atd
lrwxrwxrwx. 1 root root 15 Jun  4 03:14 S95jexec -> ../init.d/jexec
lrwxrwxrwx. 1 root root 20 Jun  3 19:30 S99certmonger -> ../init.d/certmonger
lrwxrwxrwx. 1 root root 11 Jun  3 19:29 S99local -> ../rc.local

可以看出,这些脚本实际上都是链接到父目录下的init.d里面的具体服务(也就是/etc/rc.d/init.d),/etc/rc.d/init.d(和/etc/init.d是一样的)下保存的都是各种服务的启动脚本。


由此我们看看一下服务启动脚本:

# cat -n /etc/rc.d/init.d/sshd
 1#!/bin/bash
 2#
 3# Init file for OpenSSH server daemon
 4#
 5# chkconfig: 2345 55 25
 6# description: OpenSSH server daemon
 7#
 8# processname: sshd

可以看出,所有启动脚本都会有第4和5行,这两行的内容是为了接受chkconfig命令的执行:

# chkconfig: 2345 55 25    # 2345表示启动级别,55表示START的次序,25表示在2345外的启动级别KILL的次序
# description: OpenSSH server daemon    # 简单描述信息


chkconfig命令:

chkconfig --add SERVICE_NAME     # 添加某项服务为开机启动

如:当执行chkconfig --add sshd时,chkconfig会搜索有以上5、6行内容的脚本,同时在rc2.d、rc3.d、rc4.d、rc5.d四个启动级别目录下都创建一个S55sshd的链接文件,表示在这四个启动级别下系统启动会在(执行完所有KILL后)第55个启动(START)这个服务;在剩下的rc0.d、rc1.d、rc6.d三个启动级别目录下创建K25sshd的链接文件,表示在这三个启动级别下系统启动会第25个关闭(KILL)这个服务。由此可见,执行chkconfig命令实际上是一个创建链接以实现在某个运行级别下自动开启在某个启动级别下自动关闭的过程。如下:

# ll `find /etc/rc.d/ -name "*sshd*"`
-rwxr-xr-x. 1 root root 1766 Jun  4 04:36 /etc/rc.d/init.d/sshd
lrwxrwxrwx. 1 root root   14 Jun  4 05:50 /etc/rc.d/rc0.d/K25sshd -> ../init.d/sshd
lrwxrwxrwx. 1 root root   14 Jun  4 05:50 /etc/rc.d/rc1.d/K25sshd -> ../init.d/sshd
lrwxrwxrwx. 1 root root   14 Jun  4 05:51 /etc/rc.d/rc2.d/S55sshd -> ../init.d/sshd
lrwxrwxrwx. 1 root root   14 Jun  4 05:51 /etc/rc.d/rc3.d/S55sshd -> ../init.d/sshd
lrwxrwxrwx. 1 root root   14 Jun  4 05:51 /etc/rc.d/rc4.d/S55sshd -> ../init.d/sshd
lrwxrwxrwx. 1 root root   14 Jun  4 05:51 /etc/rc.d/rc5.d/S55sshd -> ../init.d/sshd
lrwxrwxrwx. 1 root root   14 Jun  4 05:50 /etc/rc.d/rc6.d/K25sshd -> ../init.d/sshd

chkconfig [--level RUNLEVELS] SERVICE_NAME {on|off}     # 如果省略级别--level指定,默认为2345级别

原文地址:http://blog.51cto.com/lidongfeng/2062499

时间: 2024-08-29 14:37:21

Centos6 init解析及chkconfig开机启动原理的相关文章

mac开机启动原理--apache

cd  /System/Library/LaunchDaemons/     //可以看到所有自动启动的plist文件 想关掉mac下apache的开机启动 sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 如果想再打开: sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist 系统的开机启动pl

Linux系统开机启动原理

我理解的linux系统开机顺序如下,如有分歧,可一起探讨. Linux系统开机启动的顺序是: BIOS自检,检查硬件,选择引导介质如光盘or硬盘orU盘等等. 进入bootloader,它是写在硬盘最前面的部分,大小是512字节.由446字节的mbr和64字节的DPT和2字节的返回码组成. bootloader会去读取/boot/grub/grub.conf,然后根据配置文件寻找/boot分区下面的内核文件vmlinuz.....及内核辅助文件initrd......以及加载根分区 读取/etc

chkconfig开机启动

[[email protected] ~]# who -r   查看当前系统运行在哪个级别 run-level 5  2017-02-17 10:00                   last=S [[email protected] ~]# chkconfig --level 345 zabbix_agentd on   使得zabbix_agentd在345级别时启动状态 [[email protected] ~]# chkconfig --list NetworkManager 0:o

linux 实现开机启动

Linux开机执行内核后会启动init进程,该进程根据runlevel(如x)执行/etc/rcx.d/下的程序,其下的程序是符号链接,真正的程序放在/etc/init.d/下.开机启动的程序(服务等)皆为此套路. Linux开机启动程序详解我们假设大家已经熟悉其它操作系统的引导过程,了解硬件的自检引导步骤,就只从Linux操作系统的引导加载程序(对个人电脑而言通常是LILO)开始,介绍Linux开机引导的步骤. 加载内核LILO启动之后,如果你选择了Linux作为准备引导的操作系统,第一个被加

Centos7 ActiveMQ 安装并配置为开机启动

第一步, 环境准备 更新CentOS7 ,安装epel-release,安装JDK,关闭防火墙 # yum clean all && yum update# yum install -y epel-release && yum install -y java-1.8.0-openjdk# systemctl disable firwalld# systemctl stop firewalld 第二步,获取安装包 下载ActiveMQ安装包,并解压 # wget https:

开机启动服务:chkconfig命令详解

1.查看系统运行级别 # cat /etc/inittab # Default runlevel. The runlevels used are:#   0 - halt (Do NOT set initdefault to this)#   1 - Single user mode#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)#   3 - Full multiuser mode#  

Linux系统开机启动时的工作原理

Linux系统开机启动时的工作原理也是深入了解Linux系统核心工作原理的一个很好的途径. 启动第一步--加载BIOS 当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬盘信息.内存信息.时钟信息.PnP特性等等.在此之后,计算机心里就有谱了,知道应该去读取哪个硬件设备了.在BIOS将系统的控制权交给硬盘第一个扇区之后,就开始由Linux来控制系统了. 启动第二步--读取MB

linux开机启动服务和chkconfig使用方法(转)

每个被chkconfig 管理的服务需要在对应的/etc/rc.d/init.d 下的脚本加上两行或者更多行的注释. 第一行告诉 chkconfig 缺省启动的运行级以及启动和停止的优先级.如果某服务缺省不在任何运行级启动,那么使用 - 代替运行级. 第二行对服务进行描述,可以用\ 跨行注释. 例如,random.init 包含三行: # chkconfig: 2345 20 80 # description: Saves and restores system entropy pool for

linux开机启动服务和chkconfig使用方法(自定义服务路径启动)

服务概述在linux操作系统下,经常需要创建一些服务,这些服务被做成shell脚本,这些服务需要在系统启动的时候自动启动,关闭的时候自动关闭.将 需要自动启动的脚本/etc/rc.d/init.d目录下,然后用命令chkconfig --add filename将自动注册开机启动和关机关闭.实质就是在rc0.d-rc6.d目录下生成一些文件连接,这些连接连接到/etc/rc.d /init.d目录下指定文件的shell脚本. 在文章linux下Oracle自动启动与停止(一),linux下Ora