1、简述linux操作系统启动流程
Centos6 启动流程
- POST:通电自检
- 加载BIOS的硬件信息,获取第一个启动设备
- 读取第一个启动设备MBR的引导加载程序(grub)的启动信息
- 加载内核 Kernel,核心开始解压缩,并尝试驱动所有的硬件设备
- 核心执行init程序,并获取默认的运行信息
- init程序执行/etc/rc.d/rc.sysinit文件,重新挂载根文件系统
- 启动核心的外挂模块
- init执行运行的各个批处理文件(scripts)
- init执行/etc/rc.d/rc.local
- 执行/bin/login程序,等待用户登录
- 登录之后开始以Shell控制主机
CentOS 7之后版本启动流程
- UEFi或BIOS初始化,运行POST开机自检
- 选择启动设备
- 引导装载程序, centos7是grub2,加载装载程序的配置文件: /etc/grub.d/ /etc/default/grub /boot/grub2/grub.cfg
- 加载initramfs驱动模块
- 加载内核选项
- 内核初始化,centos7使用systemd代替init
- 执行initrd.target所有单元,包括挂载/etc/fstab
- 从initramfs根文件系统切换到磁盘根目录
- systemd执行默认target配置,配置文件/etc/systemd/system/default.target
- systemd执行sysinit.target初始化系统及basic.target准备操作系统
- ystemd启动multi-user.target下的本机与服务器服务
- systemd执行multi-user.target下的/etc/rc.d/rc.local
- Systemd执行multi-user.target下的getty.target及登录服务
- systemd执行graphical需要的服务
2、制作一个只运行shell的linux系统
#centos 6 添加一块20G 硬盘开始自制linux 系统
#扫描硬盘
[root@Centos6 ~]# echo '- - -' > /sys/class/scsi_host/host0/scan
[root@Centos6 ~]# echo '- - -' > /sys/class/scsi_host/host1/scan
[root@Centos6 ~]# echo '- - -' > /sys/class/scsi_host/host2/scan
[root@Centos6 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.7G 0 rom /mnt
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 97.7G 0 part /
├─sda3 8:3 0 48.8G 0 part /data
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 3.4G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
#创建分区
[root@Centos6 ~]# fdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1G
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@Centos6 ~]# fdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (133-2610, default 133):
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-2610, default 2610):
Using default value 2610
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@Centos6 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.7G 0 rom /mnt
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 97.7G 0 part /
├─sda3 8:3 0 48.8G 0 part /data
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 3.4G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 1G 0 part
└─sdb2 8:18 0 19G 0 part
#挂载boot
[root@Centos6 ~]# mkdir /mnt/boot
[root@Centos6 ~]# mount /dev/sdb1 /mnt/boot
#安装grub
[root@Centos6 ~]# grub-install --root-directory=/mnt/ /dev/sdb
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /mnt//boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.
(fd0) /dev/fd0
(hd0) /dev/sda
(hd1) /dev/sdb
[root@Centos6 ~]# ls /mnt/boot
grub lost+found
[root@Centos6 ~]# ls /mnt/boot/grub
device.map ffs_stage1_5 minix_stage1_5 stage2 xfs_stage1_5
e2fs_stage1_5 iso9660_stage1_5 reiserfs_stage1_5 ufs2_stage1_5
fat_stage1_5 jfs_stage1_5 stage1 vstafs_stage1_5
[root@Centos6 ~]# hexdump -C /dev/sdb -n 512
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 db 61 8f 27 00 00 00 01 |.........a.'....|
000001c0 01 00 83 fe 3f 83 3f 00 00 00 45 5b 20 00 00 00 |....?.?...E[ ...|
000001d0 01 84 83 fe ff ff 84 5b 20 00 2e 70 5f 02 00 00 |.......[ ..p_...|
000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
00000200
#拷贝内核和initramfs文件
[root@Centos6 ~]# cp /boot/vmlinuz /mnt/boot/
[root@Centos6 ~]# cp /boot/initramfs.img /mnt/boot
[root@Centos6 ~]# ls /mnt/boot
grub initramfs.img lost+found vmlinuz
#建立grub.conf
[root@Centos6 ~]# vim /mnt/boot/grub/grub.conf
[root@Centos6 ~]# cat /mnt/boot/grub/grub.conf
default=0
timeout=8
title=Stella S
root (hd0,0)
kernel /vmlinuz root=/dev/sda2 selinux=0 init=/bin/bash
initrd /initramfs.img
#准备根下面相关程序和库
[root@Centos6 ~]# mkdir /mnt/sysroot
[root@Centos6 ~]# mount /dev/sdb2 /mnt/sysroot
[root@Centos6 ~]# mkdir –pv /mnt/sysroot/{boot,dev,sys,proc,etc,lib,lib64,bin,sbin,tmpvar,usr,opt,home,root}
#复制bash等命令和相关库文件,如:
bash,ifconfig,insmod,ping,mount,ls,cat,df,lsblk,blkid,tree,fdisk
#拷贝网卡驱动
[root@Centos6 sysroot]# ethtool -i eth0
driver: e1000
version: 7.3.21-k8-NAPI
firmware-version:
bus-info: 0000:02:01.0
supports-statistics: yes
supports-test: yes
supports-eeprom-access: yes
supports-register-dump: yes
supports-priv-flags: no
[root@Centos6 sysroot]# modinfo -n e1000
/lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000/e1000.ko
[root@Centos6 sysroot]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 100660656 3665640 91875016 4% /
tmpfs 1018628 0 1018628 0% /dev/shm
/dev/sda1 999320 31328 915564 4% /boot
/dev/sda3 50264772 53372 47651400 1% /data
/dev/sdb1 1010780 31020 926748 4% /mnt/boot
/dev/sdb2 19460816 48176 18417416 1% /mnt/sysroot
[root@Centos6 sysroot]# cp /lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib/
[root@centos6 ~]#chroot /mnt/sysroot
3、总结systemctl管理命令及service unit文件格式
命令:
systemctl COMMAND name.service
#启动:相当于service name start
systemctl start name.service
#停止:相当于service name stop
systemctl stop name.service
#重启:相当于service name restart
systemctl restart name.service
#查看状态:相当于service name status
systemctl status name.service
#禁止自动和手动启动:
systemctl mask name.service
#取消禁止
systemctl unmask name.service
#查看某服务当前激活与否的状态:
systemctl is-active name.service
#查看所有已经激活的服务:
systemctl list-units --type|-t service
#查看所有服务:
systemctl list-units --type service --all|-a
#设定某服务开机自启,相当于chkconfig name on
systemctl enable name.service
#设定某服务开机禁止启动:相当于chkconfig name off
systemctl disable name.service
#查看所有服务的开机自启状态,相当于chkconfig --list
systemctl list-unit-files --type service
#用来列出该服务在哪些运行级别下启用和禁用:chkconfig –list name
ls /etc/systemd/system/*.wants/name.service
#查看服务是否开机自启:
systemctl is-enabled name.service
#列出失败的服务
systemctl --failed --type=service
#开机并立即启动或停止
systemctl enable --now postfix
systemctl disable --now postfix
#查看服务的依赖关系:
systemctl list-dependencies name.service
#杀掉进程:
systemctl kill unitname
service unit文件格式
/etc/systemd/system:系统管理员和用户使用
/usr/lib/systemd/system:发行版打包者使用
帮助参考:
systemd.directives(7),systemd.unit(5),systemd.service(5), systemd.socket(5), systemd.target(5),systemd.exec(5)
unit 格式说明:
以 “#” 开头的行后面的内容会被认为是注释 相关布尔值,1、yes、on、true 都是开启,0、no、off、false 都是关闭
时间单位默认是秒,所以要用毫秒(ms)分钟(m)等须显式说明
service unit file文件通常由三部分组成:
- [Unit]:定义与Unit类型无关的通用选项;用于提供unit的描述信息、unit行为及依赖关系等
- [Service]:与特定类型相关的专用选项;此处为Service类型
- [Install]:定义由“systemctl enable”以及"systemctl disable“命令在实现服务启用或禁用时用到 的一些选项
Unit段的常用选项:
- Description:描述信息
- After:定义unit的启动次序,表示当前unit应该晚于哪些unit启动,其功能与Before相反
- Requires:依赖到的其它units,强依赖,被依赖的units无法激活时,当前unit也无法激活
- Wants:依赖到的其它units,弱依赖
- Conflicts:定义units间的冲突关系
[root@Centos-8-p01_v7 system]$ cat /usr/lib/systemd/system/postfix.service
[Unit]
Description=Postfix Mail Transport Agent
After=syslog.target network.target
Conflicts=sendmail.service exim.service
[Service]
Type=forking
PIDFile=/var/spool/postfix/pid/master.pid
EnvironmentFile=-/etc/sysconfig/network
PrivateTmp=true
CapabilityBoundingSet=~ CAP_NET_ADMIN CAP_SYS_ADMIN CAP_SYS_BOOT CAP_SYS_MODULE
ProtectSystem=true
PrivateDevices=true
ExecStartPre=-/usr/libexec/postfix/aliasesdb
ExecStartPre=-/usr/libexec/postfix/chroot-update
ExecStart=/usr/sbin/postfix start
ExecReload=/usr/sbin/postfix reload
ExecStop=/usr/sbin/postfix stop
[Install]
WantedBy=multi-user.target
4、破解centos7 密码
第一种方法:
启动时任意键暂停启动
按e键进入编辑模式
将光标移动linux 开始的行,添加内核参数rd.break
按ctrl-x启动
mount –o remount,rw /sysroot
chroot /sysroot
passwd root
#如果SELinux是启用的,才需要执行下面操作,如查没有启动,不需要执行
touch /.autorelabel
exit
reboot