系统相关
首页 > 系统相关> > 『Linux』/etc/inittab

『Linux』/etc/inittab

作者:互联网

文章目录

inittab文件简介

/etc/inittab文件是Linux初始化系统使用的配置文件,在不同发行版Linux的位置不同,本文使用RedHat 9研究

#
# inittab       This file describes how the INIT process should set up
#               the system in a certain run-level.
#
# Author:       Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#               Modified for RHS Linux by Marc Ewing and Donnie Barnes
#

# Default runlevel. The runlevels used by RHS 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
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
# 
id:5: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

inittab控制着系统boot和更改runlevel时所执行的代码。在我们按下电源键时,此文件就会指导系统工作

inittab文件内容

该文件定义了以下三项:

简而言之:该文件定义了默认运行等级、每个运行等级下的操作,以及进程启动、监视、重启。

一旦inittab文件中关于运行等级的所有操作执行完成,便代表了boot完成,之后就引导进入登录界面,用户可以使用账户密码登陆进入系统

配置文件中的各项操作以id:runlevel:action:process构成

Default Runlevel

以下是linux默认运行等级及其解释:

# Default runlevel. The runlevels used by RHS 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
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
# 
id:5:initdefault:
  1. 关机
  2. 单用户模式
  3. 多用户模式(无NFS)
  4. 完全多用户模式
  5. 保留未使用
  6. X11多用户模式(GUI)
  7. 重启

注释特别说明:请勿将默认启动等级设置为0和6,这将会无法引导boot启动!

之后是当前系统的默认运行状态,当前设置为XWindows模式,启动将会进入GUI

System Initialization

接下来一行定义了无论哪个等级启动,初始化系统都会执行/etc/rc.d/rc.sysinit

si::sysinit:/etc/rc.d/rc.sysinit

一旦系统运行了rc.sysinit,它将从下面的选择默认运行等级对应的代码行,并运行/etc/rc.d/rc脚本

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

process指定了运行的脚本都是/etc/rc.d/rc唯一区别就在传参是对应的runlevel

runlevel作为参数传入rc,rc执行/etc/rcN.d,N是runlevel

for i in /etc/rc$runlevel.d/S* ; do

查看当前选中的/etc/rc.d/rc5.d目录,发现全是各种启动服务的脚本:

[root@localhost root]# ls /etc/rc5.d
K05innd            K20rwhod      K46radvd      K74ntpd           S08ipchains    S56xinetd
K05saslauthd       K24irda       K50netdump    K74ups            S08iptables    S57vmware-tools-thinprint
K09privoxy         K25squid      K50snmpd      K74ypserv         S09isdn        S59hpoj
K10psacct          K28amd        K50snmptrapd  K74ypxfrd         S10network     S80sendmail
K12mailman         K34dhcrelay   K50tux        K84bgpd           S12syslog      S80spamassassin
K12mysqld          K34yppasswdd  K50vsftpd     K84ospf6d         S13irqbalance  S85gpm
K15httpd           K35atalk      K54pxe        K84ospfd          S13portmap     S90canna
K15postgresql      K35dhcpd      K55routed     K84ripd           S14nfslock     S90crond
K16rarpd           K35smb        K61ldap       K84ripngd         S17keytable    S90cups
K20bootparamd      K35vncserver  K65identd     K85zebra          S20random      S90FreeWnn
K20iscsi           K35winbind    K65kadmin     K90isicom         S24pcmcia      S90xfs
K20netdump-server  K36lisa       K65kprop      K95firstboot      S25netfs       S95anacron
K20nfs             K40mars-nwe   K65krb524     S00microcode_ctl  S26apmd        S95atd
K20rstatd          K45arpwatch   K65krb5kdc    S03vmware-tools   S28autofs      S97rhnsd
K20rusersd         K45named      K70aep1000    S05kudzu          S55sshd        S99local
K20rwalld          K45smartd     K70bcm5820    S08ip6tables      S56rawdevices  S99mdmonitor

系统会运行K开头的脚本以杀死指定进程服务,然后运行S开头的脚本试图启动可能被关闭了的进程服务

Trap CTRL-ALT-DELETE

这个字段定义了快捷键CTRL+ALT+DELETE用以关机

ca::ctrlaltdel:/sbin/shutdown -t3 -r now

Powerfail Condition

以下是处理电源故障的代码:

# 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"
# 当UPS通知电源已经失效时,假设还有几分钟的电量储备,准备从现在开始的2分钟内关机。
# 当然,这是在假定已经安装了powerd,并且UPS已经连接并正常工作。 

# 如果在关机之前电源已经恢复了,那就取消关机操作。

