目录
作爲系統管理員,粗略地瞭解 Debian 系統的啓動和配置方式是明智的。儘管準確的細節在安裝的軟件包及對應的文檔中,但這些知識對我們大多數人來說都是必須掌握的。
筆者基於自己和其他人的過往及現在的知識,盡己所能地提供關於 Debian 系統的知識要點及其配置的快速概覽作爲讀者的參考。由於 Debian 系統在不斷地更新中,系統的狀況可能已經有所變化。在對系統做任何修改之前,請參考各個軟件包的最新文檔。
![]() |
警告 |
---|---|
本章是基於 2013 年發佈的 Debian 7.0 ( |
計算機系統從上電事件到能爲用戶提供完整的操作系統(OS)功能爲止,需要經歷幾個階段的啓動過程。
爲簡便起見,筆者將討論範圍限定在具有默認安裝的典型 PC 平臺上。
典型的啓動過程像是一個四級的火箭。每一級火箭將系統控制權交給下一級。
當然,這些階段可以有不同的配置。比如,你編譯了自己的內核,則可能會跳過迷你 Debian 系統的步驟。因此,在讀者親自確認之前,請勿假定自己系統的情況也是如此。
![]() |
注意 |
---|---|
對於 SUN 或 Macintosh 系統等非傳統 PC 平臺來說,ROM 上的 BIOS 及磁盤上的分區可能大不相同(第 9.5.2 节 “Disk partition configuration”)。對於這種情況,請另尋對應平臺相關的文檔。 |
BIOS 是啓動過程的第一階段,在上電事件後開始。CPU 的程序計數器在上電事件後被初始化爲一個特定的內存地址,駐留在只讀存儲器(ROM)中的 BIOS 就是從這個特定的內存地址開始執行。
BIOS 執行硬件的基本初始化(POST: 上電自檢)並將系統控制權交給你指定的下一步驟。BIOS 通常和硬件一同提供。
BIOS 啓動屏幕通常指示了進入 BIOS 配置界面所需的按鍵。流行的按鍵是 F1、F2、F10、Esc、Ins 和 Del 鍵。假如你的啓動屏幕被一個漂亮的圖形界面隱藏,你可以按下某些按鍵(比如 ESC)取消隱藏。這些按鍵高度依賴於硬件。
硬件位置和 BIOS 啓動的代碼的優先級可以在 BIOS 配置界面中選擇。通常,在已選擇的設備(硬盤、軟件、CD-ROM……)中,最先找到的設備的最開始的幾個扇區將被加載到內存,並執行其中的初始化代碼。初始化代碼可以是以下任意一種。
引導加載代碼
類似 FreeDOS 這樣的過濾型操作系統的內核代碼
能夠加載到如此小的空間中的目標操作系統的內核代碼
通常,系統從主硬件的特定分區中引導。傳統 PC 硬盤的最開始兩個扇區中包含了主引導記錄(MBR)。在 MBR 的末尾記錄了磁盤分區信息及引導選擇。BIOS 中執行的首段引導加載代碼佔據了 MBR 的其餘部分。
引導加載程序是啓動過程的第二階段,由 BIOS 啓動。引導加載程序將系統內核映像和 initrd 映像加載到內存並將控制權交給它們。initrd 映像是根文件系統映像,其支持程度依賴於所使用的引導加載程序。
Debian 系統通常使用 Linux 內核作爲其默認的系統內核。當前 2.6/3.x 版本 Linux 內核的 initrd 映像從技術上說是 initramfs(初始化 RAM 文件系統)映像。initramfs 映像是根文件系統中所有文件的 cpio 歸檔再經過 gzip 壓縮得到。
![]() |
警告 |
---|---|
使用新的 multi-segment initramfs 之後,上述內容已不正確。請參見錯誤 #790100。 |
Debian 系統默認將 PC 平臺的 GRUB 引導加載程序的第一階段代碼安裝在 MBR 中。可用的引導加載程序和配置選項如下。
表 3.1. 引導加載程序列表
軟件包 | 流行度 | 大小 | initrd | 引導加載程序 | 說明 |
---|---|---|---|---|---|
grub-legacy | V:0, I:2 | 710 | 支持 | 傳統 GRUB | 可智能識別磁盤分區和文件系統(例如 vfat、ext3…)。 |
grub-pc | V:35, I:878 | 542 | 支持 | GRUB 第 2 版 | 可智能識別磁盤分區和文件系統(例如 vfat、ext4…)。(默認安裝) |
grub-rescue-pc | V:0, I:2 | 5238 | 支持 | GRUB 第 2 版 | 此爲 GRUB 第 2 版的可引導修復映像(CD 和軟盤)(PC / BIOS 版本) |
lilo | V:0, I:5 | 685 | 支持 | Lilo | 依賴於數據在硬盤上的扇區位置。(較老) |
syslinux | V:6, I:66 | 294 | 支持 | Isolinux | 可識別 ISO9660 文件系統。引導 CD 使用此項。 |
syslinux | V:6, I:66 | 294 | 支持 | Syslinux | 可識別 MSDOS 文件系統(FAT)。引導軟盤使用此項。 |
loadlin | V:0, I:1 | 82 | 支持 | Loadlin | 新系統從 FreeDOS 或 MSDOS 中啓動。 |
mbr | V:1, I:12 | 50 | 不支持 | Neil Turton 的 MBR | 此爲取代 MSDOS MBR 的自由軟件。只可識別硬盤分區。 |
![]() |
警告 |
---|---|
假如沒有從 |
傳統 GRUB 的菜單配置文件位於 /boot/grub/menu.lst
。例如,文件中有如下的配置條目。
title Debian GNU/Linux root (hd0,2) kernel /vmlinuz root=/dev/hda3 ro initrd /initrd.img
GRUB 第 2 版的菜單配置文件位於 /boot/grub/grub.cfg
。此文件由
/usr/sbin/update-grub
根據
"/etc/grub.d/*
" 中的模板及
"/etc/default/grub
" 中的設置自動生成。例如,文件中有如下的配置條目。
menuentry "Debian GNU/Linux" { set root=(hd0,3) linux /vmlinuz root=/dev/hda3 initrd /initrd.img }
這些示例中,GRUB 參數的含義如下。
表 3.2. GRUB 參數的含義
GRUB 參數 | 說明 |
---|---|
root
|
使用主磁盤的第 3 個分區,在傳統 GRUB 中將此參數設置爲 "(hd0,2) ",在 GRUB 第 2
版中將此參數設置爲 "(hd0,3) "
|
kernel
|
使用位於 "/vmlinuz " 的內核,同時將 "root=/dev/hda3
ro " 作爲參數傳遞給內核
|
initrd
|
使用 initrd/initramfs 映像,其位於
"/initrd.img "
|
![]() |
注意 |
---|---|
傳統 GRUB 使用的分區號爲 Linux 內核及各種實用工具使用的分區號減 1。GRUB 第 2 版修復了這個問題。 |
![]() |
提示 |
---|---|
在標識一個塊設備時,可能需要使用 UUID(參見第 9.5.3 节 “Accessing partition using UUID”)而不是類似
" |
![]() |
提示 |
---|---|
如果使用了 GRUB,內核的啓動參數可以在
|
![]() |
提示 |
---|---|
通過使用鏈式引導技術,你可以在一個引導裝載程序中啓動另一個引導裝載程序。 |
參見 “info grub
” 及
grub-install(8)。
迷你 Debian 系統是啓動流程的第三階段,由引導加載程序啓動。它會在內存中運行系統內核和根文件系統。這是啓動流程的一個可選準備階段。
![]() |
注意 |
---|---|
“迷你 Debian 系統”是筆者自創的術語,用於在本文檔中描述啓動流程的第三個階段。這個系統通常被稱爲 initrd 或 initramfs 系統。內存中類似的系統在 Debian 安裝程序中使用。 |
/init
腳本是內存中的根文件系統上執行的第一個程序。它是一個 shell
腳本程序,用於內存在用戶空間的初始化並把控制權交給下一階段。迷你 Debian
系統能夠在主引導流程之前添加內核模塊或以加密形式掛載根文件系統,使引導流程更加靈活。
通過給內核添加 “break=init
" 等啓動參數,你可以中斷這部分啓動流程以獲取 root
shell。更多中斷條件請參見 ”/init
“ 腳本。這個 shell
環境已足夠成熟,你可通過它很好地檢查機器的硬件。
迷你 Debian 系統中可用的命令是精簡過的,且主要由一個稱爲 busybox(1) 的 GNU 工具提供。
![]() |
小心 |
---|---|
當在一個只讀的根文件系統上時,使用 |
常規 Debian 系統是啓動流程的第四階段,由迷你 Debian 系統啓動。迷你 Debian 系統的內核在此環境下繼續運行。根文件系統將由內存切換到實際的硬盤文件系統上。
init 程序是系統執行的第一個程序(PID=1),它啓動其它各種程序以完成主引導流程。init
程序的默認路徑是 ”/sbin/init
“,但可通過內核啓動參數修改,例如
”init=/path/to/init_program
"。
默認的 init 程序一直在變化中:
![]() |
提示 |
---|---|
" |
![]() |
提示 |
---|---|
你的系統中實際使用的 init 命令可以使用 “ |
表 3.3. Debian 系統啓動工具列表
軟件包 | 流行度 | 大小 | 說明 |
---|---|---|---|
initscripts
|
V:867, I:986 | 207 | 用於初始化和關閉系統的腳本 |
sysvinit-core
|
V:19, I:24 | 221 | 類 System V 的 init(8) 工具 |
sysv-rc
|
V:867, I:985 | 122 123 | 類 System V 的運行級別修改機制 |
sysvinit-utils
|
V:875, I:999 | 106 | 類 System V 的實用工具(startpar(8),bootlogd(8),……) |
lsb-base
|
V:876, I:999 | 49 | Linux 標準規範 3.2 版的 init 腳本功能 |
insserv
|
V:814, I:962 | 139 | 利用 LSB init.d 腳本依賴性來組織啓動步驟的工具 |
systemd
|
V:597, I:682 | 9276 |
基於事件且支持並發的
init(8)
守護進程(可替代 sysvinit )
|
uswsusp
|
V:4, I:14 | 493 | tools to use userspace software suspend provided by Linux |
kexec-tools
|
V:1, I:7 | 258 | kexec tool for kexec(8) reboots (warm reboot) |
bootchart2
|
V:0, I:1 | 63 | boot process performance analyser |
pybootchartgui
|
V:0, I:1 | 177 | boot process performance analyser (visualisation) |
mingetty
|
V:0, I:3 | 24 | console-only getty(8) |
mgetty
|
V:0, I:2 | 289 | smart modem getty(8) replacement |
![]() |
提示 |
---|---|
See Debian wiki: BootProcessSpeedup for the latest tips to speed up the boot process. |
![]() |
小心 |
---|---|
The current default Debian system doesn't use SysV-style init. Please read other resources for the modern systemd based init. See The Debian Administrator's Handbook |
This section describes how the good old SysV-style init used to boot the system. Your Debian system does not function exactly as described here but it is quite educational to know this basics since the newer init system tends to offer equivalent functionalities.
The SysV-style boot process essentially goes through the following.
The Debian system goes into runlevel N
(none) to initialize the system by following the
"/etc/inittab
" description.
The Debian system goes into runlevel S to initialize the system under the single-user mode to complete hardware initialization etc.
The Debian system goes into one of the specified multi-user runlevels (2 to 5) to start the system services.
The initial runlevel used for multi-user mode is specified with the
"init=
" kernel boot parameter or in the "initdefault"
line of the "/etc/inittab
". The Debian system as
installed starts at the runlevel 2.
All actual script files executed by the init system are located in the
directory "/etc/init.d/
".
See
init(8),
inittab(5),
and "/usr/share/doc/sysv-rc/README.runlevels.gz
" for the
exact explanation.
Each runlevel uses a directory for its configuration and has specific meaning as the following.
表 3.4. List of runlevels and description of their usage
runlevel | 目錄 | description of runlevel usage |
---|---|---|
N
|
none |
system bootup (NONE) level (no "/etc/rcN.d/ " directory)
|
0
|
/etc/rc0.d/
|
halt the system |
S
|
/etc/rcS.d/
|
single-user mode on boot (alias: "s ")
|
1
|
/etc/rc1.d/
|
single-user mode switched from multi-user mode |
2
|
/etc/rc2.d/
|
multi-user mode |
3
|
/etc/rc3.d/
|
,, |
4
|
/etc/rc4.d/
|
,, |
5
|
/etc/rc5.d/
|
,, |
6
|
/etc/rc6.d/
|
reboot the system |
7
|
/etc/rc7.d/
|
valid multi-user mode but not normally used |
8
|
/etc/rc8.d/
|
,, |
9
|
/etc/rc9.d/
|
,, |
You can change the runlevel from the console to, e.g., 4 by the following.
$ sudo telinit 4
![]() |
小心 |
---|---|
The Debian system does not pre-assign any special meaning differences among the runlevels between 2 and 5. The system administrator on the Debian system may change this. (I.e., Debian is not Red Hat Linux nor Solaris by Sun Microsystems nor HP-UX by Hewlett Packard nor AIX by IBM nor …) |
![]() |
小心 |
---|---|
The Debian system does not populate directories for the runlevels between 7 and 9 during installation. Traditional Unix variants don't use these runlevels. |
When init(8) or telinit(8) commands goes into the runlevel to "<n>", the system basically executes the initialization scripts as follows.
The script names starting with a "K
" in
"/etc/rc<n>.d/
" are executed in alphabetical order
with the single argument "stop
". (killing services)
The script names starting with an "S
" in
"/etc/rc<n>.d/
" are executed in alphabetical order
with the single argument "start
". (starting services)
For example, if you had the links "S10sysklogd
" and
"S20exim4
" in a runlevel directory,
"S10sysklogd
" which is symlinked to
"../init.d/sysklogd
" would run before
"S20exim4
" which is symlinked to
"../init.d/exim4
".
This simple sequential initialization system is the classical System V style boot system and was used up to the
Debian lenny
system.
The recent Debian system is optimized to execute the initialization scripts concurrently, instead.
The insserv(8) command uses the LSB package dependency information and calculates the dependencies between all scripts.
See "/usr/share/doc/insserv/README.Debian
".
The LSB package dependency information defined in the header of init scripts ensures availabilities of required resources.
![]() |
警告 |
---|---|
It is not advisable to make any changes to symlinks in
" |
For example, let's set up runlevel system somewhat like Red Hat Linux as the following.
init
starts the system in runlevel=3 as the default.
init
does not start
gdm3(1)
in runlevel=(0,1,2,6).
init
starts
gdm3(1)
in runlevel=(3,4,5).
This can be done by using editor on the "/etc/inittab
"
file to change starting runlevel and using user friendly runlevel management
tools such as sysv-rc-conf
or bum
to
edit the runlevel. If you are to use command line only instead, here is how
you do it (after the default installation of the gdm3
package and selecting it to be the choice of display manager).
# cd /etc/rc2.d ; mv S21gdm3 K21gdm3 # cd /etc ; perl -i -p -e 's/^id:.:/id:3:/' inittab
Please note the "/etc/X11/default-display-manager
" file
is checked when starting the display manager daemons:
xdm
, gdm3
, sddm
,
and wdm
.
![]() |
注意 |
---|---|
You can still start X from any console shell with the startx(1) command. |
The default parameter for each init script in
"/etc/init.d/
" is given by the corresponding file in
"/etc/default/
" which contains environment variable
assignments only. This choice of
directory name is specific to the Debian system. It is roughly the
equivalent of the "/etc/sysconfig
" directory found in
Red Hat Linux and other distributions.
For example, "/etc/default/cron
" can be used to control
how "/etc/init.d/cron
" works.
The "/etc/default/rcS
" file can be used to customize
boot-time defaults for
motd(5),
sulogin(8),
etc.
If you cannot get the behavior you want by changing such variables then you may modify the init scripts themselves. These are configuration files editable by system administrators.
The kernel maintains the system hostname. The init script in runlevel S which is
symlinked to "/etc/init.d/hostname.sh
" sets the system
hostname at boot time (using the hostname
command) to the
name stored in "/etc/hostname
". This file should contain
only the system hostname, not a fully
qualified domain name.
To print out the current hostname run hostname(1) without an argument.
Although the root filesystem is mounted by the kernel when it is started, other filesystems are mounted in the runlevel S by the following init scripts.
"/etc/init.d/mountkernfs.sh
" for kernel filesystems in
"/proc
", "/sys
", etc.
"/etc/init.d/mountdevsubfs.sh
" for virtual filesystems in
"/dev
"
"/etc/init.d/mountall.sh
" for normal filesystems using
"/etc/fstab
"
"/etc/init.d/mountnfs.sh
" for network filesystems
using"/etc/fstab
"
The mount options of special kernel filesystems (procfs, sysfs, and tmpfs
for /proc
, /sys
,
/tmp
, /run
, etc.) are set in
"/etc/default/rcS
". See
rcS(5).
The mount options of normal disk and network filesystems are set in
"/etc/fstab
". See 第 9.5.7 节 “Optimization of filesystem by mount options”.
![]() |
注意 |
---|---|
The actual mounting of network filesystems waits for the start of the network interface. |
![]() |
警告 |
---|---|
After mounting all the filesystems, temporary files in
" |
Network interfaces are initialized in runlevel S by the init script
symlinked to "/etc/init.d/ifupdown-clean
" and
"/etc/init.d/ifupdown
". See 第 5 章 網絡設置 for how to configure them.
Many network services (see 第 6 章 Network applications) are
started under multi-user mode directly as daemon processes at boot time by
the init script, e.g., "/etc/rc2.d/S20exim4
" (for
RUNLEVEL=2) which is a symlink to "/etc/init.d/exim4
".
Some network services can be started on demand using the super-server inetd
(or its
equivalents). The inetd
is started at boot time by
"/etc/rc2.d/S20inetd
" (for RUNLEVEL=2) which is a symlink
to "/etc/init.d/inetd
". Essentially,
inetd
allows one running daemon to invoke several others,
reducing load on the system.
Whenever a request for service arrives at super-server inetd
, its
protocol and service are identified by looking them up in the databases in
"/etc/protocols
" and "/etc/services
".
inetd
then looks up a normal Internet service in the
"/etc/inetd.conf
" database, or a Open Network Computing Remote
Procedure Call (ONC RPC)/Sun RPC based service in
"/etc/rpc.conf
".
Sometimes, inetd
does not start the intended server
directly but starts the TCP wrapper
program,
tcpd(8),
with the intended server name as its argument in
"/etc/inetd.conf
". In this case, tcpd
runs the appropriate server program after logging the request and doing some
additional checks using "/etc/hosts.deny
" and
"/etc/hosts.allow
".
For system security, disable as much network service programs as possible. See 第 4.6.4 节 “Restricting access to some server services”.
See
inetd(8),
inetd.conf(5),
protocols(5),
services(5),
tcpd(8),
hosts_access(5),
hosts_options(5),
rpcinfo(8),
portmap(8),
and "/usr/share/doc/portmap/portmapper.txt.gz
".
The system message can be customized by
"/etc/default/rsyslog
" and
"/etc/rsyslog.conf
" for both the log file and on-screen
display. See
rsyslogd(8)
and
rsyslog.conf(5).
See also 第 9.2.2 节 “Log analyzer”.
The kernel message can be customized by
"/etc/default/klogd
" for both the log file and on-screen
display. Set "KLOGD='-c 3'
" in this file and run
"/etc/init.d/klogd restart
". See
klogd(8).
You may directly change the error message level by the following.
# dmesg -n3
表 3.5. List of kernel error levels
error level value | error level name | 說明 |
---|---|---|
0 | KERN_EMERG | system is unusable |
1 | KERN_ALERT | action must be taken immediately |
2 | KERN_CRIT | critical conditions |
3 | KERN_ERR | error conditions |
4 | KERN_WARNING | warning conditions |
5 | KERN_NOTICE | normal but significant condition |
6 | KERN_INFO | informational |
7 | KERN_DEBUG | debug-level messages |
For Linux kernel 2.6 and newer, the udev system provides mechanism for the automatic hardware discovery and initialization (see udev(7)). Upon discovery of each device by the kernel, the udev system starts a user process which uses information from the sysfs filesystem (see 第 1.2.12 节 “procfs 和 sysfs”), loads required kernel modules supporting it using the modprobe(8) program (see 第 3.3.1 节 “The kernel module initialization”), and creates corresponding device nodes.
![]() |
提示 |
---|---|
If " |
The name of device nodes can be configured by udev rule files in
"/etc/udev/rules.d/
". Current default rules tend to
create dynamically generated names resulting non-static device names except
for cd and network devices. By adding your custom rules similar to what cd
and network devices do, you can generate static device names for other
devices such as USB memory sticks, too. See "Writing udev rules" or
"/usr/share/doc/udev/writing_udev_rules/index.html
".
Since the udev system is somewhat a moving target, I leave details to other documentations and describe the minimum information here.
![]() |
提示 |
---|---|
For mounting rules in " |
The modprobe(8) program enables us to configure running Linux kernel from user process by adding and removing kernel modules. The udev system (see 第 3.3 节 “The udev system”) automates its invocation to help the kernel module initialization.
There are non-hardware modules and special hardware driver modules as the
following which need to be pre-loaded by listing them in the
"/etc/modules
" file (see
modules(5)).
TUN/TAP modules providing virtual Point-to-Point network device (TUN) and virtual Ethernet network device (TAP),
netfilter modules providing netfilter firewall capabilities (iptables(8), 第 5.9 节 “Netfilter infrastructure”), and
watchdog timer driver modules.
The configuration files for the
modprobe(8)
program are located under the "/etc/modprobes.d/
"
directory as explained in
modprobe.conf(5).
(If you want to avoid some kernel modules to be auto-loaded, consider to
blacklist them in the "/etc/modprobes.d/blacklist
" file.)
The "/lib/modules/<version>/modules.dep
" file
generated by the
depmod(8)
program describes module dependencies used by the
modprobe(8)
program.
![]() |
注意 |
---|---|
If you experience module loading issues with boot time module loading or
with
modprobe(8),
" |
The modinfo(8) program shows information about a Linux kernel module.
The
lsmod(8)
program nicely formats the contents of the
"/proc/modules
", showing what kernel modules are
currently loaded.
![]() |
提示 |
---|---|
You can identify exact hardware on your system. See 第 9.4.3 节 “Hardware identification”. |
![]() |
提示 |
---|---|
You may configure hardware at boot time to activate expected hardware features. See 第 9.4.4 节 “Hardware configuration”. |
![]() |
提示 |
---|---|
You can probably add support for your special device by recompiling the kernel. See 第 9.9 节 “The kernel”. |