第十二章 Linux启动和内核管理
内容概述
- CentOS 6 之前版本的启动流程
- 服务管理
- Grub管理
- 启动排错
- 内核管理
- CentOS 7 以后版本启动流程
- Unit介绍
- 服务管理和查看
- 启动排错
- 破解root口令
- 修复Grub2
1.CentOS 6 的启动管理
1.1 Linux 组成
-
kernel 实现进程管理、内存管理、网络管理、驱动程序、文件系统、安全功能等功能
-
rootfs 包括程序和 glibc 库
程序:二进制执行文件库:函数集合, function, 调用接口(头文件负责描述)
1.2 内核设计流派
-
宏内核(monolithic kernel):又称单内核和强内核,Unix,Linux
把所有系统服务都放到内核里,所有功能集成于同一个程序,分层实现不同功能,系统庞大复杂,Linux其实在单内核内核实现了模块化,也就相当于吸收了微内核的优点
-
微内核(micro kernel):Windows,Solaris,HarmonyOS
简化内核功能,在内核之外的用户态尽可能多地实现系统服务,同时加入相互之间的安全保护,每种功能使用一个单独子系统实现,将内核功能移到用户空间,性能差
1.3 CentOS 6 启动流程
1.3.1 CentOS 6 启动流程
- 加载BIOS的硬件信息,获取第一个启动设备
- 读取第一个启动设备MBR的引导加载程序(grub)的启动信息
- 加载核心操作系统的核心信息,核心开始解压缩,并尝试驱动所有的硬件设备
- 核心执行init程序,并获取默认的运行信息
- init程序执行/etc/rc.d/rc.sysinit文件,重新挂载根文件系统
- 启动核心的外挂模块
- init执行运行的各个批处理文件(scripts)
- init执行/etc/rc.d/rc.local
- 执行/bin/login程序,等待用户登录
- 登录之后开始以Shell控制主机
1.3.2 硬件启动POST
POST:Power-On-Self-Test,加电自检,是BIOS功能的一个主要部分。负责完成对CPU、主板、内存、硬盘子系统、显示子系统、串并行接口、键盘等硬件情况的检测
主板的ROM:BIOS,Basic Input and Output System,保存着有关计算机系统最重要的基本输入输出程序,系统信息设置、开机加电自检程序和系统启动自举程序等
主板的RAM:CMOS互补金属氧化物半导体,保存各项参数的设定,按次序查找引导设备,第一个有引导程序的设备为本次启动设备
1.3.3 启动加载器 bootloader
1.3.3.1 grub 功能和组成
bootloader: 引导加载器,引导程序
- windows: ntloader,仅是启动OS
- Linux:功能丰富,提供菜单,允许用户选择要启动系统或不同的内核版本;把用户选定的内核装载到内存中的特定空间中,解压、展开,并把系统控制权移交给内核Linux的bootloader
- LILO:LInux LOader,早期的bootloader,功能单一
- GRUB: GRand Unified Bootloader, CentOS 6 GRUB 0.97: GRUB Legacy, CentOS 7 以后使用GRUB 2.02
GRUB 启动阶段
-
primary boot loader :
1st stage:MBR的前446个字节
1.5 stage:mbr 之后的扇区,让stage1中的bootloader能识别stage2所在的分区上的文件系统
-
secondary boot loader :2nd stage,分区文件/boot/grub/
范例:
[root@centos6 ~]# hexdump -C -n 512 /dev/sda
00000000 eb 48 90 00 00 00 00 00 00 00 00 00 00 00 00 00 |.H..............|
00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 02 |................|
00000040 ff 00 00 20 01 00 00 00 00 02 fa 90 90 f6 c2 80 |... ............|
00000050 75 02 b2 80 ea 59 7c 00 00 31 c0 8e d8 8e d0 bc |u....Y|..1......|
00000060 00 20 fb a0 40 7c 3c ff 74 02 88 c2 52 f6 c2 80 |. ..@|<.t...R...|
00000070 74 54 b4 41 bb aa 55 cd 13 5a 52 72 49 81 fb 55 |tT.A..U..ZRrI..U|
00000080 aa 75 43 a0 41 7c 84 c0 75 05 83 e1 01 74 37 66 |.uC.A|..u....t7f|
00000090 8b 4c 10 be 05 7c c6 44 ff 01 66 8b 1e 44 7c c7 |.L...|.D..f..D|.|
000000a0 04 10 00 c7 44 02 01 00 66 89 5c 08 c7 44 06 00 |....D...f.\..D..|
000000b0 70 66 31 c0 89 44 04 66 89 44 0c b4 42 cd 13 72 |pf1..D.f.D..B..r|
000000c0 05 bb 00 70 eb 7d b4 08 cd 13 73 0a f6 c2 80 0f |...p.}....s.....|
000000d0 84 f0 00 e9 8d 00 be 05 7c c6 44 ff 00 66 31 c0 |........|.D..f1.|
000000e0 88 f0 40 66 89 44 04 31 d2 88 ca c1 e2 02 88 e8 |..@f.D.1........|
000000f0 88 f4 40 89 44 08 31 c0 88 d0 c0 e8 02 66 89 04 |..@.D.1......f..|
00000100 66 a1 44 7c 66 31 d2 66 f7 34 88 54 0a 66 31 d2 |f.D|f1.f.4.T.f1.|
00000110 66 f7 74 04 88 54 0b 89 44 0c 3b 44 08 7d 3c 8a |f.t..T..D.;D.}<.|
00000120 54 0d c0 e2 06 8a 4c 0a fe c1 08 d1 8a 6c 0c 5a |T.....L......l.Z|
00000130 8a 74 0b bb 00 70 8e c3 31 db b8 01 02 cd 13 72 |.t...p..1......r|
00000140 2a 8c c3 8e 06 48 7c 60 1e b9 00 01 8e db 31 f6 |*....H|`......1.|
00000150 31 ff fc f3 a5 1f 61 ff 26 42 7c be 7f 7d e8 40 |1.....a.&B|..}.@|
00000160 00 eb 0e be 84 7d e8 38 00 eb 06 be 8e 7d e8 30 |.....}.8.....}.0|
00000170 00 be 93 7d e8 2a 00 eb fe 47 52 55 42 20 00 47 |...}.*...GRUB .G|
00000180 65 6f 6d 00 48 61 72 64 20 44 69 73 6b 00 52 65 |eom.Hard Disk.Re|
00000190 61 64 00 20 45 72 72 6f 72 00 bb 01 00 b4 0e cd |ad. Error.......|
000001a0 10 ac 3c 00 75 f4 c3 00 00 00 00 00 00 00 00 00 |..<.u...........|
000001b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 20 |............... |
000001c0 21 00 83 aa 28 82 00 08 00 00 00 00 20 00 00 aa |!...(....... ...|
000001d0 29 82 83 fe ff ff 00 08 20 00 00 80 3c 0c 00 fe |)....... ...<...|
000001e0 ff ff 83 fe ff ff 00 88 5c 0c 00 00 40 06 00 fe |........\...@...|
000001f0 ff ff 05 fe ff ff 00 88 9c 12 00 78 63 06 55 aa |...........xc.U.|
00000200
[root@centos6 ~]# hexdump -C -n 512 -v /dev/sda
00000000 eb 48 90 00 00 00 00 00 00 00 00 00 00 00 00 00 |.H..............|
00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 02 |................|
00000040 ff 00 00 20 01 00 00 00 00 02 fa 90 90 f6 c2 80 |... ............|
00000050 75 02 b2 80 ea 59 7c 00 00 31 c0 8e d8 8e d0 bc |u....Y|..1......|
00000060 00 20 fb a0 40 7c 3c ff 74 02 88 c2 52 f6 c2 80 |. ..@|<.t...R...|
00000070 74 54 b4 41 bb aa 55 cd 13 5a 52 72 49 81 fb 55 |tT.A..U..ZRrI..U|
00000080 aa 75 43 a0 41 7c 84 c0 75 05 83 e1 01 74 37 66 |.uC.A|..u....t7f|
00000090 8b 4c 10 be 05 7c c6 44 ff 01 66 8b 1e 44 7c c7 |.L...|.D..f..D|.|
000000a0 04 10 00 c7 44 02 01 00 66 89 5c 08 c7 44 06 00 |....D...f.\..D..|
000000b0 70 66 31 c0 89 44 04 66 89 44 0c b4 42 cd 13 72 |pf1..D.f.D..B..r|
000000c0 05 bb 00 70 eb 7d b4 08 cd 13 73 0a f6 c2 80 0f |...p.}....s.....|
000000d0 84 f0 00 e9 8d 00 be 05 7c c6 44 ff 00 66 31 c0 |........|.D..f1.|
000000e0 88 f0 40 66 89 44 04 31 d2 88 ca c1 e2 02 88 e8 |..@f.D.1........|
000000f0 88 f4 40 89 44 08 31 c0 88 d0 c0 e8 02 66 89 04 |..@.D.1......f..|
00000100 66 a1 44 7c 66 31 d2 66 f7 34 88 54 0a 66 31 d2 |f.D|f1.f.4.T.f1.|
00000110 66 f7 74 04 88 54 0b 89 44 0c 3b 44 08 7d 3c 8a |f.t..T..D.;D.}<.|
00000120 54 0d c0 e2 06 8a 4c 0a fe c1 08 d1 8a 6c 0c 5a |T.....L......l.Z|
00000130 8a 74 0b bb 00 70 8e c3 31 db b8 01 02 cd 13 72 |.t...p..1......r|
00000140 2a 8c c3 8e 06 48 7c 60 1e b9 00 01 8e db 31 f6 |*....H|`......1.|
00000150 31 ff fc f3 a5 1f 61 ff 26 42 7c be 7f 7d e8 40 |1.....a.&B|..}.@|
00000160 00 eb 0e be 84 7d e8 38 00 eb 06 be 8e 7d e8 30 |.....}.8.....}.0|
00000170 00 be 93 7d e8 2a 00 eb fe 47 52 55 42 20 00 47 |...}.*...GRUB .G|
00000180 65 6f 6d 00 48 61 72 64 20 44 69 73 6b 00 52 65 |eom.Hard Disk.Re|
00000190 61 64 00 20 45 72 72 6f 72 00 bb 01 00 b4 0e cd |ad. Error.......|
000001a0 10 ac 3c 00 75 f4 c3 00 00 00 00 00 00 00 00 00 |..<.u...........|
000001b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 20 |............... |
000001c0 21 00 83 aa 28 82 00 08 00 00 00 00 20 00 00 aa |!...(....... ...|
000001d0 29 82 83 fe ff ff 00 08 20 00 00 80 3c 0c 00 fe |)....... ...<...|
000001e0 ff ff 83 fe ff ff 00 88 5c 0c 00 00 40 06 00 fe |........\...@...|
000001f0 ff ff 05 fe ff ff 00 88 9c 12 00 78 63 06 55 aa |...........xc.U.|
00000200
[root@centos6 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 97G 908M 91G 1% /
tmpfs tmpfs 490M 0 490M 0% /dev/shm
/dev/sda1 ext4 976M 31M 895M 4% /boot
/dev/sda3 ext4 50G 74M 47G 1% /data
[root@centos6 ~]# modinfo ext4
filename: /lib/modules/2.6.32-754.el6.x86_64/kernel/fs/ext4/ext4.ko
license: GPL
description: Fourth Extended Filesystem
author: Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others
retpoline: Y
srcversion: 3C0D8A8996992A22750E504
depends: mbcache,jbd2
vermagic: 2.6.32-754.el6.x86_64 SMP mod_unload modversions
[root@centos6 ~]# ll -h /lib/modules/2.6.32-754.el6.x86_64/kernel/fs/ext4/ext4.ko
-rwxr--r--. 1 root root 636K Jun 20 2018 /lib/modules/2.6.32-754.el6.x86_64/kernel/fs/ext4/ext4.ko
[root@centos6 ~]# fdisk -l
Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0008a04f
Device Boot Start End Blocks Id System
/dev/sda1 * 1 131 1048576 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 131 12910 102645760 83 Linux
/dev/sda3 12910 19437 52428800 83 Linux
/dev/sda4 19437 26109 53591040 5 Extended
/dev/sda5 19437 19698 2097152 82 Linux swap / Solaris
[root@centos7 ~]# fdisk -l
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b65a1
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 211814399 104857600 83 Linux
/dev/sda3 211814400 316671999 52428800 83 Linux
/dev/sda4 316672000 419430399 51379200 5 Extended
/dev/sda5 316674048 320868351 2097152 82 Linux swap / Solaris
[root@centos6 ~]# ls /boot
config-2.6.32-754.el6.x86_64 grub lost+found System.map-2.6.32-754.el6.x86_64
efi initramfs-2.6.32-754.el6.x86_64.img symvers-2.6.32-754.el6.x86_64.gz vmlinuz-2.6.32-754.el6.x86_64
[root@centos6 ~]# ls /boot/grub/
device.map fat_stage1_5 grub.conf jfs_stage1_5 minix_stage1_5 splash.xpm.gz stage2 vstafs_stage1_5
e2fs_stage1_5 ffs_stage1_5 iso9660_stage1_5 menu.lst reiserfs_stage1_5 stage1 ufs2_stage1_5 xfs_stage1_5
[root@centos6 ~]# cat /boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/sda2
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz #加载图片
hiddenmenu
title CentOS 6 (2.6.32-754.el6.x86_64) #菜单
root (hd0,0)
kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=UUID=f26b3084-878f-4218-a7df-0eedd8cb79af rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-754.el6.x86_64.img
#/vmlinuz-2.6.32-754.el6.x86_64 内核 root=UUID=55ed4e69-a262-485e-a2c8-9df9ac044aec / 根分区UUID initrd /initramfs-2.6.32-754.el6.x86_64.img 驱动文件
[root@centos6 ~]# blkid
/dev/sda1: UUID="07ff813e-74af-4047-88ad-c402b397fd1c" TYPE="ext4"
/dev/sda2: UUID="f26b3084-878f-4218-a7df-0eedd8cb79af" TYPE="ext4"
/dev/sda3: UUID="c8cea348-2536-46ae-9b3b-fe0543fbdc13" TYPE="ext4"
/dev/sda5: UUID="66c77c3c-e56c-4df2-90fd-08150563b747" TYPE="swap"
[root@centos6 ~]# ls /boot
config-2.6.32-754.el6.x86_64 grub lost+found System.map-2.6.32-754.el6.x86_64
efi initramfs-2.6.32-754.el6.x86_64.img symvers-2.6.32-754.el6.x86_64.gz vmlinuz-2.6.32-754.el6.x86_64
# initramfs-2.6.32-754.el6.x86_64.img 驱动文件 vmlinuz-2.6.32-754.el6.x86_64 内核文件
[root@centos6 ~]# file /boot/initramfs-2.6.32-754.el6.x86_64.img
/boot/initramfs-2.6.32-754.el6.x86_64.img: gzip compressed data, from Unix, last modified: Wed Oct 6 23:46:51 2021, max compression
[root@centos6 ~]# cp /boot/initramfs-2.6.32-754.el6.x86_64.img /data
cp: overwrite `/data/initramfs-2.6.32-754.el6.x86_64.img'? y
[root@centos6 ~]# cd /data
[root@centos6 data]# ls
initramfs-2.6.32-754.el6.x86_64.img lost+found
[root@centos6 data]# mv initramfs-2.6.32-754.el6.x86_64.img initramfs-2.6.32-754.el6.x86_64.img.gz
[root@centos6 data]# gzip -d initramfs-2.6.32-754.el6.x86_64.img.gz
[root@centos6 data]# ls
initramfs-2.6.32-754.el6.x86_64.img lost+found
[root@centos6 data]# ll -h initramfs-2.6.32-754.el6.x86_64.img
-rw------- 1 root root 61M Nov 5 20:13 initramfs-2.6.32-754.el6.x86_64.img
[root@centos6 data]# ll /boot/initramfs-2.6.32-754.el6.x86_64.img -h
-rw-------. 1 root root 22M Nov 4 22:57 /boot/initramfs-2.6.32-754.el6.x86_64.img
[root@centos6 data]# file initramfs-2.6.32-754.el6.x86_64.img
initramfs-2.6.32-754.el6.x86_64.img: ASCII cpio archive (SVR4 with no CRC)
[root@centos6 data]# cpio -id < initramfs-2.6.32-754.el6.x86_64.img
123797 blocks
[root@centos6 data]# ls
bin dracut-004-411.el6 init initqueue-settled lib mount pre-pivot proc sysroot var
cmdline emergency initqueue initqueue-timeout lib64 netroot pre-trigger sbin tmp
dev etc initqueue-finished initramfs-2.6.32-754.el6.x86_64.img lost+found pre-mount pre-udev sys usr
[root@centos6 data]# find -name ext4.ko
./lib/modules/2.6.32-754.el6.x86_64/kernel/fs/ext4/ext4.ko
1.3.3.2 CentOS 6 grub 安装
安装 grub 两种方式
方法1: grub-install 安装grub stage1和stage1_5到/dev/DISK磁盘上,并复制GRUB相关文件到DIR/boot目录下
grub-install --root-directory=DIR /dev/DISK
方法2 : grub命令
#grub
grub> root (hd#,#)
grub> setup (hd#)
范例:修复grub的第1阶段故障
[root@centos6 ~]# hexdump -C -n 512 /dev/sda
00000000 eb 48 90 10 8e d0 bc 00 b0 b8 00 00 8e d8 8e c0 |.H..............|
00000010 fb be 00 7c bf 00 06 b9 00 02 f3 a4 ea 21 06 00 |...|.........!..|
00000020 00 be be 07 38 04 75 0b 83 c6 10 81 fe fe 07 75 |....8.u........u|
00000030 f3 eb 16 b4 02 b0 01 bb 00 7c b2 80 8a 74 03 02 |.........|...t..|
00000040 80 00 00 80 30 f2 04 00 00 08 fa 90 90 f6 c2 80 |....0...........|
00000050 75 02 b2 80 ea 59 7c 00 00 31 c0 8e d8 8e d0 bc |u....Y|..1......|
00000060 00 20 fb a0 40 7c 3c ff 74 02 88 c2 52 f6 c2 80 |. ..@|<.t...R...|
00000070 74 54 b4 41 bb aa 55 cd 13 5a 52 72 49 81 fb 55 |tT.A..U..ZRrI..U|
00000080 aa 75 43 a0 41 7c 84 c0 75 05 83 e1 01 74 37 66 |.uC.A|..u....t7f|
00000090 8b 4c 10 be 05 7c c6 44 ff 01 66 8b 1e 44 7c c7 |.L...|.D..f..D|.|
000000a0 04 10 00 c7 44 02 01 00 66 89 5c 08 c7 44 06 00 |....D...f.\..D..|
000000b0 70 66 31 c0 89 44 04 66 89 44 0c b4 42 cd 13 72 |pf1..D.f.D..B..r|
000000c0 05 bb 00 70 eb 7d b4 08 cd 13 73 0a f6 c2 80 0f |...p.}....s.....|
000000d0 84 f0 00 e9 8d 00 be 05 7c c6 44 ff 00 66 31 c0 |........|.D..f1.|
000000e0 88 f0 40 66 89 44 04 31 d2 88 ca c1 e2 02 88 e8 |..@f.D.1........|
000000f0 88 f4 40 89 44 08 31 c0 88 d0 c0 e8 02 66 89 04 |..@.D.1......f..|
00000100 66 a1 44 7c 66 31 d2 66 f7 34 88 54 0a 66 31 d2 |f.D|f1.f.4.T.f1.|
00000110 66 f7 74 04 88 54 0b 89 44 0c 3b 44 08 7d 3c 8a |f.t..T..D.;D.}<.|
00000120 54 0d c0 e2 06 8a 4c 0a fe c1 08 d1 8a 6c 0c 5a |T.....L......l.Z|
00000130 8a 74 0b bb 00 70 8e c3 31 db b8 01 02 cd 13 72 |.t...p..1......r|
00000140 2a 8c c3 8e 06 48 7c 60 1e b9 00 01 8e db 31 f6 |*....H|`......1.|
00000150 31 ff fc f3 a5 1f 61 ff 26 42 7c be 7f 7d e8 40 |1.....a.&B|..}.@|
00000160 00 eb 0e be 84 7d e8 38 00 eb 06 be 8e 7d e8 30 |.....}.8.....}.0|
00000170 00 be 93 7d e8 2a 00 eb fe 47 52 55 42 20 00 47 |...}.*...GRUB .G|
00000180 65 6f 6d 00 48 61 72 64 20 44 69 73 6b 00 52 65 |eom.Hard Disk.Re|
00000190 61 64 00 20 45 72 72 6f 72 00 bb 01 00 b4 0e cd |ad. Error.......|
000001a0 10 ac 3c 00 75 f4 c3 00 00 00 00 00 00 00 00 00 |..<.u...........|
000001b0 00 00 00 00 00 00 00 00 4f a0 08 00 00 00 80 20 |........O...... |
000001c0 21 00 83 aa 28 82 00 08 00 00 00 00 20 00 00 aa |!...(....... ...|
000001d0 29 82 83 fe ff ff 00 08 20 00 00 80 3c 0c 00 fe |)....... ...<...|
000001e0 ff ff 83 fe ff ff 00 88 5c 0c 00 00 40 06 00 fe |........\...@...|
000001f0 ff ff 05 fe ff ff 00 88 9c 12 00 78 63 06 55 aa |...........xc.U.|
00000200
#破坏grub第1阶段
[[root@centos6 ~]# dd if=/dev/zero of=/dev/sda bs=1 count=446
446+0 records in
446+0 records out
446 bytes (446 B) copied, 0.000782486 s, 570 kB/s
[root@centos6 ~]# hexdump -C -n 512 /dev/sda
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000001b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 20 |............... |
000001c0 21 00 83 aa 28 82 00 08 00 00 00 00 20 00 00 aa |!...(....... ...|
000001d0 29 82 83 fe ff ff 00 08 20 00 00 80 3c 0c 00 fe |)....... ...<...|
000001e0 ff ff 83 fe ff ff 00 88 5c 0c 00 00 40 06 00 fe |........\...@...|
000001f0 ff ff 05 fe ff ff 00 88 9c 12 00 78 63 06 55 aa |...........xc.U.|
00000200
[root@centos6 ~]# hexdump -C -n 512 -v /dev/sda
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000000a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000000b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000000c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000000d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000000e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000000f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000110 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000130 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000150 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000160 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000170 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000180 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000190 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000001a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000001b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 20 |............... |
000001c0 21 00 83 aa 28 82 00 08 00 00 00 00 20 00 00 aa |!...(....... ...|
000001d0 29 82 83 fe ff ff 00 08 20 00 00 80 3c 0c 00 fe |)....... ...<...|
000001e0 ff ff 83 fe ff ff 00 88 5c 0c 00 00 40 06 00 fe |........\...@...|
000001f0 ff ff 05 fe ff ff 00 88 9c 12 00 78 63 06 55 aa |...........xc.U.|
00000200
#前446字节被清空
[root@centos6 ~]# reboot
一阶段被破坏,系统认为硬盘不能启动,自动从光盘启动
语言
键盘布局
网络不配置
Continue
/ 根分区被挂载到/mnt/sysimage
光盘启动,进入rescue模式
#chroot /mnt/sysimage #切到根目录
#grub-install /dev/sda #修复前446字节
#sync #存盘
#exit #退出
#reboot #重启
系统可以正常启动
范例:
[root@centos6 ~]# grub
Probing devices to guess BIOS drives. This may take a long time.
GNU GRUB version 0.97 (640K lower / 3072K upper memory)
[ Minimal BASH-like line editing is supported. For the first word, TAB
lists possible command completions. Anywhere else TAB lists the possible
completions of a device/filename.]
grub> root (hd0,0)
root (hd0,0)
Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0)
setup (hd0)
Checking if "/boot/grub/stage1" exists... no
Checking if "/grub/stage1" exists... yes
Checking if "/grub/stage2" exists... yes
Checking if "/grub/e2fs_stage1_5" exists... yes
Running "embed /grub/e2fs_stage1_5 (hd0)"... 27 sectors are embedded.
succeeded
Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub> quit
quit
1.3.3.3 grub legacy 管理
配置文件:/boot/grub/grub.conf <-- /etc/grub.conf
stage2及内核等通常放置于一个基本磁盘分区
grub legacy 功用:
(1) 提供启动菜单、并提供交互式接口
a:内核参数
e:编辑模式,用于编辑菜单
c:命令模式,交互式接口
(2) 加载用户选择的内核或操作系统
允许传递参数给内核
可隐藏启动菜单
(3) 为菜单提供了保护机制
为编辑启动菜单进行认证
为启用内核或操作系统进行认证
grub的命令行接口
help: 获取帮助列表
help KEYWORD: 详细帮助信息
find (hd#,#)/PATH/TO/SOMEFILE:
root (hd#,#)
kernel /PATH/TO/KERNEL_FILE: 设定本次启动的内核文件;额外还可添加许多内核支持使用的
cmdline参数
例如:max_loop=100 selinux=0 init=/path/to/init
initrd /PATH/TO/INITRAMFS_FILE: 设定为选定的内核提供额外文件的ramdisk
boot: 引导启动选定的内核
cat /proc/cmdline 内核参数
内核参数文档:
/usr/share/doc/kernel-doc-2.6.32/Documentation/kernel-parameters.txt
grub legacy识别硬盘设备
(hd#,#)
hd#: 磁盘编号,用数字表示;从0开始编号
#: 分区编号,用数字表示; 从0开始编号
示例:
(hd0,0) 第一块硬盘,第一个分区
手动在grub命令行接口启动系统
grub> root (hd#,#)
grub> kernel /vmlinuz-VERSION-RELEASE ro root=/dev/DEVICE
grub> initrd /initramfs-VERSION-RELEASE.img
grub> boot
grub legacy配置文件:/boot/grub/grub.conf
default=#: 设定默认启动的菜单项;落单项(title)编号从0开始
timeout=#:指定菜单项等待选项选择的时长
splashimage=(hd#,#)/PATH/XPM_FILE:菜单背景图片文件路径
password [--md5| --encrypt] STRING: 启动菜单编辑认证
hiddenmenu:隐藏菜单
title TITLE:定义菜单项“标题”, 可出现多次
root (hd#,#):查找stage2及kernel文件所在设备分区;为grub的根
kernel /PATH/TO/VMLINUZ_FILE [PARAMETERS]:启动的内核
initrd /PATH/TO/INITRAMFS_FILE: 内核匹配的ramfs文件
password [--md5|--encrypted ] STRING: 启动选定的内核或操作系统时进行认证
grub加密生成grub口令
grub-md5-crypt
grub-crypt
破解root口令:
(1) 编辑grub菜单(选定要编辑的title,而后使用a 或 e 命令)
(2) 在选定的kernel后附加1, s, S,single 都可以进入单用户模式
(3) 在kernel所在行,键入“b”命令
范例:破解CentOS 6 root密码
按ESC,进入菜单选择,按a
在最后输入1,表示临时进入1模式,单用户模式
#不用密码直接进来了,passwd 直接修改密码
# passwd root
# init 3 #进入3模式
输入新密码 直接登录
范例: 给grub 添加密码,防止破解root密码
[root@centos6 ~]# grub-crypt
Password:
Retype password:
$6$dd3QxTQiLvVTxITw$HGyYnMmaFsMA98nYgs8bDaccnw716Bm8i/toQeDRJFcYQ5L7hYdD7OKGTx30fsX9GFr7AGsByFj0SzHbZIAJY/
#设置grub生成密码
[root@centos6 ~]# vim /boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/sda2
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
password --encrypt $6$dd3QxTQiLvVTxITw$HGyYnMmaFsMA98nYgs8bDaccnw716Bm8i/toQeDRJFcYQ5L7hYdD7OKGTx30fsX9GFr7AGsByFj0SzHbZIAJY/ #在这里设置grub密码
title CentOS 6 (2.6.32-754.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=UUID=f26b3084-878f-4218-a7df-0eedd8cb79af rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-754.el6.x86_64.img
[root@centos6 ~]# reboot
按ESC,进入菜单选择,按p
输入密码
只有输入密码才可以进到这里
忘记grub密码,直接去vim /boot/grub/grub.conf文件,把password --encrypt $6$dd3QxTQiLvVTxITw$HGyYnMmaFsMA98nYgs8bDaccnw716Bm8i/toQeDRJFcYQ5L7hYdD7OKGTx30fsX9GFr7AGsByFj0SzHbZIAJY/
这行删除就没有grub密码了
范例:生成背景图片
[root@centos6 ~]# convert -resize 640x480 -colors 14 winner.png splash.xpm
[root@centos6 ~]# more splash.xpm
#生成splash.xpm.gz
[root@centos6 ~]# gzip splash.xpm
[root@centos6 ~]# mv splash.xpm.gz /boot/grub
1.3. 加载 kernel
kernel 自身初始化过程
- 探测可识别到的所有硬件设备
- 加载硬件驱动程序(借助于ramdisk加载驱动)
- 以只读方式挂载根文件系统
- 运行用户空间的第一个应用程序:/sbin/init
Linux内核特点:
- 支持模块化:.ko(内核对象),如:文件系统,硬件驱动,网络协议等
- 支持内核模块的动态装载和卸载
内核组成部分:
-
核心文件:/boot/vmlinuz-VERSION-release
ramdisk:辅助的伪根系统,加载相应的硬件驱动,ramdisk --> ramfs 提高速度
CentOS 5 /boot/initrd-VERSION-release.img
CentOS 6 以后版本 /boot/initramfs-VERSION-release.img
-
模块文件:/lib/modules/VERSION-release
范例:误删除内核文件/boot/vmlinuz-2.6.32-754.el6.x86_64无法启动,故障恢复
[root@centos6 ~]# rm -f /boot/vmlinuz-2.6.32-754.el6.x86_64
[root@centos6 ~]# reboot
系统启动不了
按ESC ,选择光驱引导
进入光盘救援模式
语言
键盘布局
网络不配置
Continue
/ 根分区被挂载到/mnt/sysimage
#chroot /mnt/sysimage 切换到根目录
#mount /dev/sr0 /mnt/ #挂载光盘
#cp /mnt/isolinux/vmlinuz /boot/vmlinuz-2.6.32-754.el6.x86_64 #从光盘复制内核文件
#sync #存盘
#exit #退出
#reboot #重启
系统可以正常启动
ramdisk文件的制作:
- mkinitrd命令
mkinitrd /boot/initramfs-$(uname -r).img $(uname -r)
- dracut命令
dracut /boot/initramfs-$(uname -r).img $(uname -r)
范例:误删除/boot/initramfs-2.6.32-754.el6.x86_64.img无法启动,故障恢复
[root@centos6 ~]# rm -f /boot/initramfs-2.6.32-754.el6.x86_64.img
[root@centos6 ~]# reboot
按ESC ,选择光驱引导
进入光盘救援模式
语言
键盘布局
网络不配置
Continue
/ 根分区被挂载到/mnt/sysimage
#chroot /mnt/sysimage #切换到根目录
#mkinitrd /boot/initramfs-`uname -r`.img `uname -r` #生成驱动文件
#sync #存盘
#exit #退出
#reboot #重启
系统可以正常启动
1.3.5 init初始化
POST --> BootSequence (BIOS) --> Bootloader(MBR) --> kernel(ramdisk) --> rootfs(只读) --> init(systemd)
init程序的类型:
SysV: init, CentOS 5之前
配置文件:/etc/inittab
Upstart: init,CentOS 6
配置文件:/etc/inittab, /etc/init/*.conf
Systemd:systemd, CentOS 7
配置文件:/usr/lib/systemd/system
/etc/systemd/system
1.3.5.1 运行级别
运行级别:为系统运行或维护等目的而设定;0-6:7个级别,一般使用3, 5做为默认级别
0:关机
1:单用户模式(root自动登录), single, 维护模式
2:多用户模式,启动网络功能,但不会启动NFS;维护模式
3:多用户模式,正常模式;文本界面
4:预留级别;可同3级别
5:多用户模式,正常模式;图形界面
6:重启
切换级别:
init #
查看级别:
runlevel
who -r
定义运行级别
/etc/inittab
CentOS 5 的inittab文件还定义以下内容
初始运行级别(RUN LEVEL)
系统初始化脚本
对应运行级别的脚本目录
捕获某个关键字顺序
定义UPS电源终端/恢复脚本
在虚拟控制台生成getty
在运行级别5初始化X
CentOS 5 的inittab文件每一行格式:
id:runlevel:action:process
id:是惟一标识该项的字符序列
runlevels: 定义了操作所使用的运行级别
action: 指定了要执行的特定操作
wait: 切换至此级别运行一次
respawn:此process终止,就重新启动之
initdefault:设定默认运行级别;process省略
sysinit:设定系统初始化方式
process:定义了要执行的进程
范例:CentOS 5 的inittab文件
id:5:initdefault:
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
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down”
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled”
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
x:5:respawn:/etc/X11/prefdm -nodaemon
CentOS 6 /etc/inittab和相关文件
CentOS 6 init程序为 upstart, 其配置文件/etc/inittab, /etc/init/*.conf,配置文件的语法 遵循 upstart配置文件语法格式,和CentOS5不同
/etc/inittab 设置系统默认的运行级别
/etc/init/control-alt-delete.conf
/etc/init/tty.conf
/etc/init/start-ttys.conf
/etc/init/rc.conf
/etc/init/prefdm.conf
范例:
[root@centos6 ~]# pstree -p
init(1)─┬─auditd(1165)───{auditd}(1166)
├─crond(1356)
├─master(1342)─┬─pickup(1350)
│ └─qmgr(1351)
├─mingetty(1369)
├─mingetty(1371)
├─mingetty(1373)
├─mingetty(1375)
├─mingetty(1377)
├─mingetty(1379)
├─rsyslogd(1187)─┬─{rsyslogd}(1188)
│ ├─{rsyslogd}(1190)
│ └─{rsyslogd}(1191)
├─sshd(1263)─┬─sshd(1381)───bash(1383)───pstree(1405)
│ └─sshd(1384)───sftp-server(1400)
└─udevd(483)─┬─udevd(773)
└─udevd(783)
[root@centos6 ~]# cat /etc/inittab
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used 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: #设置开机进入到哪个模式启动
[root@centos5 ~]# cat /etc/inittab
#
# 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
1.3.5.2 初始化脚本 sysinit
[root@centos6 ~]# file /etc/rc.d/rc.sysinit
/etc/rc.d/rc.sysinit: Bourne-Again shell script text executable
[root@centos6 ~]# cat /etc/rc.d/rc.sysinit
#!/bin/bash
#
# /etc/rc.d/rc.sysinit - run once at boot time
#
# Taken in part from Miquel van Smoorenburg's bcheckrc.
#
HOSTNAME=$(/bin/hostname)
set -m
if [ -f /etc/sysconfig/network ]; then
. /etc/sysconfig/network
fi
if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
HOSTNAME=localhost
fi
if [ ! -e /proc/mounts ]; then
mount -n -t proc /proc /proc
mount -n -t sysfs /sys /sys >/dev/null 2>&1
fi
if [ ! -d /proc/bus/usb ]; then
modprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb
else
mount -n -t usbfs /proc/bus/usb /proc/bus/usb
fi
#remount /dev/shm to set attributes from fstab #669700
mount -n -o remount /dev/shm >/dev/null 2>&1
#remount /proc to set attributes from fstab #984003
mount -n -o remount /proc >/dev/null 2>&1
. /etc/init.d/functions
PLYMOUTH=
[ -x /bin/plymouth ] && PLYMOUTH=yes
# Check SELinux status
SELINUX_STATE=
if [ -e "/selinux/enforce" ] && [ "$(cat /proc/self/attr/current)" != "kernel" ]; then
if [ -r "/selinux/enforce" ] ; then
SELINUX_STATE=$(cat "/selinux/enforce")
else
# assume enforcing if you can't read it
SELINUX_STATE=1
fi
fi
if [ -n "$SELINUX_STATE" -a -x /sbin/restorecon ] && __fgrep " /dev " /proc/mounts >/dev/null 2>&1 ; then
/sbin/restorecon -R -F /dev 2>/dev/null
fi
disable_selinux() {
echo $"*** Warning -- SELinux is active"
echo $"*** Disabling security enforcement for system recovery."
echo $"*** Run 'setenforce 1' to reenable."
echo "0" > "/selinux/enforce"
}
relabel_selinux() {
# if /sbin/init is not labeled correctly this process is running in the
# wrong context, so a reboot will be required after relabel
AUTORELABEL=
. /etc/selinux/config
echo "0" > /selinux/enforce
[ -n "$PLYMOUTH" ] && plymouth --hide-splash
if [ "$AUTORELABEL" = "0" ]; then
echo
echo $"*** Warning -- SELinux ${SELINUXTYPE} policy relabel is required. "
echo $"*** /etc/selinux/config indicates you want to manually fix labeling"
echo $"*** problems. Dropping you to a shell; the system will reboot"
echo $"*** when you leave the shell."
start rcS-emergency
else
echo
echo $"*** Warning -- SELinux ${SELINUXTYPE} policy relabel is required."
echo $"*** Relabeling could take a very long time, depending on file"
echo $"*** system size and speed of hard drives."
/sbin/fixfiles -F restore > /dev/null 2>&1
fi
rm -f /.autorelabel
# at this point fsck was already executed see bz1236062
[ -f /forcefsck ] && rm -f /forcefsck
echo $"Unmounting file systems"
umount -a
mount -n -o remount,ro /
echo $"Automatic reboot in progress."
reboot -f
}
# Print a text banner.
echo -en $"\t\tWelcome to "
read -r system_release < /etc/system-release
if [[ "$system_release" == *"Red Hat"* ]]; then
[ "$BOOTUP" = "color" ] && echo -en "\\033[0;31m"
echo -en "Red Hat"
[ "$BOOTUP" = "color" ] && echo -en "\\033[0;39m"
PRODUCT=$(sed "s/Red Hat \(.*\) release.*/\1/" /etc/system-release)
echo " $PRODUCT"
elif [[ "$system_release" == *Fedora* ]]; then
[ "$BOOTUP" = "color" ] && echo -en "\\033[0;34m"
echo -en "Fedora"
[ "$BOOTUP" = "color" ] && echo -en "\\033[0;39m"
PRODUCT=$(sed "s/Fedora \(.*\) \?release.*/\1/" /etc/system-release)
echo " $PRODUCT"
elif [[ "$system_release" =~ "CentOS" ]]; then
[ "$BOOTUP" = "color" ] && echo -en "\\033[0;36m"
echo -en "CentOS"
[ "$BOOTUP" = "color" ] && echo -en "\\033[0;39m"
PRODUCT=$(sed "s/CentOS \(.*\) \?release.*/\1/" /etc/system-release)
echo " $PRODUCT"
else
PRODUCT=$(sed "s/ release.*//g" /etc/system-release)
echo "$PRODUCT"
fi
# Only read this once.
cmdline=$(cat /proc/cmdline)
# Initialize hardware
if [ -f /proc/sys/kernel/modprobe ]; then
if ! strstr "$cmdline" nomodules && [ -f /proc/modules ] ; then
sysctl -w kernel.modprobe="/sbin/modprobe" >/dev/null 2>&1
else
# We used to set this to NULL, but that causes 'failed to exec' messages"
sysctl -w kernel.modprobe="/bin/true" >/dev/null 2>&1
fi
fi
touch /dev/.in_sysinit >/dev/null 2>&1
# Set default affinity
if [ -x /bin/taskset ]; then
if strstr "$cmdline" default_affinity= ; then
for arg in $cmdline ; do
if [ "${arg##default_affinity=}" != "${arg}" ]; then
/bin/taskset -p ${arg##default_affinity=} 1
/bin/taskset -p ${arg##default_affinity=} $$
fi
done
fi
fi
nashpid=$(pidof nash 2>/dev/null)
[ -n "$nashpid" ] && kill $nashpid >/dev/null 2>&1
unset nashpid
apply_sysctl
/sbin/start_udev
# Load other user-defined modules
for file in /etc/sysconfig/modules/*.modules ; do
[ -x $file ] && $file
done
# Load modules (for backward compatibility with VARs)
if [ -f /etc/rc.modules ]; then
/etc/rc.modules
fi
mount -n /dev/pts >/dev/null 2>&1
[ -n "$SELINUX_STATE" ] && restorecon -F /dev/pts >/dev/null 2>&1
# Configure kernel parameters
update_boot_stage RCkernelparam
apply_sysctl
# Set the hostname.
update_boot_stage RChostname
action $"Setting hostname ${HOSTNAME}: " hostname ${HOSTNAME}
[ -n "${NISDOMAIN}" ] && domainname ${NISDOMAIN}
# Sync waiting for storage.
{ rmmod scsi_wait_scan ; modprobe scsi_wait_scan ; rmmod scsi_wait_scan ; } >/dev/null 2>&1
# Device mapper & related initialization
if ! __fgrep "device-mapper" /proc/devices >/dev/null 2>&1 ; then
modprobe dm-mod >/dev/null 2>&1
fi
if [ -f /etc/crypttab ]; then
init_crypto 0
fi
if ! strstr "$cmdline" nompath && [ -f /etc/multipath.conf -a \
-x /sbin/multipath ]; then
modprobe dm-multipath > /dev/null 2>&1
/sbin/multipath -v 0
if [ -x /sbin/kpartx ]; then
action_silent $"Add partition mappings: " /sbin/dmsetup ls --target multipath --exec "/sbin/kpartx -a -p p"
fi
fi
if ! strstr "$cmdline" nodmraid && [ -x /sbin/dmraid ]; then
modprobe dm-mirror >/dev/null 2>&1
dmraidsets=$(LC_ALL=C /sbin/dmraid -s -c -i)
if [ "$?" = "0" ]; then
for dmname in $dmraidsets; do
if [[ "$dmname" == isw_* ]] && \
! strstr "$cmdline" noiswmd; then
continue
fi
action_silent $"Activate software (ATA)RAID: " /sbin/dmraid -ay -i --rm_partitions -p "$dmname"
/sbin/kpartx -a -p p "/dev/mapper/$dmname"
done
fi
fi
# Start any MD RAID arrays that haven't been started yet
[ -r /proc/mdstat -a -r /dev/md/md-device-map ] && action $"Run MD devices: " /sbin/mdadm -IRs
if [ -x /sbin/lvm ]; then
if [ ! -f /.nolvm ] && ! strstr "$cmdline" nolvm ; then
action $"Setting up Logical Volume Management:" /sbin/lvm vgchange -a ay --sysinit --ignoreskippedcluster
else
echo $"Logical Volume Management disabled at boot."
fi
fi
if [ -f /etc/crypttab ]; then
init_crypto 0
fi
if [ -f /fastboot ] || strstr "$cmdline" fastboot ; then
fastboot=yes
fi
if [ -f /fsckoptions ]; then
fsckoptions=$(cat /fsckoptions)
fi
if [ -f /forcefsck ] || strstr "$cmdline" forcefsck ; then
fsckoptions="-f $fsckoptions"
elif [ -f /.autofsck ]; then
[ -f /etc/sysconfig/autofsck ] && . /etc/sysconfig/autofsck
if [ "$AUTOFSCK_DEF_CHECK" = "yes" ]; then
AUTOFSCK_OPT="$AUTOFSCK_OPT -f"
fi
if [ -n "$AUTOFSCK_SINGLEUSER" ]; then
[ -n "$PLYMOUTH" ] && plymouth --hide-splash
echo
echo $"*** Warning -- the system did not shut down cleanly. "
echo $"*** Dropping you to a shell; the system will continue"
echo $"*** when you leave the shell."
[ -n "$SELINUX_STATE" ] && echo "0" > /selinux/enforce
start rcS-emergency
[ -n "$SELINUX_STATE" ] && echo "1" > /selinux/enforce
[ -n "$PLYMOUTH" ] && plymouth --show-splash
fi
fsckoptions="$AUTOFSCK_OPT $fsckoptions"
fi
if [ "$BOOTUP" = "color" ]; then
fsckoptions="-C $fsckoptions"
else
fsckoptions="-V $fsckoptions"
fi
READONLY=
if [ -f /etc/sysconfig/readonly-root ]; then
. /etc/sysconfig/readonly-root
fi
if strstr "$cmdline" readonlyroot ; then
READONLY=yes
[ -z "$RW_MOUNT" ] && RW_MOUNT=/var/lib/stateless/writable
[ -z "$STATE_MOUNT" ] && STATE_MOUNT=/var/lib/stateless/state
fi
if strstr "$cmdline" noreadonlyroot ; then
READONLY=no
fi
if [ "$READONLY" = "yes" -o "$TEMPORARY_STATE" = "yes" ]; then
mount_empty() {
if [ -e "$1" ]; then
echo "$1" | cpio -p -vd "$RW_MOUNT" &>/dev/null
mount -n --bind "$RW_MOUNT$1" "$1"
fi
}
mount_dirs() {
if [ -e "$1" ]; then
mkdir -p "$RW_MOUNT$1"
find "$1" -type d -print0 | cpio -p -0vd "$RW_MOUNT" &>/dev/null
mount -n --bind "$RW_MOUNT$1" "$1"
fi
}
mount_files() {
if [ -e "$1" ]; then
cp -a --parents "$1" "$RW_MOUNT"
mount -n --bind "$RW_MOUNT$1" "$1"
fi
}
# Common mount options for scratch space regardless of
# type of backing store
mountopts=
# Scan partitions for local scratch storage
rw_mount_dev=$(blkid -t LABEL="$RW_LABEL" -l -o device)
# First try to mount scratch storage from /etc/fstab, then any
# partition with the proper label. If either succeeds, be sure
# to wipe the scratch storage clean. If both fail, then mount
# scratch storage via tmpfs.
if mount $mountopts "$RW_MOUNT" > /dev/null 2>&1 ; then
rm -rf "$RW_MOUNT" > /dev/null 2>&1
elif [ x$rw_mount_dev != x ] && mount $rw_mount_dev $mountopts "$RW_MOUNT" > /dev/null 2>&1; then
rm -rf "$RW_MOUNT" > /dev/null 2>&1
else
mount -n -t tmpfs $RW_OPTIONS $mountopts none "$RW_MOUNT"
fi
for file in /etc/rwtab /etc/rwtab.d/* /dev/.initramfs/rwtab ; do
is_ignored_file "$file" && continue
[ -f $file ] && cat $file | while read type path ; do
case "$type" in
empty)
mount_empty $path
;;
files)
mount_files $path
;;
dirs)
mount_dirs $path
;;
*)
;;
esac
[ -n "$SELINUX_STATE" -a -e "$path" ] && restorecon -R "$path"
done
done
# Use any state passed by initramfs
[ -d /dev/.initramfs/state ] && cp -a /dev/.initramfs/state/* $RW_MOUNT
# In theory there should be no more than one network interface active
# this early in the boot process -- the one we're booting from.
# Use the network address to set the hostname of the client. This
# must be done even if we have local storage.
ipaddr=
if [ "$HOSTNAME" = "localhost" -o "$HOSTNAME" = "localhost.localdomain" ]; then
ipaddr=$(ip addr show to 0.0.0.0/0 scope global | awk '/[[:space:]]inet / { print gensub("/.*","","g",$2) }')
for ip in $ipaddr ; do
HOSTNAME=
eval $(ipcalc -h $ip 2>/dev/null)
[ -n "$HOSTNAME" ] && { hostname ${HOSTNAME} ; break; }
done
fi
# Clients with read-only root filesystems may be provided with a
# place where they can place minimal amounts of persistent
# state. SSH keys or puppet certificates for example.
#
# Ideally we'll use puppet to manage the state directory and to
# create the bind mounts. However, until that's all ready this
# is sufficient to build a working system.
# First try to mount persistent data from /etc/fstab, then any
# partition with the proper label, then fallback to NFS
state_mount_dev=$(blkid -t LABEL="$STATE_LABEL" -l -o device)
if mount $mountopts $STATE_OPTIONS "$STATE_MOUNT" > /dev/null 2>&1 ; then
/bin/true
elif [ x$state_mount_dev != x ] && mount $state_mount_dev $mountopts "$STATE_MOUNT" > /dev/null 2>&1; then
/bin/true
elif [ ! -z "$CLIENTSTATE" ]; then
# No local storage was found. Make a final attempt to find
# state on an NFS server.
mount -t nfs $CLIENTSTATE/$HOSTNAME $STATE_MOUNT -o rw,nolock
fi
if [ -w "$STATE_MOUNT" ]; then
mount_state() {
if [ -e "$1" ]; then
[ ! -e "$STATE_MOUNT$1" ] && cp -a --parents "$1" "$STATE_MOUNT"
mount -n --bind "$STATE_MOUNT$1" "$1"
fi
}
for file in /etc/statetab /etc/statetab.d/* ; do
is_ignored_file "$file" && continue
[ ! -f "$file" ] && continue
if [ -f "$STATE_MOUNT/$file" ] ; then
mount -n --bind "$STATE_MOUNT/$file" "$file"
fi
for path in $(grep -v "^#" "$file" 2>/dev/null); do
mount_state "$path"
[ -n "$SELINUX_STATE" -a -e "$path" ] && restorecon -R "$path"
done
done
if [ -f "$STATE_MOUNT/files" ] ; then
for path in $(grep -v "^#" "$STATE_MOUNT/files" 2>/dev/null); do
mount_state "$path"
[ -n "$SELINUX_STATE" -a -e "$path" ] && restorecon -R "$path"
done
fi
fi
if mount | grep -q /var/lib/nfs/rpc_pipefs ; then
mount -t rpc_pipefs sunrpc /var/lib/nfs/rpc_pipefs && service rpcidmapd restart
fi
fi
if [[ " $fsckoptions" != *" -y"* ]]; then
fsckoptions="-a $fsckoptions"
fi
_RUN_QUOTACHECK=0
if [ -f /forcequotacheck ] || strstr "$cmdline" forcequotacheck ; then
_RUN_QUOTACHECK=1
fi
if [ -z "$fastboot" -a "$READONLY" != "yes" ]; then
STRING=$"Checking filesystems"
echo $STRING
fsck -T -t noopts=_netdev -A $fsckoptions
rc=$?
if [ "$rc" -eq "0" ]; then
success "$STRING"
echo
elif [ "$rc" -eq "1" ]; then
passed "$STRING"
echo
elif [ "$rc" -eq "2" -o "$rc" -eq "3" ]; then
echo $"Unmounting file systems"
umount -a
mount -n -o remount,ro /
echo $"Automatic reboot in progress."
reboot -f
fi
# A return of 4 or higher means there were serious problems.
if [ $rc -gt 1 ]; then
[ -n "$PLYMOUTH" ] && plymouth --hide-splash
failure "$STRING"
echo
echo
echo $"*** An error occurred during the file system check."
echo $"*** Dropping you to a shell; the system will reboot"
echo $"*** when you leave the shell."
str=$"(Repair filesystem)"
PS1="$str \# # "; export PS1
[ "$SELINUX_STATE" = "1" ] && disable_selinux
start rcS-emergency
echo $"Unmounting file systems"
umount -a
mount -n -o remount,ro /
echo $"Automatic reboot in progress."
reboot -f
elif [ "$rc" -eq "1" ]; then
_RUN_QUOTACHECK=1
fi
fi
remount_needed() {
local state oldifs
[ "$READONLY" = "yes" ] && return 1
state=$(LC_ALL=C awk '/ \/ / && ($3 !~ /rootfs/) { print $4 }' /proc/mounts)
oldifs=$IFS
IFS=","
for opt in $state ; do
if [ "$opt" = "rw" ]; then
IFS=$oldifs
return 1
fi
done
IFS=$oldifs
return 0
}
# Remount the root filesystem read-write.
update_boot_stage RCmountfs
if remount_needed ; then
action $"Remounting root filesystem in read-write mode: " mount -n -o remount,rw /
fi
# Clean up SELinux labels
if [ -n "$SELINUX_STATE" ]; then
restorecon /etc/mtab /etc/ld.so.cache /etc/blkid/blkid.tab /etc/resolv.conf >/dev/null 2>&1
fi
# If relabeling, relabel mount points.
if [ -n "$SELINUX_STATE" -a "$READONLY" != "yes" ]; then
if [ -f /.autorelabel ] || strstr "$cmdline" autorelabel ; then
restorecon $(awk '!/^#/ && $4 !~ /noauto/ && $2 ~ /^\// { print $2 }' /etc/fstab) >/dev/null 2>&1
fi
fi
if [ "$READONLY" != "yes" ] ; then
# Clear mtab
(> /etc/mtab) &> /dev/null
# Remove stale backups
rm -f /etc/mtab~ /etc/mtab~~
# Enter mounted filesystems into /etc/mtab
mount -f /
mount -f /proc >/dev/null 2>&1
mount -f /sys >/dev/null 2>&1
mount -f /dev/pts >/dev/null 2>&1
mount -f /dev/shm >/dev/null 2>&1
mount -f /proc/bus/usb >/dev/null 2>&1
fi
# Mount all other filesystems (except for NFS and /proc, which is already
# mounted). Contrary to standard usage,
# filesystems are NOT unmounted in single user mode.
# The 'no' applies to all listed filesystem types. See mount(8).
if [ "$READONLY" != "yes" ] ; then
action $"Mounting local filesystems: " mount -a -t nonfs,nfs4,smbfs,ncpfs,cifs,gfs,gfs2,glusterfs -O no_netdev
else
action $"Mounting local filesystems: " mount -a -n -t nonfs,nfs4,smbfs,ncpfs,cifs,gfs,gfs2,glusterfs -O no_netdev
fi
# Check to see if a full relabel is needed
if [ -n "$SELINUX_STATE" -a "$READONLY" != "yes" ]; then
if [ -f /.autorelabel ] || strstr "$cmdline" autorelabel ; then
relabel_selinux
fi
else
if [ -d /etc/selinux -a "$READONLY" != "yes" ]; then
[ -f /.autorelabel ] || touch /.autorelabel
fi
fi
# Update quotas if necessary
if [ X"$_RUN_QUOTACHECK" = X1 -a -x /sbin/quotacheck ]; then
action $"Checking local filesystem quotas: " /sbin/quotacheck -anug
fi
if [ -x /sbin/quotaon ]; then
action $"Enabling local filesystem quotas: " /sbin/quotaon -aug
fi
# Initialize pseudo-random number generator
if [ -f "/var/lib/random-seed" ]; then
cat /var/lib/random-seed > /dev/urandom
else
[ "$READONLY" != "yes" ] && touch /var/lib/random-seed
fi
if [ "$READONLY" != "yes" ]; then
chmod 600 /var/lib/random-seed
dd if=/dev/urandom of=/var/lib/random-seed count=1 bs=4096 2>/dev/null
fi
if [ -f /etc/crypttab ]; then
init_crypto 1
fi
# Configure machine if necessary.
if [ -f /.unconfigured ]; then
if [ -x /bin/plymouth ]; then
/bin/plymouth quit
fi
if [ -x /usr/bin/system-config-keyboard ]; then
/usr/bin/system-config-keyboard
fi
if [ -x /usr/bin/passwd ]; then
/usr/bin/passwd root
fi
if [ -x /usr/sbin/system-config-network-tui ]; then
/usr/sbin/system-config-network-tui
fi
if [ -x /usr/sbin/timeconfig ]; then
/usr/sbin/timeconfig
fi
if [ -x /usr/sbin/authconfig-tui ]; then
/usr/sbin/authconfig-tui --nostart
fi
if [ -x /usr/sbin/ntsysv ]; then
/usr/sbin/ntsysv --level 35
fi
# Reread in network configuration data.
if [ -f /etc/sysconfig/network ]; then
. /etc/sysconfig/network
# Reset the hostname.
action $"Resetting hostname ${HOSTNAME}: " hostname ${HOSTNAME}
fi
rm -f /.unconfigured
fi
# Clean out /.
rm -f /fastboot /fsckoptions /forcefsck /.autofsck /forcequotacheck /halt \
/poweroff /.suspended &> /dev/null
# Do we need (w|u)tmpx files? We don't set them up, but the sysadmin might...
_NEED_XFILES=
[ -f /var/run/utmpx -o -f /var/log/wtmpx ] && _NEED_XFILES=1
# Clean up /var.
rm -rf /var/lock/cvs/* /var/run/screen/*
find /var/lock /var/run ! -type d -exec rm -f {} \;
rm -f /var/lib/rpm/__db* &> /dev/null
rm -f /var/gdm/.gdmfifo &> /dev/null
[ "$PROMPT" != no ] && plymouth watch-keystroke --command "touch /var/run/confirm" --keys=Ii &
# Clean up utmp/wtmp
> /var/run/utmp
touch /var/log/wtmp
chgrp utmp /var/run/utmp /var/log/wtmp
chmod 0664 /var/run/utmp /var/log/wtmp
if [ -n "$_NEED_XFILES" ]; then
> /var/run/utmpx
touch /var/log/wtmpx
chgrp utmp /var/run/utmpx /var/log/wtmpx
chmod 0664 /var/run/utmpx /var/log/wtmpx
fi
[ -n "$SELINUX_STATE" ] && restorecon /var/run/utmp* /var/log/wtmp* >/dev/null 2>&1
# Clean up various /tmp bits
[ -n "$SELINUX_STATE" ] && restorecon /tmp
rm -f /tmp/.X*-lock /tmp/.lock.* /tmp/.gdm_socket /tmp/.s.PGSQL.*
rm -rf /tmp/.X*-unix /tmp/.ICE-unix /tmp/.font-unix /tmp/hsperfdata_* \
/tmp/kde-* /tmp/ksocket-* /tmp/mc-* /tmp/mcop-* /tmp/orbit-* \
/tmp/scrollkeeper-* /tmp/ssh-* \
/dev/.in_sysinit
# Make ICE directory
mkdir -m 1777 -p /tmp/.ICE-unix >/dev/null 2>&1
chown root:root /tmp/.ICE-unix
[ -n "$SELINUX_STATE" ] && restorecon /tmp/.ICE-unix >/dev/null 2>&1
# Start up swapping.
update_boot_stage RCswap
action $"Enabling /etc/fstab swaps: " swapon -a -e
if [ "$AUTOSWAP" = "yes" ]; then
curswap=$(awk '/^\/dev/ { print $1 }' /proc/swaps | while read x; do get_numeric_dev dec $x ; echo -n " "; done)
swappartitions=$(blkid -t TYPE=swap -o device)
if [ x"$swappartitions" != x ]; then
for partition in $swappartitions ; do
[ ! -e $partition ] && continue
majmin=$(get_numeric_dev dec $partition)
echo $curswap | grep -qw "$majmin" || action $"Enabling local swap partitions: " swapon $partition
done
fi
fi
# Set up binfmt_misc
/bin/mount -t binfmt_misc none /proc/sys/fs/binfmt_misc > /dev/null 2>&1
# Boot time profiles. Yes, this should be somewhere else.
if [ -x /usr/sbin/system-config-network-cmd ]; then
if strstr "$cmdline" netprofile= ; then
for arg in $cmdline ; do
if [ "${arg##netprofile=}" != "${arg}" ]; then
/usr/sbin/system-config-network-cmd --profile ${arg##netprofile=}
fi
done
fi
fi
# Now that we have all of our basic modules loaded and the kernel going,
# let's dump the syslog ring somewhere so we can find it later
[ -f /var/log/dmesg ] && mv -f /var/log/dmesg /var/log/dmesg.old
dmesg -s 131072 > /var/log/dmesg
# create the crash indicator flag to warn on crashes, offer fsck with timeout
touch /.autofsck &> /dev/null
[ "$PROMPT" != no ] && plymouth --ignore-keystroke=Ii
if strstr "$cmdline" confirm ; then
touch /var/run/confirm
fi
# Let rhgb know that we're leaving rc.sysinit
if [ -x /bin/plymouth ]; then
/bin/plymouth --sysinit
fi
系统初始化脚本功能
(1) 设置主机名
(2) 设置欢迎信息
(3) 激活udev和selinux
(4) 挂载/etc/fstab文件中定义的文件系统
(5) 检测根文件系统,并以读写方式重新挂载根文件系统
(6) 设置系统时钟
(7) 激活swap设备
(8) 根据/etc/sysctl.conf文件设置内核参数
(9) 激活lvm及software raid设备
(10)加载额外设备的驱动程序
(11)清理操作
范例:
[root@centos6 ~]# cat /etc/init/rcS.conf
# rcS - runlevel compatibility
#
# This task runs the old sysv-rc startup scripts.
#
# Do not edit this file directly. If you want to change the behaviour,
# please create a file rcS.override and put your changes there.
start on startup
stop on runlevel
task
# Note: there can be no previous runlevel here, if we have one it's bad
# information (we enter rc1 not rcS for maintenance). Run /etc/rc.d/rc
# without information so that it defaults to previous=N runlevel=S.
console output
pre-start script
for t in $(cat /proc/cmdline); do
case $t in
emergency)
start rcS-emergency
break
;;
esac
done
end script
exec /etc/rc.d/rc.sysinit
post-stop script
if [ "$UPSTART_EVENTS" = "startup" ]; then
[ -f /etc/inittab ] && runlevel=$(/bin/awk -F ':' '$3 == "initdefault" && $1 !~ "^#" { print $2 }' /etc/inittab)
[ -z "$runlevel" ] && runlevel="3"
for t in $(cat /proc/cmdline); do
case $t in
-s|single|S|s) runlevel="S" ;;
[1-9]) runlevel="$t" ;;
esac
done
exec telinit $runlevel
fi
end script
1.3.5.3 服务管理
[root@centos6 ~]# cat /etc/init/rc.conf
# rc - System V runlevel compatibility
#
# This task runs the old sysv-rc runlevel scripts. It
# is usually started by the telinit compatibility wrapper.
#
# Do not edit this file directly. If you want to change the behaviour,
# please create a file rc.override and put your changes there.
start on runlevel [0123456]
stop on runlevel [!$RUNLEVEL]
task
export RUNLEVEL
console output
exec /etc/rc.d/rc $RUNLEVEL
[root@centos6 ~]# file /etc/rc.d/rc
/etc/rc.d/rc: Bourne-Again shell script text executable
[root@centos6 ~]# cat /etc/rc.d/rc
#! /bin/bash
#
# rc This file is responsible for starting/stopping
# services when the runlevel changes.
#
# Original Author:
# Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#
set -m
# check a file to be a correct runlevel script
check_runlevel ()
{
# Check if the file exists at all.
[ -x "$1" ] || return 1
is_ignored_file "$1" && return 1
return 0
}
# Now find out what the current and what the previous runlevel are.
argv1="$1" #根据这个$1参数运行
set $(/sbin/runlevel)
runlevel=$2
previous=$1
export runlevel previous
. /etc/init.d/functions
export CONSOLETYPE
do_confirm="no"
if [ -f /var/run/confirm ]; then
do_confirm="yes"
fi
UPSTART=
[ -x /sbin/initctl ] && UPSTART=yes
# See if we want to be in user confirmation mode
if [ "$previous" = "N" ]; then
if [ "$do_confirm" = "yes" ]; then
echo $"Entering interactive startup"
else
echo $"Entering non-interactive startup"
fi
fi
# Get first argument. Set new runlevel to this argument.
[ -n "$argv1" ] && runlevel="$argv1"
# Is there an rc directory for this new runlevel?
[ -d /etc/rc$runlevel.d ] || exit 0
# Set language, vc settings once to avoid doing it for every init script
# through functions
if [ -f /etc/sysconfig/i18n -a -z "${NOLOCALE:-}" ] ; then
. /etc/profile.d/lang.sh 2>/dev/null
export LANGSH_SOURCED=1
fi
# First, run the KILL scripts. #先运行这段代码
for i in /etc/rc$runlevel.d/K* ; do
# Check if the subsystem is already up.
subsys=${i#/etc/rc$runlevel.d/K??}
[ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] || continue
check_runlevel "$i" || continue
# Bring the subsystem down.
[ -n "$UPSTART" ] && initctl emit --quiet stopping JOB=$subsys
$i stop
[ -n "$UPSTART" ] && initctl emit --quiet stopped JOB=$subsys
done
# Now run the START scripts. #再运行这段代码
for i in /etc/rc$runlevel.d/S* ; do
# Check if the subsystem is already up.
subsys=${i#/etc/rc$runlevel.d/S??}
[ -f /var/lock/subsys/$subsys ] && continue
[ -f /var/lock/subsys/$subsys.init ] && continue
check_runlevel "$i" || continue
# If we're in confirmation mode, get user confirmation
if [ "$do_confirm" = "yes" ]; then
confirm $subsys
rc=$?
if [ "$rc" = "1" ]; then
continue
elif [ "$rc" = "2" ]; then
do_confirm="no"
fi
fi
update_boot_stage "$subsys"
# Bring the subsystem up.
[ -n "$UPSTART" ] && initctl emit --quiet starting JOB=$subsys
if [ "$subsys" = "halt" -o "$subsys" = "reboot" ]; then
export LC_ALL=C
exec $i start
fi
$i start
[ -n "$UPSTART" ] && initctl emit --quiet started JOB=$subsys
done
[ "$do_confirm" = "yes" ] && rm -f /var/run/confirm
exit 0
[root@centos6 ~]# ls /etc/rc*.d -d
/etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rc3.d /etc/rc4.d /etc/rc5.d /etc/rc6.d /etc/rc.d
[root@centos6 ~]# ls /etc/rc3.d
K10saslauthd K87restorecond K89rdisc S08ip6tables S10network S12rsyslog S25blk-availability S26udev-post S55sshd S90crond
K61nfs-rdma K89netconsole S05rdma S08iptables S11auditd S15mdmonitor S25netfs S50kdump S80postfix S99local
[root@centos6 ~]# ll /etc/rc.d/rc3.d
total 0
lrwxrwxrwx. 1 root root 19 Oct 6 23:45 K10saslauthd -> ../init.d/saslauthd
lrwxrwxrwx. 1 root root 18 Oct 6 23:46 K61nfs-rdma -> ../init.d/nfs-rdma
lrwxrwxrwx. 1 root root 21 Oct 6 23:45 K87restorecond -> ../init.d/restorecond
lrwxrwxrwx. 1 root root 20 Oct 6 23:45 K89netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 15 Oct 6 23:45 K89rdisc -> ../init.d/rdisc
lrwxrwxrwx. 1 root root 14 Oct 6 23:46 S05rdma -> ../init.d/rdma
lrwxrwxrwx. 1 root root 19 Oct 6 23:46 S08ip6tables -> ../init.d/ip6tables
lrwxrwxrwx. 1 root root 18 Oct 6 23:45 S08iptables -> ../init.d/iptables
lrwxrwxrwx. 1 root root 17 Oct 6 23:45 S10network -> ../init.d/network
lrwxrwxrwx. 1 root root 16 Oct 6 23:46 S11auditd -> ../init.d/auditd
lrwxrwxrwx. 1 root root 17 Oct 6 23:45 S12rsyslog -> ../init.d/rsyslog
lrwxrwxrwx. 1 root root 19 Oct 6 23:45 S15mdmonitor -> ../init.d/mdmonitor
lrwxrwxrwx. 1 root root 26 Oct 6 23:45 S25blk-availability -> ../init.d/blk-availability
lrwxrwxrwx. 1 root root 15 Oct 6 23:45 S25netfs -> ../init.d/netfs
lrwxrwxrwx. 1 root root 19 Oct 6 23:45 S26udev-post -> ../init.d/udev-post
lrwxrwxrwx. 1 root root 15 Oct 6 23:46 S50kdump -> ../init.d/kdump
lrwxrwxrwx. 1 root root 14 Oct 6 23:46 S55sshd -> ../init.d/sshd
lrwxrwxrwx. 1 root root 17 Oct 6 23:45 S80postfix -> ../init.d/postfix
lrwxrwxrwx. 1 root root 15 Oct 6 23:45 S90crond -> ../init.d/crond
lrwxrwxrwx. 1 root root 11 Oct 6 23:45 S99local -> ../rc.local
service 命令:手动管理服务
service 服务 start|stop|restart
service --status-all
/etc/rc.d/rc 控制服务脚本的开机自动运行
for srv in /etc/rc.d/rcN.d/K*; do
$srv stop
done
for srv in /etc/rc.d/rcN.d/S*; do
$srv start
done
说明:rc N --> 意味着读取/etc/rc.d/rcN.d/
K: K##:##运行次序;数字越小,越先运行;数字越小的服务,通常为依赖到别的服务
S: S##:##运行次序;数字越小,越先运行;数字越小的服务,通常为被依赖到的服务
配置服务开机启动
- chkconfig命令
- ntsysv命令
chkconfig 命令管理服务
#查看服务在所有级别的启动或关闭设定情形:
chkconfig [--list] [name]
#添加服务
SysV的服务脚本放置于/etc/rc.d/init.d (/etc/init.d)
#!/bin/bash
chkconfig: LLLL nn nn #LLLL 表示初始在哪个级别下启动,-表示都不启动
description : 描述信息
chkconfig --add name
#删除服务
chkconfig --del name
#修改指定的运行级别
chkconfig [--level levels] name <on|off|reset>
说明:--level LLLL: 指定要设置的级别;省略时表示2345
范例:
[root@centos6 ~]# service crond status
crond (pid 1358) is running...
[root@centos6 ~]# service rdisc status
rdisc is stopped
[root@centos6 ~]# chkconfig --list
auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
kdump 0:off 1:off 2:off 3:on 4:on 5:on 6:off
mdmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 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
nfs-rdma 0:off 1:off 2:off 3:off 4:off 5:off 6:off
postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off
rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rdma 0:off 1:on 2:on 3:on 4:on 5:on 6:off
restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
saslauthd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off
[root@centos6 ~]# chkconfig --level 2345 crond off
[root@centos6 ~]# chkconfig --list crond
crond 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@centos6 ~]# ls /etc/rc*.d/*crond
/etc/rc0.d/K60crond /etc/rc2.d/K60crond /etc/rc4.d/K60crond /etc/rc6.d/K60crond
/etc/rc1.d/K60crond /etc/rc3.d/K60crond /etc/rc5.d/K60crond
[root@centos6 ~]# chkconfig crond on
[root@centos6 ~]# chkconfig --list crond
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@centos6 ~]# ls /etc/rc*.d/*crond
/etc/rc0.d/K60crond /etc/rc2.d/S90crond /etc/rc4.d/S90crond /etc/rc6.d/K60crond
/etc/rc1.d/K60crond /etc/rc3.d/S90crond /etc/rc5.d/S90crond
[root@centos6 ~]# ls /etc/rc3.d/*
/etc/rc3.d/K10saslauthd /etc/rc3.d/S05rdma /etc/rc3.d/S12rsyslog /etc/rc3.d/S50kdump /etc/rc3.d/S99local
/etc/rc3.d/K61nfs-rdma /etc/rc3.d/S08ip6tables /etc/rc3.d/S15mdmonitor /etc/rc3.d/S55sshd
/etc/rc3.d/K87restorecond /etc/rc3.d/S08iptables /etc/rc3.d/S25blk-availability /etc/rc3.d/S56xinetd
/etc/rc3.d/K89netconsole /etc/rc3.d/S10network /etc/rc3.d/S25netfs /etc/rc3.d/S80postfix
/etc/rc3.d/K89rdisc /etc/rc3.d/S11auditd /etc/rc3.d/S26udev-post /etc/rc3.d/S90crond
[root@centos6 ~]# ls /etc/init.d
auditd functions iptables mdmonitor network rdisc rsyslog single udev-post
blk-availability halt kdump netconsole nfs-rdma rdma sandbox sshd
crond ip6tables killall netfs postfix restorecond saslauthd testsrv
[root@centos6 ~]# ls /etc/init.d/crond
/etc/init.d/crond
[root@centos6 ~]# cat /etc/init.d/crond
#!/bin/sh
#
# crond Start/Stop the cron clock daemon.
#
# chkconfig: 2345 90 60
# description: cron is a standard UNIX program that runs user-specified \
# programs at periodic scheduled times. vixie cron adds a \
# number of features to the basic UNIX cron, including better \
# security and more powerful configuration options.
### BEGIN INIT INFO
# Provides: crond crontab
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog
# Default-Start: 2345
# Default-Stop: 90
# Short-Description: run cron daemon
# Description: cron is a standard UNIX program that runs user-specified
# programs at periodic scheduled times. vixie cron adds a
# number of features to the basic UNIX cron, including better
# security and more powerful configuration options.
### END INIT INFO
[ -f /etc/sysconfig/crond ] || {
[ "$1" = "status" ] && exit 4 || exit 6
}
RETVAL=0
prog="crond"
exec=/usr/sbin/crond
lockfile=/var/lock/subsys/crond
config=/etc/sysconfig/crond
# Source function library.
. /etc/rc.d/init.d/functions
[ $UID -eq 0 ] && [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
start() {
if [ $UID -ne 0 ] ; then
echo "User has insufficient privilege."
exit 4
fi
[ -x $exec ] || exit 5
[ -f $config ] || exit 6
echo -n $"Starting $prog: "
daemon $prog $CRONDARGS
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
}
stop() {
if [ $UID -ne 0 ] ; then
echo "User has insufficient privilege."
exit 4
fi
echo -n $"Stopping $prog: "
if [ -n "`pidfileofproc $exec`" ]; then
killproc $exec
RETVAL=3
else
failure $"Stopping $prog"
fi
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
}
restart() {
rh_status_q && stop
start
}
reload() {
echo -n $"Reloading $prog: "
if [ -n "`pidfileofproc $exec`" ]; then
killproc $exec -HUP
else
failure $"Reloading $prog"
fi
retval=$?
echo
}
force_reload() {
# new configuration takes effect after restart
restart
}
rh_status() {
# run checks to determine if the service is running or use generic status
status -p /var/run/crond.pid $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
exit $?
[root@centos6 ~]# ls /etc/rc3.d/*
/etc/rc3.d/K10saslauthd /etc/rc3.d/K89rdisc /etc/rc3.d/S10network /etc/rc3.d/S25blk-availability /etc/rc3.d/S55sshd
/etc/rc3.d/K61nfs-rdma /etc/rc3.d/S05rdma /etc/rc3.d/S11auditd /etc/rc3.d/S25netfs /etc/rc3.d/S80postfix
/etc/rc3.d/K87restorecond /etc/rc3.d/S08ip6tables /etc/rc3.d/S12rsyslog /etc/rc3.d/S26udev-post /etc/rc3.d/S90crond
/etc/rc3.d/K89netconsole /etc/rc3.d/S08iptables /etc/rc3.d/S15mdmonitor /etc/rc3.d/S50kdump /etc/rc3.d/S99local
[root@centos6 ~]# ll /etc/rc3.d/*
lrwxrwxrwx. 1 root root 19 Oct 6 23:45 /etc/rc3.d/K10saslauthd -> ../init.d/saslauthd
lrwxrwxrwx. 1 root root 18 Oct 6 23:46 /etc/rc3.d/K61nfs-rdma -> ../init.d/nfs-rdma
lrwxrwxrwx. 1 root root 21 Oct 6 23:45 /etc/rc3.d/K87restorecond -> ../init.d/restorecond
lrwxrwxrwx. 1 root root 20 Oct 6 23:45 /etc/rc3.d/K89netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 15 Oct 6 23:45 /etc/rc3.d/K89rdisc -> ../init.d/rdisc
lrwxrwxrwx. 1 root root 14 Oct 6 23:46 /etc/rc3.d/S05rdma -> ../init.d/rdma
lrwxrwxrwx. 1 root root 19 Oct 6 23:46 /etc/rc3.d/S08ip6tables -> ../init.d/ip6tables
lrwxrwxrwx. 1 root root 18 Oct 6 23:45 /etc/rc3.d/S08iptables -> ../init.d/iptables
lrwxrwxrwx. 1 root root 17 Oct 6 23:45 /etc/rc3.d/S10network -> ../init.d/network
lrwxrwxrwx. 1 root root 16 Oct 6 23:46 /etc/rc3.d/S11auditd -> ../init.d/auditd
lrwxrwxrwx. 1 root root 17 Oct 6 23:45 /etc/rc3.d/S12rsyslog -> ../init.d/rsyslog
lrwxrwxrwx. 1 root root 19 Oct 6 23:45 /etc/rc3.d/S15mdmonitor -> ../init.d/mdmonitor
lrwxrwxrwx. 1 root root 26 Oct 6 23:45 /etc/rc3.d/S25blk-availability -> ../init.d/blk-availability
lrwxrwxrwx. 1 root root 15 Oct 6 23:45 /etc/rc3.d/S25netfs -> ../init.d/netfs
lrwxrwxrwx. 1 root root 19 Oct 6 23:45 /etc/rc3.d/S26udev-post -> ../init.d/udev-post
lrwxrwxrwx. 1 root root 15 Oct 6 23:46 /etc/rc3.d/S50kdump -> ../init.d/kdump
lrwxrwxrwx. 1 root root 14 Oct 6 23:46 /etc/rc3.d/S55sshd -> ../init.d/sshd
lrwxrwxrwx. 1 root root 17 Oct 6 23:45 /etc/rc3.d/S80postfix -> ../init.d/postfix
lrwxrwxrwx 1 root root 15 Nov 2 19:55 /etc/rc3.d/S90crond -> ../init.d/crond
lrwxrwxrwx. 1 root root 11 Oct 6 23:45 /etc/rc3.d/S99local -> ../rc.local
[root@centos6 ~]# ll /etc/rc5.d/*
lrwxrwxrwx. 1 root root 19 Oct 6 23:45 /etc/rc5.d/K10saslauthd -> ../init.d/saslauthd
lrwxrwxrwx. 1 root root 18 Oct 6 23:46 /etc/rc5.d/K61nfs-rdma -> ../init.d/nfs-rdma
lrwxrwxrwx. 1 root root 21 Oct 6 23:45 /etc/rc5.d/K87restorecond -> ../init.d/restorecond
lrwxrwxrwx. 1 root root 20 Oct 6 23:45 /etc/rc5.d/K89netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 15 Oct 6 23:45 /etc/rc5.d/K89rdisc -> ../init.d/rdisc
lrwxrwxrwx. 1 root root 14 Oct 6 23:46 /etc/rc5.d/S05rdma -> ../init.d/rdma
lrwxrwxrwx. 1 root root 19 Oct 6 23:46 /etc/rc5.d/S08ip6tables -> ../init.d/ip6tables
lrwxrwxrwx. 1 root root 18 Oct 6 23:45 /etc/rc5.d/S08iptables -> ../init.d/iptables
lrwxrwxrwx. 1 root root 17 Oct 6 23:45 /etc/rc5.d/S10network -> ../init.d/network
lrwxrwxrwx. 1 root root 16 Oct 6 23:46 /etc/rc5.d/S11auditd -> ../init.d/auditd
lrwxrwxrwx. 1 root root 17 Oct 6 23:45 /etc/rc5.d/S12rsyslog -> ../init.d/rsyslog
lrwxrwxrwx. 1 root root 19 Oct 6 23:45 /etc/rc5.d/S15mdmonitor -> ../init.d/mdmonitor
lrwxrwxrwx. 1 root root 26 Oct 6 23:45 /etc/rc5.d/S25blk-availability -> ../init.d/blk-availability
lrwxrwxrwx. 1 root root 15 Oct 6 23:45 /etc/rc5.d/S25netfs -> ../init.d/netfs
lrwxrwxrwx. 1 root root 19 Oct 6 23:45 /etc/rc5.d/S26udev-post -> ../init.d/udev-post
lrwxrwxrwx. 1 root root 15 Oct 6 23:46 /etc/rc5.d/S50kdump -> ../init.d/kdump
lrwxrwxrwx. 1 root root 14 Oct 6 23:46 /etc/rc5.d/S55sshd -> ../init.d/sshd
lrwxrwxrwx. 1 root root 17 Oct 6 23:45 /etc/rc5.d/S80postfix -> ../init.d/postfix
lrwxrwxrwx 1 root root 15 Nov 2 19:55 /etc/rc5.d/S90crond -> ../init.d/crond
lrwxrwxrwx. 1 root root 11 Oct 6 23:45 /etc/rc5.d/S99local -> ../rc.local
[root@centos6 ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 Oct 6 23:45 /etc/rc.local -> rc.d/rc.local
[root@centos6 ~]# cat /etc/rc.d/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
#/etc/rc.d/rc.local 开机启动文件写到这个文件里
[root@centos6 ~]# vim /etc/init.d/testsrv
#!/bin/bash
#chkconfig: - 96 3
#description: the service script
. /etc/init.d/functions
start(){
touch /var/lock/subsys/testsrv
action "Starting testsrv"
sleep 3
}
stop(){
rm -f /var/lock/subsys/testsrv
action "Shutting down testsrv"
}
restart(){
stop
start
}
status(){
if [ -e /var/lock/subsys/testsrv ];then
echo "testsrv is runing..."
else
echo "testsrv is stopped"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "Usage: /etc/init.d/testsrv {start|stop|restart|status}"
;;
esac
[root@centos6 ~]# chmod +x /etc/init.d/testsrv
[root@centos6 ~]# chkconfig --list
auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
kdump 0:off 1:off 2:off 3:on 4:on 5:on 6:off
mdmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 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
nfs-rdma 0:off 1:off 2:off 3:off 4:off 5:off 6:off
postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off
rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rdma 0:off 1:on 2:on 3:on 4:on 5:on 6:off
restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
saslauthd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off
[root@centos6 ~]# ls /etc/rc*.d/*testsrv
ls: cannot access /etc/rc*.d/*testsrv: No such file or directory
[root@centos6 ~]# service testsrv status
testsrv is stopped
[root@centos6 ~]# service testsrv start
Starting testsrv [ OK ]
[root@centos6 ~]# chkconfig testsrv on
[root@centos6 ~]# chkconfig --list testsrv
testsrv 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@centos6 ~]# ls /etc/rc*.d/*testsrv
/etc/rc2.d/S96testsrv /etc/rc3.d/S96testsrv /etc/rc4.d/S96testsrv /etc/rc5.d/S96testsrv
[root@centos6 ~]# reboot
[root@centos6 ~]# service testsrv status
testsrv is runing...
范例:服务启动失败,故障恢复
[root@centos6 ~]# vim /etc/init.d/testsrv
#!/bin/bash
#chkconfig: - 96 3
#description: the service script
. /etc/init.d/functions
start(){
touch /var/lock/subsys/testsrv
action "Starting testsrv"
sleep 1000 #把等待时间改成1000,模拟服务不能正常启动
}
[root@centos6 ~]# reboot
如果服务卡住,启动不了
按a
进入单用户模式
# chkconfig --list testsrv
# chkconfig testsrv off
# init 3
[root@centos6 ~]# chkconfig --del testsrv #删除开机启动服务
[root@centos6 ~]# chkconfig --list testsrv
service testsrv supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add testsrv')
[root@centos6 ~]# ls /etc/rc*.d/*testsrv
ls: cannot access /etc/rc*.d/*testsrv: No such file or directory
1.3.5.4 非独立服务
服务分为独立服务和非独立服务
瞬态(Transient)服务被超级守护进程 xinetd 进程所管理,也称为非独立服务
进入的请求首先被xinetd代理
配置文件:
/etc/xinetd.conf
/etc/xinetd.d/<service>
用chkconfig控制非独立服务开机启动
示例:chkconfig tftp on
/etc/rc.d/rc.local
注意:正常级别下,最后启动一个服务S99local没有链接至/etc/rc.d/init.d一个服务脚本,而是指向了/etc/rc.d/rc.local脚本
不便或不需写为服务脚本放置于/etc/rc.d/init.d/目录,且又想开机时自动运行的命令,可直接放置于/etc/rc.d/rc.local文件中
/etc/rc.d/rc.local在指定运行级别脚本后运行
范例:
[root@centos6 ~]# yum -y install telnet-server
[root@centos6 ~]# chkconfig --list
auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
kdump 0:off 1:off 2:off 3:on 4:on 5:on 6:off
mdmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 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
nfs-rdma 0:off 1:off 2:off 3:off 4:off 5:off 6:off
postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off
rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rdma 0:off 1:on 2:on 3:on 4:on 5:on 6:off
restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
saslauthd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off
xinetd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
xinetd based services:
chargen-dgram: off
chargen-stream: off
daytime-dgram: off
daytime-stream: off
discard-dgram: off
discard-stream: off
echo-dgram: off
echo-stream: off
tcpmux-server: off
telnet: off
time-dgram: off
time-stream: off
[root@centos6 ~]# service xinetd start
Starting xinetd: [ OK ]
[root@centos6 ~]# service xinetd status
xinetd (pid 1465) is running...
[root@centos6 ~]# chkconfig telnet on
[root@centos6 ~]# chkconfig --list
auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
kdump 0:off 1:off 2:off 3:on 4:on 5:on 6:off
mdmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 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
nfs-rdma 0:off 1:off 2:off 3:off 4:off 5:off 6:off
postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off
rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rdma 0:off 1:on 2:on 3:on 4:on 5:on 6:off
restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
saslauthd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off
xinetd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
xinetd based services:
chargen-dgram: off
chargen-stream: off
daytime-dgram: off
daytime-stream: off
discard-dgram: off
discard-stream: off
echo-dgram: off
echo-stream: off
tcpmux-server: off
telnet: on
time-dgram: off
time-stream: off
[root@centos6 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :::22 :::*
LISTEN 0 128 *:22 *:*
LISTEN 0 64 :::23 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 127.0.0.1:25 *:*
[root@centos6 ~]# ss -ntlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :::22 :::* users:(("sshd",1301,4))
LISTEN 0 128 *:22 *:* users:(("sshd",1301,3))
LISTEN 0 64 :::23 :::* users:(("xinetd",1465,5)) #telnet依赖xinetd启动
LISTEN 0 100 ::1:25 :::* users:(("master",1380,13))
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",1380,12))
[root@centos6 ~]# ps aux |grep telnet
root 1496 0.0 0.0 103320 884 pts/0 S+ 16:46 0:00 grep telnet
#telnet服务没有启动
[root@rocky8 ~]# telnet 172.31.0.6
Trying 172.31.0.6...
Connected to 172.31.0.6.
Escape character is '^]'.
CentOS release 6.10 (Final)
Kernel 2.6.32-754.el6.x86_64 on an x86_64
centos6 login:
[root@centos6 ~]# ps aux |grep telnet
root 1503 0.0 0.1 12528 1024 ? Ss 16:47 0:00 in.telnetd: 172.31.1.8
root 1508 0.0 0.0 103320 884 pts/0 S+ 16:48 0:00 grep telnet
#一但有人访问telnet服务,就自动启动服务
[root@centos6 ~]# lsof -i :23
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
xinetd 1465 root 5u IPv6 10616 0t0 TCP *:telnet (LISTEN)
in.telnet 1503 root 0u IPv4 11192 0t0 TCP 172.31.0.6:telnet->172.31.1.8:44944 (ESTABLISHED)
in.telnet 1503 root 1u IPv4 11192 0t0 TCP 172.31.0.6:telnet->172.31.1.8:44944 (ESTABLISHED)
in.telnet 1503 root 2u IPv4 11192 0t0 TCP 172.31.0.6:telnet->172.31.1.8:44944 (ESTABLISHED)
#断开telnet访问
[root@centos6 ~]# ps aux |grep telnet
root 1515 0.0 0.0 103320 884 pts/0 S+ 16:49 0:00 grep telnet
#telnet服务被关闭
1.3.5.5 开机启动文件 rc.local
/etc/rc.d/rc.local
注意:正常级别下,最后启动一个服务S99local没有链接至/etc/rc.d/init.d一个服务脚本,而是指向了/etc/rc.d/rc.local脚本
不便或不需写为服务脚本放置于/etc/rc.d/init.d/目录,且又想开机时自动运行的命令,可直接放置于/etc/rc.d/rc.local文件中
/etc/rc.d/rc.local在指定运行级别脚本后运行
范例:
[root@rocky8 ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 May 19 10:58 /etc/rc.local -> rc.d/rc.local
[root@centos8 ~]# vim /etc/rc.d/rc.local
ping 127.0.0.1
[root@rocky8 ~]# chmod +x /etc/rc.d/rc.local
[root@rocky8 ~]# reboot
[root@rocky8 ~]# ps aux |grep ping
root 847 0.0 0.3 32448 2428 ? S 17:38 0:00 ping 127.0.0.1
root 1338 0.0 0.1 12136 1068 pts/1 R+ 17:38 0:00 grep --color=auto ping
1.3.6 CentOS 启动过程总结
/sbin/init --> (/etc/inittab) --> 设置默认运行级别 --> 运行系统初始脚本/etc/rc.d/rc.sysinit、完成系统初始化 -->(关闭对应下需要关闭的服务)启动需要启动服务/etc/rc#.d/Sxxxx,/etc/rc.d/rc.local–> 设置登录终端
参看:http://s4.51cto.com/wyfs02/M02/87/20/wKiom1fVBELjXsvaAAUkuL83t2Q304.jpg