Unix How To: The Linux /etc/inittab file

One of the files that the average Unix sysadmin rarely looks at, almost never changes and yet depends on every time he or she reboots a system is the /etc/inittab file. This modest little file controls what happens whenever a system is rebooted or forced to change run levels. Let‘s take a look at the configuration lines that tell your system what it‘s supposed to do when you hit that power button.

NOTE: The /etc/inittab files on Solaris and other Unix systems that share this way of booting follow the same general rules and most of what is described below applies equally well to those Unix variants.

To begin with, the /etc/inittab file usually starts out with a block of comments describing the content of the file or giving credit like the lines shown below.

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

Your /etc/inittab file might contain descriptions of the different run states that the system can assume. For those of you unfamiliar with this concept, a run state is basically a way to describe what is running on the system when that state is achieved. For example, run state 3 is "full multiuser mode" for most Unix systems and will have users logging in and all the expected system services running to support their use of the system.

Here‘s a sample of this section of the /etc/inittab file. Notice how it defines each run level for the sysadmin.

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

One of the most critical lines in the /etc/inittab file is the one that defines the default run level -- that is the run level that will be assumed whenever you boot the system without specifying an alternate boot level. For most Unix systems, the default run state is 3 as shown below.

id:3:initdefault:

Since this "initdefault" line contains "3" as the second field, run state 3 is the default.

Another important line on Linux systems is the sysinit line shown below. This tells the system to run the /etc/rc.d/rc.sysinit script when the system is booted.

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

Once the system has run rc.sysinit and knows the run state that it is expected to achieve on bootup, it will select the appropriate line from the list below and run the /etc/rc.d/rc script with the argument appropriate to the run level specified.

Normally, this will be "rc 3", the fourth line in the list below.

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

As you have likely surmised, the system scans the second column for the one that corresponds to the run level being requested by the initdefault setting or the controlling user‘s init command.

The /etc/rc.d/rc script will then use the specified run level to determine which set of run scripts to execute. Under normal conditions, then, rc will run with "3" as its argument and will run all the scripts in the /etc/rc3.d directory. It will run the kill scripts (those that start with an uppercase "K") first and then the start scripts (those that start with an uppercase "S") using lines like this:

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

If you look at the contents on the /etc/rc3.d directory on a Linux system, you will see something like this -- a large collection of both kill and start scripts that determine what is shut down (if running) and what is started.

$ ls /etc/rc2.d
K01dnsmasq         K36mysqld          K89netplugd         S15mdmonitor
K02avahi-daemon    K44rawdevices      K89pand             S25bluetooth
K02avahi-dnsconfd  K50netconsole      K89rdisc            S25pcscd
K02haldaemon       K50tux             K91capi             S26acpid
K02NetworkManager  K50vsftpd          K95firstboot        S26apmd
K03rhnsd           K69rpcsvcgssd      K95kudzu            S26hidd
K05atd             K72autofs          K99readahead_later  S50hplip
K05conman          K73ypbind          S00microcode_ctl    S55sshd
K05saslauthd       K74ipmi            S02lvm2-monitor     S56cups
K10dc_server       K74nscd            S04readahead_early  S56xinetd
K10psacct          K75netfs           S06cpuspeed         S80postfix
K10xfs             K85mdmpd           S08ip6tables        S85gpm
K12dc_client       K85messagebus      S08iptables         S90crond
K15httpd           K85rpcgssd         S08mcstrans         S95anacron
K20nfs             K85rpcidmapd       S09isdn             S95jexec
K24irda            K86nfslock         S10network          S97yum-updatesd
K25squid           K87multipathd      S11auditd           S99local
K30spamassassin    K87portmap         S12restorecond      S99smartd
K35dovecot         K88wpa_supplicant  S12syslog
K35winbind         K89dund            S13irqbalance

The following line will be unfamiliar to Solaris sysadmins, but the "control alt delete" sequence will be recognized by just about anyone who has worked with Windows systems

Here we have this particular key sequence signalling a shutdown operation.

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

A shutdown operation will also be initiated if a powerfail condition is recognized, as shown and explained in the lines below.

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

The following series of lines use the "respawn" option to keep six mingetty processes running on the system. If someone tries to kill one of these processes as root, the process will simply be respawned. Only critical processes are set up in this way to keep them safe from anything else happening on the system.

# 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

If you‘re curious about these processes, check them out on the running system and you‘ll see
something like this:

$ ps -ef | grep getty
root      2818     1  0 Jan09 tty3     00:00:00 /sbin/mingetty tty3
root      2819     1  0 Jan09 tty4     00:00:00 /sbin/mingetty tty4
root      2821     1  0 Jan09 tty5     00:00:00 /sbin/mingetty tty5
root      2823     1  0 Jan09 tty6     00:00:00 /sbin/mingetty tty6
root      3092     1  0 Jan09 tty2     00:00:00 /sbin/mingetty tty2
root      3224     1  0 Jan09 tty1     00:00:00 /sbin/mingetty tty1
jdoe      4877  4684  0 20:33 pts/11   00:00:00 grep getty