正如注释所示,这一段代码是用于处理UPS电源故障

Run Gettys

以下代码用以生成6个mingetty进程,且这六个进程被kill后还会respawn

# 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

只有关键进程被设置成这种方式,以保证它们不受系统中其他事情的影响。

下面代码保证了在runlevel-5启动时,保护X display manager无法被kill

# Run xdm in runlevel 5
x:5:respawn:/etc/X11/prefdm -nodaemon

因此:可以通过修改rcN.d来做到客制化kill和start,以满足用户的需求

init 0/6 与 shutdown/reoot

在Terminal可以使用init 0init 6来关机和重启,那么和shutdown nowreboot有区别吗?

先看看这两个runlevel具体运行了什么脚本

[root@localhost root]# ls /etc/rc0.d
K03rhnsd       K16rarpd           K35atalk                   K50vsftpd   K74ups         K91isdn
K05anacron     K20bootparamd      K35dhcpd                   K50xinetd   K74ypserv      K92ip6tables
K05atd         K20iscsi           K35smb                     K54pxe      K74ypxfrd      K92ipchains
K05innd        K20netdump-server  K35vncserver               K55routed   K75netfs       K92iptables
K05keytable    K20nfs             K35winbind                 K60crond    K80random      K95firstboot
K05saslauthd   K20rstatd          K36lisa                    K61hpoj     K84bgpd        K95kudzu
K09privoxy     K20rusersd         K40mars-nwe                K61ldap     K84ospf6d      K96pcmcia
K10cups        K20rwalld          K43vmware-tools-thinprint  K65identd   K84ospfd       K99mdmonitor
K10psacct      K20rwhod           K44rawdevices              K65kadmin   K84ripd        K99microcode_ctl
K10xfs         K24irda            K45arpwatch                K65kprop    K84ripngd      K99vmware-tools
K12canna       K25squid           K45named                   K65krb524   K85zebra       S00killall
K12FreeWnn     K25sshd            K45smartd                  K65krb5kdc  K86nfslock     S01halt
K12mailman     K28amd             K46radvd                   K70aep1000  K87irqbalance
K12mysqld      K30sendmail        K50netdump                 K70bcm5820  K87portmap
K15gpm         K30spamassassin    K50snmpd                   K72autofs   K88syslog
K15httpd       K34dhcrelay        K50snmptrapd               K74apmd     K90isicom
K15postgresql  K34yppasswdd       K50tux                     K74ntpd     K90network

[root@localhost root]# ls /etc/rc6.d
K03rhnsd       K16rarpd           K35atalk                   K50vsftpd   K74ups         K91isdn
K05anacron     K20bootparamd      K35dhcpd                   K50xinetd   K74ypserv      K92ip6tables
K05atd         K20iscsi           K35smb                     K54pxe      K74ypxfrd      K92ipchains
K05innd        K20netdump-server  K35vncserver               K55routed   K75netfs       K92iptables
K05keytable    K20nfs             K35winbind                 K60crond    K80random      K95firstboot
K05saslauthd   K20rstatd          K36lisa                    K61hpoj     K84bgpd        K95kudzu
K09privoxy     K20rusersd         K40mars-nwe                K61ldap     K84ospf6d      K96pcmcia
K10cups        K20rwalld          K43vmware-tools-thinprint  K65identd   K84ospfd       K99mdmonitor
K10psacct      K20rwhod           K44rawdevices              K65kadmin   K84ripd        K99microcode_ctl
K10xfs         K24irda            K45arpwatch                K65kprop    K84ripngd      K99vmware-tools
K12canna       K25squid           K45named                   K65krb524   K85zebra       S00killall
K12FreeWnn     K25sshd            K45smartd                  K65krb5kdc  K86nfslock     S01reboot
K12mailman     K28amd             K46radvd                   K70aep1000  K87irqbalance
K12mysqld      K30sendmail        K50netdump                 K70bcm5820  K87portmap
K15gpm         K30spamassassin    K50snmpd                   K72autofs   K88syslog
K15httpd       K34dhcrelay        K50snmptrapd               K74apmd     K90isicom
K15postgresql  K34yppasswdd       K50tux                     K74ntpd     K90network

可以发现这两个脚本都是在最后的基础上提前kill了进程,因此二者的效果与shutdown nowreboot是相同的

参考

From:
networkworld: unix-how-to-the-linux-etc-inittab-file
geek-university: etc-inittab
serverfault: what-is-the-difference-between-init-6-and-reboot-on-red-hat-rhel-centos

标签:sbin,respawn,Linux,inittab,etc,rc,runlevel,wait
来源: https://blog.csdn.net/Xxy605/article/details/117291423