1. runlevel
运行级别(runlevel)SysVinit
01Ss23456

1.1 查看当前运行级别

[root@CentOS ~]# runlevel 
N 3
[root@CentOS ~]# who -r
         run-level 3  2016-12-06 04:29

1.2 切换运行级别

/sbin/init/sbin/telinitman init
# 关机
[root@CentOS ~]# init 0

# 比如当前运行级别为3,想要切换到图形界面 5
[root@CentOS ~]# runlevel 
N 3
[root@CentOS ~]# init 5

# 重启
[root@CentOS ~]# init 6

更改系统运行级别后,任何未被新运行级别指定的正在运行的进程将被终止(先发送 SIGTERM 信号,再发送 SIGKILL 信号)

2. SysVinit 如何初始化系统
init/sbin/init

2.1 /etc/inittab

/sbin/init/etc/inittab
[root@CentOS ~]# cat /etc/inittab 
#
# inittab       This file describes how the INIT process should set up
#               the system in a certain run-level.
#
id:3:initdefault:

# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit

l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6

# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now

# When our UPS tells us power has failed, assume we have a few minutes
# of power left.  Schedule a shutdown for 2 minutes from now.
# This does, of course, assume you have powerd installed and your
# UPS connected and working correctly.  
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"

# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"


# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6

# Run xdm in runlevel 5
x:5:respawn:/etc/X11/prefdm -nodaemon
/etc/inittabman inittabentryid:runlevel:action:process
idrunlevelactionactionprocessaction
action
initdefaultsysinit/etc/rc.d/rc.sysinitwaitinitctrlaltdelrespawn
/etc/inittab
id:3:initdefault:si::sysinit:/etc/rc.d/rc.sysinitl3:3:wait:/etc/rc.d/rc 3/etc/rc.d/rc 3

2.2 /etc/rc.d/rc.sysinit

这个 Shell 脚本在系统启动时被执行一次,内容还挺多的,会 Shell 脚本的慢慢看就明白了,它大致在做以下几个工作:

SELINUX=enforcingSELINUX=disabledWelcome to CentOS/bin/dmesg -n $LOGLEVEL/sbin/modprobe/etc/sysconfig/modules/*.modulessysctl -e -p /etc/sysctl.conf >/dev/null 2>&1读写(rw, read write)rootfs/etc/fstabfsck

2.3 /etc/rc.d/rc

3X Display Manager/etc/inittab
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
/etc/rc.d/rc 3/etc/rc3.d/etc/rc.d/rc3.d/
/etc/rc3.d/文件名KSKS/etc/rc.d/rcfor/etc/rc3.d/
[root@CentOS ~]# ls -l /etc/rc3.d/
total 300
lrwxrwxrwx 1 root root 17 Dec 26 05:08 K01dnsmasq -> ../init.d/dnsmasq
lrwxrwxrwx 1 root root 24 Dec 26 05:08 K02avahi-dnsconfd -> ../init.d/avahi-dnsconfd
lrwxrwxrwx 1 root root 24 Dec 26 05:10 K02NetworkManager -> ../init.d/NetworkManager
lrwxrwxrwx 1 root root 17 Dec 26 05:08 K02oddjobd -> ../init.d/oddjobd
lrwxrwxrwx 1 root root 16 Dec 26 05:07 K05conman -> ../init.d/conman
lrwxrwxrwx 1 root root 19 Dec 26 05:08 K05saslauthd -> ../init.d/saslauthd
lrwxrwxrwx 1 root root 17 Dec 26 05:09 K05wdaemon -> ../init.d/wdaemon
lrwxrwxrwx 1 root root 16 Dec 26 05:07 K10psacct -> ../init.d/psacct
lrwxrwxrwx 1 root root 14 Dec 26 05:07 K10tcsd -> ../init.d/tcsd
lrwxrwxrwx 1 root root 15 Dec 26 05:08 K15httpd -> ../init.d/httpd
lrwxrwxrwx 1 root root 13 Dec 26 05:10 K20nfs -> ../init.d/nfs
lrwxrwxrwx 1 root root 14 Dec 26 05:10 K24irda -> ../init.d/irda
lrwxrwxrwx 1 root root 19 Dec 26 05:10 K35vncserver -> ../init.d/vncserver
lrwxrwxrwx 1 root root 17 Dec 26 05:14 K35winbind -> ../init.d/winbind
lrwxrwxrwx 1 root root 20 Dec 26 05:08 K50netconsole -> ../init.d/netconsole
lrwxrwxrwx 1 root root 15 Dec 26 05:08 K50snmpd -> ../init.d/snmpd
lrwxrwxrwx 1 root root 19 Dec 26 05:08 K50snmptrapd -> ../init.d/snmptrapd
lrwxrwxrwx 1 root root 20 Dec 26 05:10 K69rpcsvcgssd -> ../init.d/rpcsvcgssd
lrwxrwxrwx 1 root root 16 Dec 26 05:14 K73ypbind -> ../init.d/ypbind
lrwxrwxrwx 1 root root 14 Dec 26 05:07 K74nscd -> ../init.d/nscd
lrwxrwxrwx 1 root root 14 Dec 26 05:34 K74ntpd -> ../init.d/ntpd
lrwxrwxrwx 1 root root 15 Dec 26 05:09 K85mdmpd -> ../init.d/mdmpd
lrwxrwxrwx 1 root root 20 Dec 26 05:07 K87multipathd -> ../init.d/multipathd
lrwxrwxrwx 1 root root 24 Dec 26 05:08 K88wpa_supplicant -> ../init.d/wpa_supplicant
lrwxrwxrwx 1 root root 14 Dec 26 05:09 K89dund -> ../init.d/dund
lrwxrwxrwx 1 root root 18 Dec 26 05:07 K89netplugd -> ../init.d/netplugd
lrwxrwxrwx 1 root root