Here‘s another "respawn" line that you might see in your /etc/inittab line. This line keeps the xdm (X display manager) up and running in run state 5.

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

From this line and the comment near the top of the file, you can see that run state 5 -- at least on this particular Linux system -- represents multi-user mode with X11 running.

Most Linux sysadmins know how to properly add both kill and start scripts to the appropriate /etc/rc?.d directory to ensure that services are started in the appropriate run state, but they might not know exactly why adding a script to /etc/rc3.d or /etc/rc2.d has the effect that they want. A little time spent with the /etc/inittab file will likely answer any questions they might have on how this process works.

Unix How To: The Linux /etc/inittab file

时间: 2024-10-21 19:56:19

Unix How To: The Linux /etc/inittab file的相关文章

【转】linux中inittab文件详解

原文网址:http://www.2cto.com/os/201108/98426.html linux中inittab文件详解 init的进程号是1(ps -aux | less),从这一点就能看出,init进程是系统所有进程的起点,Linux在完成核内引导以后,就开始运行init程序. init程序需要读取配置文件/etc/inittab.inittab是一个不可执行的文本文件,它有若干行指令所组成. 理解Runlevel: runlevel用来表示在init进程结束之后的系统状态,在系统的硬

我使用过的Linux命令之file - 检测并显示文件类型

摘自:http://codingstandards.iteye.com/blog/804463 我使用过的Linux命令之file - 检测并显示文件类型 用途说明 file命令是用来检测并显示文件类型(determine file type). 常用参数 -b 不显示文件名称,只显示文件类型.在shell脚本中时有用. -i     显示MIME类别. -L 直接显示符号连接所指向的文件的类别. -f namefile    指定名称文件(namefile),该文件每一行为一个文件名,file

Linux 修改inittab导致系统无法启动修复

以红帽Linux为例,由于修改inittab内容不当,导致系统无法启动. 解决思路:启动时修改grub参数,进入单用户模式,将inittab文件恢复,重新启动系统即可.而且该方法不需要光盘启动,特别适合虚拟机下的inittab等文件的恢复. 解决步骤: 1.修改grub参数. 在启动Linux时,按上下键,进入启动参数选择模式. 2.按e键进入grub参数编辑模式. 3.选择启动项,将rhgb参数修改为single,敲回车返回,再按b键启动Linux. 将 grub append>ro root

Linux core dump file详解

Linux core dump file详解 http://www.cnblogs.com/langqi250/archive/2013/03/05/2944931.html

创建Docker容器时报dial unix /var/run/docker.sock: no such file or directory

在创建容器时报下列错误 [[email protected] sample]# docker build -t ovcer/nginx .Sending build context to Docker daemon 2015/01/07 06:43:51 Post http:///var/run/docker.sock/v1.15/build?rm=1&t=ovcer%2Fnginx: dial unix /var/run/docker.sock: no such file or directo

FATA[0000] Get http:///var/run/docker.sock/v1.18/images/json: dial unix /var/run/docker.sock: no such file or directory. Are you trying to connect to a TLS-enabled daemon without TLS?

>> docker imagesFATA[0000] Get http:///var/run/docker.sock/v1.18/images/json: dial unix /var/run/docker.sock: no such file or directory. Are you trying to connect to a TLS-enabled daemon without TLS? solution: Execute 'service docker restart'. If vm

linux中inittab文件详解

init的进程号是1(ps -aux | less),从这一点就能看出,init进程是系统所有进程的起点,Linux在完成核内引导以后,就开始运行init程序. init程序需要读取配置文件/etc/inittab.inittab是一个不可执行的文本文件,它有若干行指令所组成. 理解Runlevel: runlevel用来表示在init进程结束之后的系统状态,在系统的硬件中没有固定的信息来表示runlevel,它纯粹是一种软件结构.init和inittab是runlevel影响系统状态的唯一原因

UNIX高级环境编程(2)FIle I/O - 原子操作、共享文件描述符和I/O控制函数

引言: 本篇通过对open函数的讨论,引入原子操作,多进程通信(共享文件描述符)和内核相关的数据结构. 还会讨论集中常见的文件IO控制函数,包括: dup和dup2 sync,fsync和fdatasync fcntl ioctl /dev/fd ? 一.文件共享 这里所说的文件共享主要指的是进程间共享打开的文件. 这一节主要讨论文件在进程间共享的理论基础和数据结构,不涉及具体的技术实现,不同的系统可能会有不同的实现. 每一个打开的文件,涉及内核中的三种数据结构,这三种数据结构也是文件在进程间共

Linux 操作系统下 File.Delete 的表现

在我的上一篇随笔"对 File.Delete 方法的一点看法"中,在 Windows 操作系统上对 File.Delete 方法进行了测试.这次,在 Linux 操作系统上使用 mono 的最新版本 1.2.5 版对 File.Delete 方法进行测试. 下面是我的运行 Linux 操作系统的计算机的基本信息: [email protected]:~/work$ cat /etc/issue.net Ubuntu 6.10 [email protected]:~/work$ unam