1科贬、簡(jiǎn)述linux操作系統(tǒng)啟動(dòng)流程
Centos6 啟動(dòng)流程
- POST:通電自檢
- 加載BIOS的硬件信息,獲取第一個(gè)啟動(dòng)設(shè)備
- 讀取第一個(gè)啟動(dòng)設(shè)備MBR的引導(dǎo)加載程序(grub)的啟動(dòng)信息
- 加載內(nèi)核 Kernel佣盒,核心開(kāi)始解壓縮肺樟,并嘗試驅(qū)動(dòng)所有的硬件設(shè)備
- 核心執(zhí)行init程序檐春,并獲取默認(rèn)的運(yùn)行信息
- init程序執(zhí)行/etc/rc.d/rc.sysinit文件,重新掛載根文件系統(tǒng)
- 啟動(dòng)核心的外掛模塊
- init執(zhí)行運(yùn)行的各個(gè)批處理文件(scripts)
- init執(zhí)行/etc/rc.d/rc.local
- 執(zhí)行/bin/login程序么伯,等待用戶登錄
- 登錄之后開(kāi)始以Shell控制主機(jī)
CentOS 7之后版本啟動(dòng)流程
- UEFi或BIOS初始化疟暖,運(yùn)行POST開(kāi)機(jī)自檢
- 選擇啟動(dòng)設(shè)備
- 引導(dǎo)裝載程序, centos7是grub2,加載裝載程序的配置文件: /etc/grub.d/ /etc/default/grub /boot/grub2/grub.cfg
- 加載initramfs驅(qū)動(dòng)模塊
- 加載內(nèi)核選項(xiàng)
- 內(nèi)核初始化,centos7使用systemd代替init
- 執(zhí)行initrd.target所有單元田柔,包括掛載/etc/fstab
- 從initramfs根文件系統(tǒng)切換到磁盤根目錄
- systemd執(zhí)行默認(rèn)target配置俐巴,配置文件/etc/systemd/system/default.target
- systemd執(zhí)行sysinit.target初始化系統(tǒng)及basic.target準(zhǔn)備操作系統(tǒng)
- ystemd啟動(dòng)multi-user.target下的本機(jī)與服務(wù)器服務(wù)
- systemd執(zhí)行multi-user.target下的/etc/rc.d/rc.local
- Systemd執(zhí)行multi-user.target下的getty.target及登錄服務(wù)
- systemd執(zhí)行g(shù)raphical需要的服務(wù)
2、制作一個(gè)只運(yùn)行shell的linux系統(tǒng)
#centos 6 添加一塊20G 硬盤開(kāi)始自制linux 系統(tǒng)
#掃描硬盤
[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
#創(chuàng)建分區(qū)
[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
#拷貝內(nèi)核和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
#準(zhǔn)備根下面相關(guān)程序和庫(kù)
[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}
#復(fù)制bash等命令和相關(guān)庫(kù)文件硬爆,如:
bash,ifconfig,insmod,ping,mount,ls,cat,df,lsblk,blkid,tree,fdisk
#拷貝網(wǎng)卡驅(qū)動(dòng)
[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欣舵、總結(jié)systemctl管理命令及service unit文件格式
命令:
systemctl COMMAND name.service
#啟動(dòng):相當(dāng)于service name start
systemctl start name.service
#停止:相當(dāng)于service name stop
systemctl stop name.service
#重啟:相當(dāng)于service name restart
systemctl restart name.service
#查看狀態(tài):相當(dāng)于service name status
systemctl status name.service
#禁止自動(dòng)和手動(dòng)啟動(dòng):
systemctl mask name.service
#取消禁止
systemctl unmask name.service
#查看某服務(wù)當(dāng)前激活與否的狀態(tài):
systemctl is-active name.service
#查看所有已經(jīng)激活的服務(wù):
systemctl list-units --type|-t service
#查看所有服務(wù):
systemctl list-units --type service --all|-a
#設(shè)定某服務(wù)開(kāi)機(jī)自啟,相當(dāng)于chkconfig name on
systemctl enable name.service
#設(shè)定某服務(wù)開(kāi)機(jī)禁止啟動(dòng):相當(dāng)于chkconfig name off
systemctl disable name.service
#查看所有服務(wù)的開(kāi)機(jī)自啟狀態(tài)摆屯,相當(dāng)于chkconfig --list
systemctl list-unit-files --type service
#用來(lái)列出該服務(wù)在哪些運(yùn)行級(jí)別下啟用和禁用:chkconfig –list name
ls /etc/systemd/system/*.wants/name.service
#查看服務(wù)是否開(kāi)機(jī)自啟:
systemctl is-enabled name.service
#列出失敗的服務(wù)
systemctl --failed --type=service
#開(kāi)機(jī)并立即啟動(dòng)或停止
systemctl enable --now postfix
systemctl disable --now postfix
#查看服務(wù)的依賴關(guān)系:
systemctl list-dependencies name.service
#殺掉進(jìn)程:
systemctl kill unitname
service unit文件格式
/etc/systemd/system:系統(tǒng)管理員和用戶使用
/usr/lib/systemd/system:發(fā)行版打包者使用
幫助參考:
systemd.directives(7)邻遏,systemd.unit(5),systemd.service(5), systemd.socket(5), systemd.target(5),systemd.exec(5)
unit 格式說(shuō)明:
以 “#” 開(kāi)頭的行后面的內(nèi)容會(huì)被認(rèn)為是注釋 相關(guān)布爾值,1虐骑、yes准验、on、true 都是開(kāi)啟廷没,0糊饱、no、off颠黎、false 都是關(guān)閉
時(shí)間單位默認(rèn)是秒另锋,所以要用毫秒(ms)分鐘(m)等須顯式說(shuō)明
service unit file文件通常由三部分組成:
- [Unit]:定義與Unit類型無(wú)關(guān)的通用選項(xiàng)滞项;用于提供unit的描述信息、unit行為及依賴關(guān)系等
- [Service]:與特定類型相關(guān)的專用選項(xiàng)夭坪;此處為Service類型
- [Install]:定義由“systemctl enable”以及"systemctl disable“命令在實(shí)現(xiàn)服務(wù)啟用或禁用時(shí)用到 的一些選項(xiàng)
Unit段的常用選項(xiàng):
- Description:描述信息
- After:定義unit的啟動(dòng)次序文判,表示當(dāng)前unit應(yīng)該晚于哪些unit啟動(dòng),其功能與Before相反
- Requires:依賴到的其它units室梅,強(qiáng)依賴戏仓,被依賴的units無(wú)法激活時(shí),當(dāng)前unit也無(wú)法激活
- Wants:依賴到的其它units亡鼠,弱依賴
- Conflicts:定義units間的沖突關(guān)系
[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 密碼
第一種方法:
啟動(dòng)時(shí)任意鍵暫停啟動(dòng)
按e鍵進(jìn)入編輯模式
將光標(biāo)移動(dòng)linux 開(kāi)始的行,添加內(nèi)核參數(shù)rd.break
按ctrl-x啟動(dòng)
mount –o remount,rw /sysroot
chroot /sysroot
passwd root
#如果SELinux是啟用的,才需要執(zhí)行下面操作,如查沒(méi)有啟動(dòng),不需要執(zhí)行
touch /.autorelabel
exit
reboot