Quick HOWTO : Ch07 : The Linux Boot Process/zh
文章出处: |
{{#if: | {{{2}}} | http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch07_:_The_Linux_Boot_Process }} |
点击翻译: |
English {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/af | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|Afrikaans| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/af|Afrikaans]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/ar | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|العربية| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/ar|العربية]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/az | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|azərbaycanca| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/bcc | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|جهلسری بلوچی| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/bg | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|български| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/bg|български]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/br | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|brezhoneg| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/br|brezhoneg]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/ca | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|català| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/ca|català]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/cs | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|čeština| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/cs|čeština]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/de | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|Deutsch| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/de|Deutsch]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/el | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|Ελληνικά| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/es | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|español| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/es|español]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/fa | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|فارسی| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/fa|فارسی]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/fi | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|suomi| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/fi|suomi]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/fr | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|français| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/fr|français]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/gu | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|ગુજરાતી| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/he | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|עברית| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/he|עברית]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/hu | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|magyar| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/hu|magyar]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/id | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|Bahasa Indonesia| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/it | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|italiano| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/it|italiano]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/ja | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|日本語| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/ja|日本語]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/ko | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|한국어| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/ko|한국어]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/ksh | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|Ripoarisch| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/mr | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|मराठी| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/mr|मराठी]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/ms | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|Bahasa Melayu| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/nl | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|Nederlands| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/nl|Nederlands]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/no | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|norsk| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/no|norsk]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/oc | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|occitan| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/oc|occitan]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/pl | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|polski| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/pl|polski]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/pt | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|português| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/pt|português]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/ro | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|română| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/ro|română]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/ru | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|русский| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/ru|русский]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/si | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|සිංහල| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/si|සිංහල]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/sq | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|shqip| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/sq|shqip]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/sr | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|српски / srpski| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/sv | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|svenska| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/sv|svenska]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/th | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|ไทย| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/th|ไทย]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/tr | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|Türkçe| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/tr|Türkçe]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/vi | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|Tiếng Việt| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/yue | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|粵語| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/yue|粵語]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/zh | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|中文| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/zh|中文]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/zh-hans | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|中文(简体)| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: Quick HOWTO : Ch07 : The Linux Boot Process | Quick HOWTO : Ch07 : The Linux Boot Process | {{#if: | :}}Quick HOWTO : Ch07 : The Linux Boot Process/zh}}/zh-hant | • {{#if: Quick HOWTO : Ch07 : The Linux Boot Process|中文(繁體)| [[::Quick HOWTO : Ch07 : The Linux Boot Process/zh/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:Quick HOWTO : Ch07 : The Linux Boot Process|:Quick HOWTO : Ch07 : The Linux Boot Process/zh|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :Quick HOWTO : Ch07 : The Linux Boot Process/zh/zh | | {{#ifexist: Quick HOWTO : Ch07 : The Linux Boot Process/zh/zh | | {{#ifeq: {{#titleparts:Quick HOWTO : Ch07 : The Linux Boot Process/zh|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:Quick HOWTO : Ch07 : The Linux Boot Process/zh|1|-1|}} | zh | | }}
简介
了解Linux如何启动很有用,之後您就可以改变登录界面的类型,或者自由选择启动时加载哪些程序。欢迎阅读下面的细节。
Linux启动顺序
您或许还记得,当初安装Linux时,安装程序显示了一份硬盘分区的列表,包含各个分区的大小,您的文件系统将安装在这些分区上。
当为各个分区分配磁盘空间时,每个分区的第一个数据单元,或称之为“扇区”,总是预留给启动代码。整块硬盘的第一个扇区也用于同样的目的,该扇区被称作主引导记录(MBR)。
当由硬盘启动PC时,BIOS被加载,之後运行MBR中的启动引导代码。MBR需要知道哪个分区的引导扇区中包含了操作系统的启动引导代码,之後MBR会试着启动其中的一个。
Fedora Linux发行版使用了非常复杂的GRUB启动引导程序,该程序无法放入MBR的512字节中。GRUB启动引导程序仅搜寻特定的启动分区,找到後加载第二阶段的启动引导程序。GRUB从包含各类操作系统和操作系统的启动参数的配置文件/boot/grub/grub.conf中读取启动配置数据。当GRUB启动引导程序完成後,第二阶段的启动引导程序在屏幕上显示有Fedora标记的所有你选择的操作系统核心配置。
注意: 在一些操作系统中,如Debian / Ubuntu,配置文件 /boot/grub/grub.conf 可能会被下面的文件取代 /boot/grub/menu.lst
图7-1显示了能够同时启动Fedora Linux和Windows 2000系统的一个典型grub.conf文件。配置文件的结构将在第33章讨论, "Modifying the Kernel to Improve Performance"。
图 7-1 grub.conf 文件示例
default=0 timeout=10 splashimage=(hd0,0)/grub/splash.xpm.gz title Fedora Core (2.6.8-1.521) root (hd0,0) kernel /vmlinuz-2.6.8-1.521 ro root=LABEL=/ initrd /initrd-2.6.8-1.521.img title Windows 2000 rootnoverify (hd0,1) chainloader +1
当Linux开始启动内核时,首先运行/sbin/init脚本程序进行一些系统检查,如文件系统完整性的确认和操作系统正常运作的重要程序的启动。 Linux开始检测/etc/initab文件,并决定Linux的全部运行模式和运行级别。有效的运行级别列表见表7-1。
表 7-1 Linux 运行级别
Mode | Directory | Run Level Description |
---|---|---|
0 | /etc/rc.d/rc0.d | 关机 |
1 | /etc/rc.d/rc1.d | 单用户模式 |
2 | /etc/rc.d/rc2.d | 未使用,用户自定义 |
3 | /etc/rc.d/rc3.d | 多用户模式,无图形界面 |
4 | /etc/rc.d/rc4.d | 未使用(用户定义) |
5 | /etc/rc.d/rc5.d | 多用户模式(图形界面) |
6 | /etc/rc.d/rc6.d | 重启 |
init程序依据选定的运行级别执行在子目录 /etc/rc.d
下的脚本程序.。运行级别0-6分别对应子目录中的 /etc/rc.d/rc0.d
-/etc/rc.d/rc6.d
。
以下是/etc/rc.d/rc3.d
目录下的脚本文件列表
[root@bigboy tmp]# ls /etc/rc.d/rc3.d ... ... K75netfs K96pcmcia ... ... ... ... K86nfslock S05kudzu ... ... ... ... K87portmap S09wlan ... ... ... ... K91isdn S10network ... ... ... ... K92iptables S12syslog ... ... ... ... K95firstboot S17keytable ... ... [root@bigboy tmp]#
如上,你会发现,这些目录中的每个文件名要么是以“S”开头,以表明这些脚本是在系统启动的时候运行,或以“K”开头,表示这类脚本在系统正在关闭的时候被调用。如果一个脚本不在此目录,那么此脚本不会被运行。
大多数linux软件包将它们的启动脚本放在/etc/init.d
目录下,并且在/etc/rc.d
目录中创建相应的子目录以保存启动脚本的符号连接。如此使得文件管理更为容易。比如说,你只需删除文件的符号连接,而无需删除真实文件,这样以後你还可以使用它。
跟在K或者S後的数字按升序排列也就是这些脚本运行的次序。如,kudzu
(後05)将比wlan
(後09)先运行。
幸运的是,你不必需要一个总的脚本,或者符号连接来保证所有脚本运行正常。因为Fedora中有一个名为chkconfig
的组件,Debian/Ubuntu使用update-rc.d
命令来帮你做所有的事情。我们等会在详细讲它。
设置默认的启动级别
系统默认启动级别在文件/etc/inittab,变量initdefault中设置。当被设置为3时,系统以文本模式启动;
当被设置为5时,系统将会以图形界面模式启动。这里是此文件的一部分(删除你不需要的initdefault行):
# 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:3:initdefault: # Console Text Mode id:5:initdefault: # Console GUI Mode
注意:
- 大多数用户以如windows的图形界面启动(运行级别 5)
- 大多数techies倾向于基于文本的命令行模式启动(运行级别 3)
- 将默认启动值从3改为5,或者反过来,对下次重启有影响。查看下列章节:如何设置始终图形界面登录直到下次重启。
- 当然,不要设置默认启动值为6,否则你会不断重启;设置默认启动值0会导致永远开不了机。
获取一个GUI控制界面
手工方法: 每次需要运行X终端GUI应用程序时,通过在VGA控制台模式下运行startx命令启动X终端。记住登出後你将再次返回到基于文本的控制台.
[root@bigboy tmp]# startx
自动方法:可以让Linux在每次登录时自动载入X终端GUI界面,除非用init命令重新启动系统。 需要编辑/etc/inittab文件的初始默认变量,在先前的文章中提到这些变量将使系统重新启动後仍保持这些功能。
[root@bigboy tmp]# init 5
当你的机器中CPU容量或内存变量低的时候或者你需要最大化利用你的资源,大部分时候你需要在runlevel3文本模式下运行系统,必要时通过startx命令启动GUI。
机器可以作为个人工作站或服务器来使用,这对相关非技术员工而言,可能需要花费更多的时间来操作机器。机器通过init 5命令一直运行在运行级别 5。记住,修改/etc/initab文件使得即使重新启动系统也能够一直运行在运行级别 5。
获取一个在GUI环境下的文本终端
运行在Linux GUI环境中,有很多方法可以开启终端程序。当想要快速使用命令活对GUI菜单选项不是很熟悉,这些方法很重要。
使用一个GUI终端窗口
通过下面方法,开启一个基于GUI的终端窗口:
- 点击屏幕左边底部的Fedora标志按钮。
- 点击系统工具(System Tools),然後点击终端(Terminal)。
使用虚拟控制台终端
默认情况下,在VGA控制台模式下,Linux运行6个虚拟控制台或TTY sessions。这是通过在/etc/inittab文件中定义mingetty语句来实现的。X终端GUI控制台通过第一变量TTY(不受mingetty控制)创建自己的虚拟终端。 这将使GUI运行在第7个虚拟控制台上:
- 可以通过按下Ctl-Alt-F1到F5键来依次查看各个虚拟控制台,每个虚拟控制台都是一个新login提示符 。
- 只有在run level 5时在载入startx命令後通过Ctrl-Alt-F7键得到GUI登录界面。
系统关闭与重启
当你使用完毕後,直接关闭电源不是一个好方法。这有可能会的更新的文件被破坏,甚至于破坏文件系统目录结构。Linux有许多安全的关机与重启系统的方法,在这一节中将概述这些方法。
终止或关闭系统
init命令允许修改当前的runlevel,对于关闭系统,runlevel值是0,下面是例子:
[root@bigboy tmp]# init 0
Fedora也使用shutdown命令来完成相同的功能。常需要确认是否确定执行shutdown命令,可以通过使用-y开关来取消shutdown命令的执行。-h开关表示立即强迫系统停止运行,第一个参数确认开始程序运行后等待多长时间执行命令,此处例子为0分钟。也可以指定一天中的某个时间关闭系统,细节请查看man页。另一个有用的功能是shutdown命令将通知使用者系统即将关闭。
[root@bigboy tmp]# shutdown -hy 0 Broadcast message from root (pts/0) (Sat Nov 6 13:15:27 2004): The system is going down for system halt NOW! [root@bigboy tmp]#
重新启动系统
可以通过init命令立刻修改runlevel为6重新启动系统 。
[root@bigboy tmp]# init 6
reboot命令有同样的功能,并且对所有使用者发送一条警告消息。
[root@bigboy tmp]# reboot Broadcast message from root (pts/0) (Sat Nov 6 12:39:31 2004): The system is going down for reboot NOW! [root@bigboy tmp]#
更好的重启系统方法是使用shutdown的-r开关,并可指定一个重启延时时间,本例为10分钟。
[root@bigboy root]# shutdown -ry 10 Broadcast message from root (pts/0) (Sat Nov 6 13:26:39 2004): The system is going DOWN for reboot in 10 minutes! Broadcast message from root (pts/0) (Sat Nov 6 13:27:39 2004): The system is going DOWN for reboot in 9 minutes! ... ... ... Broadcast message from root (pts/0) (Sat Nov 6 13:36:39 2004): The system is going down for reboot NOW!
进入单用户模式
一些任务需要强迫你退出所用用户,结束第三方应用程序和断开网络连接,这样可以通过VGA控制台以管理员身份进入系统。 典型的任务是添加一个硬盘扩充硬盘容量或添加后出现启动过程失败(or the troubleshooting of a failed boot process. ),添加硬盘在第27章"Expanding Disk Capacity"中有说明。 另一个使用原因是恢复你的root密码。
选择单用户模式
当系统正常运行,可以使用init命令进入runlevel 1 。在控制台下这种方法并不好,当在一个远程终端运行这个命令时,用户将登出当前系统。
[root@bigboy root]# init 1 ... ... bash-2.05b#
不幸地是,这个对用户并没有瞻前性的警告作用,shutdown命令对单用户模式没有作用。可以通过运行带延迟1分钟作为唯一参数的shutdown命令来克服上面的困难。
[root@bigboy tmp]# shutdown 1 Broadcast message from root (pts/0) (Sat Nov 6 13:44:59 2004): The system is going DOWN to maintenance mode in 1 minute! Broadcast message from root (pts/0) (Sat Nov 6 13:45:59 2004): The system is going down to maintenance mode NOW! ... ... bash-2.05b#
在Grub屏幕下选择单用户模式
可以在启动系统后直接进入当单用户模式。按下面的列表一步步操作:
1. 开启系统,等待产生“Grub loading”消息,准备按下任意键或ESC键进入grub启动菜单,这些依赖Linux发行版。
Grub loading, please wait ... Press ESC to enter the menu
或者
Grub loading, please wait ... Press any key to enter the menu
2. 获取的grub主菜单是一份可用内核的列表。通过方向键滚动到你想要的内核版本,按下e键开始编辑。
Fedora Core (2.6.18-1.2239.fc5smp) Fedora Core (2.6.18-1.2200.fc5smp)
3. 内核启动菜单然后出现,使用方向键滚动到“kernel”行,按下e键开始编辑。
root (hd0,0) kernel /vmlinuz-2.6.18-1.2239.fc5smp ro root=LABEL=/ initrd /initrd-2.6.18-1.2239.fc5smp.img
4. 一个grub edit器出现,通过方向键移动到行最后面,然后在行后添加“single”,中间用空格隔开。 把
grub edit> kernel /vmlinuz-2.6.18-1.2239.fc5smp ro root=LABEL=/
修改为
grub edit> kernel /vmlinuz-2.6.18-1.2239.fc5smp ro root=LABEL=/ single
5. 按下enter回车键保存修改,按下b键开始“boot”启动。
6. 系统继续启动,但将直接进入root#提示符下,没有用户名和密码的询问。
从单用户模式转入默认运行级别
exit命令强制系统退出runlevel 1,转入系统默认的runlevel。也可以通过使用init命令(如init 3和init 5)选择默认行为:
bash-2.05b# exit INIT: Entering runlevel: 3 ... ... ... Fedora Core release 2 (Tettnang) Kernel 2.6.8-1.521 on an i686 bigboy login:
Root密码恢复
有时你可能会忘记root密码,或者先前的系统管理员调到一个新工作但密码并没有给你。通过下面的步骤恢复密码:
- 到VGA控制台,按下 Ctrl-Alt-Del键.系统将会正常关闭。
- 重新启动系统,进入单用户模式。
- 在命令提示符下,修改密码。单用户模式下鉴定控制台下的用户为系统管理员的root,因此不用指定root用户名。
- 使用exit命令返回到默认runlevel。
启动和停止守护进程
对于守护进程一个比较简单的定义是守护进程是一种即使没有人登录进入你的系统也会在无人值守的情况下运行的程序。比较常见的守护进程有syslog
守护进程用于收集系统出错信息并将其写入日志文件; 还有 apache
或 httpd
守护进程用于为互联网浏览器提供所需浏览的网页以及sendmail
守护进程用于将服务器所收到的电子邮件推送至你的收件箱。
The startup scripts I have been mentioning in the /etc/init.d
directory govern the activation of daemons that were installed with some of your Linux packages. The commands to start and stop them are universal. The startup scripts I have been mentioning in the /etc/init.d
directory govern the activation of daemons that were installed with some of your Linux packages. The commands to start and stop them are universal.
启动守护进程
如果在 /etc/init.d
目录中存在一个启动脚本,那么它的守护进程将通过指定它的文件名并在后面加上关键词“start”的方式启动,如以下显示:
root@u-bigboy:~# /etc/init.d/apache start * Starting apache 1.3 web server... ...done. root@u-bigboy:~#
停止守护进程
守护进程可以通过指定它的文件名并在后面加上关键词“stop”的方式停止;
root@u-bigboy:~# /etc/init.d/apache stop * Stopping apache 1.3 web server... ...done. root@u-bigboy:~#
重启守护进程
通常情况守护进程只在启动时读取相应的配置文件,因此,如果你改写它的配置文件,为了使其生效,你将不得不重启守护进程。带有关键词“restart”的命令将帮你实现重启操作:
root@u-bigboy:~# /etc/init.d/apache restart * Restarting apache 1.3 web server... ...done. root@u-bigboy:~#
别为配置你的守护进程担心。稍后我们将介绍一些常用的守护程序并通过一些简单的例子来具体了解它们。
关于服务命令
一些操作系统例如:Fedora 和 Rathat 也同样具有快捷的服务命令,允许你使用 "start", "stop" 和"restart" 这些关键词来控制守护进程, 但是使用更少的字符。 这里是一些实现该功能的快捷的,直观的例子:
[root@bigboy ~]# service httpd start [root@bigboy ~]# service httpd stop [root@bigboy ~]# service httpd restart
服务命令同样也具有“status”这个关键字,用来提供一个关于守护进程目前工作的简洁的报告。
[root@bigboy ~]# service httpd status httpd (pid 6135 6133 6132 6131 6130 6129 6128 6127 1561) is running... [root@bigboy ~]#
在每个运行级别中使用 chkconfig 启动守护进程
正如先前所说, chkconfig命令可以用来调节程序在哪个运行级别启动.你可以使用这个命令带 --list 参数来选择包含在/etc/init.d下的包列表和它的运行级别的开关:
[root@bigboy tmp]# chkconfig --list keytable 0:off 1:on 2:on 3:on 4:on 5:on 6:off atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off syslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off gpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off kudzu 0:off 1:off 2:off 3:on 4:on 5:on 6:off wlan 0:off 1:off 2:on 3:on 4:on 5:on 6:off sendmail 0:off 1:off 2:off 3:on 4:off 5:on 6:off netfs 0:off 1:off 2:off 3:on 4:on 5:on 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off random 0:off 1:off 2:on 3:on 4:on 5:on 6:off ... ...
chkconfig举例
你可以使用chkconfig来为一个包改变运行级别.这里你会看到sendmail在运行级别3或5中常规启动.让我们把sendmail改成不在系统启动时运行.
用chkconfig来得到sendmail的当前运行操作列表
chkconfig命令可以和grep结合使用,来确定sendmail在哪个运行级别可以运行.我们看到这里它可以行动在级别3和5.
[root@bigboy tmp]# chkconfig --list | grep mail sendmail 0:off 1:off 2:off 3:on 4:off 5:on 6:off [root@bigboy tmp]#
Use Chkconfig to Get a Listing of sendmail's Current Startup Options
The chkconfig command can be used with grep to determine the run levels in which sendmail will run. Here we see it will run at levels 3 and 5.
[root@bigboy tmp]# chkconfig --level 35 sendmail off [root@bigboy tmp]#
使sendmail不在运行级别3和5启动
chkconfig带--level参数可以指示在该运行级别上的相关操作.命令中的第一个参数是你相设置的包的名称第二个参数则让你选择是on或off.在这里我们使sendmail不要在运行级别3和5中启动:
[root@bigboy tmp]# chkconfig --level 35 sendmail off [root@bigboy tmp]#
如果没有指定--level后的运行级别,chkconfig会自动改变运行级别3,5.
[root@bigboy tmp]# chkconfig sendmail off
因为是要永久的关闭sendmail,我们当然要在运行前关闭它.
[root@bigboy tmp]# service sendmail stop Shutting down sendmail: [ OK ] Shutting down sm-client: [ OK ] [root@bigboy tmp]#
Double-check that sendmail Will Not Start Up
We can then use chkconfig to double-check our work.
[root@bigboy tmp]# chkconfig --list | grep mail sendmail 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@bigboy tmp]#
Turn On sendmail Again
To reactivate sendmail, we can use chkconfig once more, but with the on argument. Start sendmail again to get it running immediately, not just after the next reboot.
[root@bigboy tmp]# chkconfig sendmail on [root@bigboy tmp]# chkconfig --list | grep mail sendmail 0:off 1:off 2:off 3:on 4:off 5:on 6:off [root@bigboy tmp]# service sendmail start Starting sendmail: [ OK ] Starting sm-client: [ OK ] [root@bigboy tmp]#
Using chkconfig to Improve Security
A default Fedora installation automatically starts a number of daemons that you may not necessarily need for a Web server. This usually results in your system listening on a variety of unexpected TCP/IP ports that could be used as doors into your system by hackers.
The screen output of the netstat -an command below shows a typical case. Some ports are relatively easy to recognize. TCP ports 25 and 22 are for mail and SSH, respectively, but some others are less obvious. Should you use the chkconfig command and the scripts in the /etc/init.d directory to shut these down permanently?
[root@bigboy tmp]# netstat -an Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:32768 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:32769 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 0 :::22 :::* LISTEN udp 0 0 0.0.0.0:32768 0.0.0.0:* udp 0 0 0.0.0.0:930 0.0.0.0:* udp 0 0 0.0.0.0:68 0.0.0.0:* udp 0 0 0.0.0.0:111 0.0.0.0:* udp 0 0 0.0.0.0:631 0.0.0.0:* ... ... [root@bigboy tmp]#
For example, how do you know which startup script is responsible for TCP port 111? The answer is to use the lsof command which lists all open, or actively used, files and can be given additional options to extend its scope to include the TCP/IP protocol stack.
In the next examples we see that TCP ports 111 and 32769, and UDP port 123 are being used by the portmap, xinetd and ntp daemons respectively. The portmap daemon is required for the operation of NFS and NIS, topics that are covered in Chapters 29, "Remote Disk Access with NFS", and 30, "Configuring NIS". portmap also has many known security flaws that makes it advisable to be run on a secured network. If you don't need any of these three applications, it's best to shut down portmap permanently. NTP, which is covered in Chapter 24, "The NTP Server", is required for synchronizing your time with a reliable time source, and may be necessary. A number of network applications are reliant on xinetd, as explained in Chapter 16, "Telnet, TFTP, and xinetd", and it might be required for their operation:
[root@ bigboy tmp]# lsof -i tcp:111 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME portmap 1165 rpc 4u IPv4 2979 TCP *:sunrpc (LISTEN) [root@ bigboy tmp # [root@bigboy tmp]# lsof -i tcp:32769 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME xinetd 1522 root 5u IPv4 2764 TCP probe-001:32769 (LISTEN) [root@bigboy tmp]# [root@bigboy root]# lsof -i udp:123 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME ntpd 1321 ntp 4u IPv4 3390 UDP *:ntp ... ... [root@bigboy root]#
In some cases it's tricky to determine the application based on the results of the lsof command. In the example below, we've discovered that TCP port 32768 is being used by rpc.statd, but there is no rpc.statd file in the /etc/init.d directory. The simple solution is to use the grep command to search all the files for the string rpc.statd to determine which one is responsible for its operation. We soon discover that the nfslock daemon uses it. If you don't need nfslock, then shut it down permanently.
[root@bigboy tmp]# lsof -i tcp:32768 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME rpc.statd 1178 rpcuser 6u IPv4 2400 TCP *:32768 (LISTEN) [root@bigboy tmp]# ls /etc/init.d/rpc.statd ls: /etc/init.d/rpc.statd: No such file or directory [root@bigboy tmp]# grep -i statd /etc/init.d/* /etc/init.d/nfslock:[ -x /sbin/rpc.statd ] || exit 0 ... ... [root@bigboy tmp]#
As a rule of thumb, applications listening only on the loopback interface (IP address 127.0.0.1) are usually the least susceptible to network attack and probably don't need to be stopped for network security reasons. Those listening on all interfaces, depicted as IP address 0.0.0.0, are naturally more vulnerable and their continued operation should be dependent on your server's needs. I usually shutdown nfs, nfslock, netfs, portmap, and cups printing as standard practice on Internet servers. I keep sendmail running as it is always needed to send and receive mail (see Chapter 21, "Configuring Linux Mail Servers", for details). Your needs may be different.
Remember to thoroughly research your options thoroughly before choosing to shut down an application. Use the Linux man pages, reference books and the Internet for information. Unpredictable results are always undesirable.
Shutting down applications is only a part of server security. Firewalls, physical access restrictions, password policies, and patch updates need to be considered. Full coverage of server and network security is beyond the scope of this book, but you should always have a security reference guide on hand to guide your final decisions.
Final Tips on chkconfig
Remember the following:
- In most cases, you want to modify runlevels 3 and 5 simultaneously and with the same values.
- Don't add/remove anything to other runlevels unless you absolutely know what you are doing. Don't experiment, unless in a test environment.
- chkconfig doesn't start the programs in the /etc/init.d directory, it just configures them to be started or ignored when the system boots up. The commands for starting and stopping the programs covered in this book are covered in each respective chapter.
Using sysv-rc-conf to Start Daemons at Each runlevel
With Debian / Ubuntu Linux, the update-rc.d
command replaces chkconfig
as the default package for modifying /etc/init.d
script links. Unfortunately, the utility was written to facilitate link modification when packages are installed or removed, but is less friendly when you need to alter links for existing packages you want to keep. Fortunately there is hope for the harried systems administrator in the form of the sysv-rc-conf
package which uses an almost identical syntax to chkconfig
, and has a GUI mode if you run it from the command line without any arguments. This section will show you some important tips in using sysv-rc-conf
.
Installing sysv-rc-conf
The sysv-rc-conf
package can be installed easily using apt-get
. Here is an example.
root@u-bigboy:~# apt-get install sysv-rc-conf
Listing the runlevels for Daemons
This can be done with the --list
option. In this example below we get a listing for only the apache daemon.
root@u-bigboy:~# sysv-rc-conf --list apache
apache 0:off 1:off 2:on 3:on 4:on 5:on 6:off
root@u-bigboy:~#
Here we get a listing for everything.
root@u-bigboy:~# sysv-rc-conf --list
acpi-support 0:off 1:off 2:on 3:on 4:on 5:on 6:off
acpid 0:off 1:off 2:on 3:on 4:on 5:on 6:off
alsa-utils 0:off 6:off … … …
vbesave 2:on 3:on 4:on 5:on
x11-common S:on
root@u-bigboy:~#
Setting the runlevels for Daemons
The sysv-rc-conf
program has further similarities to the chkconfig
syntax. Here we set the apache
daemon to start automatically at levels 2 through 5. root@u-bigboy:~# sysv-rc-conf apache on Similarly, we can set the apache
daemon not to start at levels 2 through 5 with this command:
root@u-bigboy:~# sysv-rc-conf apache off
Finally we can use set the apache
daemon to start only at levels 3 and 5.
root@u-bigboy:~# sysv-rc-conf --level 35 apache on
Conclusion
The topics discussed in this chapter might seem simple, but like syslog, which was covered in Chapter 5, "Troubleshooting Linux with syslog", they are an essential part of Linux administration that gets frequently overlooked especially when new software is installed. Whenever possible, always try to reboot your system to make sure all the newly installed applications start up correctly. Sometimes they start but give errors listed only in the /var/log directory. Taking the time to configure and test your startup scripts could prevent you from being awakened in the middle of the night while you are on vacation! It is really important.