引导与服务

168 阅读10分钟

引导

引导流程

开机自检

加电自检所有硬件的运行情况及主板BIOS是否正常

MBR引导

运行放在MBR扇区里的启动 GRUB引导程序

Grub菜单

用于加载内核的程序,通过读取grub配置文件来获取内核和镜像文件的设置和路径位置

  • centos6使用grub
  • centos7使用grub2

grub功能和组成

简述:bootloader引导加载器,引导硬件找到内核

  • windows:ntloader,仅是启动操作系统
  • Linux:grub,grub2,:功能丰富,提供菜单,允许用户选择要启动系统或不同的内核版本;把用户选定的内核装载到内存中的特定空间中,解压、展开,并把系统控制权移交给内核

GRUB 启动阶段

  • 第一阶段:

    1st stage:MBR的前446个字节 引导 硬件去找 内核

    1.5 stage:MBR 之后的扇区,让stage1中的bootloader能识别stage2所在的分区上的文件系统

  • 第二阶段 :2nd stage,分区文件/boot/grub2/grub.cfg

加载Linux内核

  • 内核是操作系统的核心。 把内核和镜像文件系统加载到内存中,使其可以使用

init进程初始化

  • 运行中的程序称为进程。 加载硬件驱动程序,以及初始化进程,内核把init进程加载到内存中运行。 init进程是由内核直接启动的第一个用户级进程,负责启动系统的用户终端(登录界面),启动各种系统服务和守护进程等等。

排除启动类故障

MBR扇区故障

故障原因

  • 病毒、木马的等造成的破坏
  • 不正确的分区操作、磁盘读写误操作等

模拟修复MBR扇区故障

[root@localhost system]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   60G  0 disk 
├─sda1            8:1    0    4G  0 part /boot
└─sda2            8:2    0   44G  0 part 
  ├─centos-root 253:0    0   40G  0 lvm  /
  └─centos-swap 253:1    0    4G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk 
└─sdb1            8:17   0   20G  0 part 
sdc               8:32   0   20G  0 disk 
sdd               8:48   0   20G  0 disk 
sr0              11:0    1  4.2G  0 rom  
# 创建新的分区
[root@localhost system]# hexdump -C -n 512 /dev/sda
00000000  eb 63 90 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |.c..............|
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 01 8b  |.........|...t..|
00000040  4c 02 cd 13 ea 00 7c 00  00 eb fe 00 00 00 00 00  |L.....|.........|
00000050  00 00 00 00 00 00 00 00  00 00 00 80 01 00 00 00  |................|
00000060  00 00 00 00 ff fa 90 90  f6 c2 80 74 05 f6 c2 70  |...........t...p|
00000070  74 02 b2 80 ea 79 7c 00  00 31 c0 8e d8 8e d0 bc  |t....y|..1......|
00000080  00 20 fb a0 64 7c 3c ff  74 02 88 c2 52 be 05 7c  |. ..d|<.t...R..||
00000090  b4 41 bb aa 55 cd 13 5a  52 72 3d 81 fb 55 aa 75  |.A..U..ZRr=..U.u|
000000a0  37 83 e1 01 74 32 31 c0  89 44 04 40 88 44 ff 89  |7...t21..D.@.D..|
000000b0  44 02 c7 04 10 00 66 8b  1e 5c 7c 66 89 5c 08 66  |D.....f..|f..f|
000000c0  8b 1e 60 7c 66 89 5c 0c  c7 44 06 00 70 b4 42 cd  |..`|f...D..p.B.|
000000d0  13 72 05 bb 00 70 eb 76  b4 08 cd 13 73 0d 5a 84  |.r...p.v....s.Z.|
000000e0  d2 0f 83 de 00 be 85 7d  e9 82 00 66 0f b6 c6 88  |.......}...f....|
000000f0  64 ff 40 66 89 44 04 0f  b6 d1 c1 e2 02 88 e8 88  |d.@f.D..........|
00000100  f4 40 89 44 08 0f b6 c2  c0 e8 02 66 89 04 66 a1  |.@.D.......f..f.|
00000110  60 7c 66 09 c0 75 4e 66  a1 5c 7c 66 31 d2 66 f7  |`|f..uNf.|f1.f.|
00000120  34 88 d1 31 d2 66 f7 74  04 3b 44 08 7d 37 fe c1  |4..1.f.t.;D.}7..|
00000130  88 c5 30 c0 c1 e8 02 08  c1 88 d0 5a 88 c6 bb 00  |..0........Z....|
00000140  70 8e c3 31 db b8 01 02  cd 13 72 1e 8c c3 60 1e  |p..1......r...`.|
00000150  b9 00 01 8e db 31 f6 bf  00 80 8e c6 fc f3 a5 1f  |.....1..........|
00000160  61 ff 26 5a 7c be 80 7d  eb 03 be 8f 7d e8 34 00  |a.&Z|..}....}.4.|
00000170  be 94 7d e8 2e 00 cd 18  eb fe 47 52 55 42 20 00  |..}.......GRUB .|
00000180  47 65 6f 6d 00 48 61 72  64 20 44 69 73 6b 00 52  |Geom.Hard Disk.R|
00000190  65 61 64 00 20 45 72 72  6f 72 0d 0a 00 bb 01 00  |ead. Error......|
000001a0  b4 0e cd 10 ac 3c 00 75  f4 c3 00 00 00 00 00 00  |.....<.u........|
000001b0  00 00 00 00 00 00 00 00  c2 67 00 00 00 00 80 20  |.........g..... |
000001c0  21 00 83 4b 81 0a 00 08  00 00 00 00 80 00 00 4b  |!..K...........K|
000001d0  82 0a 8e fe ff ff 00 08  80 00 00 40 80 05 00 00  |...........@....|
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
# MBR破坏前[root@localhost /]# mount /dev/sdb1 /mnt
# 将sdb1挂载到mnt文件夹下[root@localhost mnt]# dd if=/dev/sda of=/mnt/bak/mbr.bak bs=512 count=1
记录了1+0 的读入
记录了1+0 的写出
512字节(512 B)已复制,0.000212631 秒,2.4 MB/秒
# 将mbr扇区备份到sdb1分区[root@localhost bak]# dd if=/dev/zero of=/dev/sda bs=512 count=1
记录了1+0 的读入
记录了1+0 的写出
512字节(512 B)已复制,0.000242439 秒,2.1 MB/秒
# 破坏sda的mbr扇区[root@localhost bak]# hexdump -C -n 512 /dev/sda
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000200
#观察破坏后的mbr扇区
​
​

微信截图_20240414201831.png

微信截图_20240414201857.png

微信截图_20240414202132.png

微信截图_20240414202758.png

微信截图_20240414202825.png

grub引导故障

故障原因

  • MBR中的GRUB引导程序遭到破坏
  • grub.cfg 文件丢失、引导配置有误

模拟修复grub配置文件

[root@localhost ~]# cd /boot/grub2
[root@localhost grub2]# rm -rf grub.cfg
[root@localhost grub2]# ls
device.map  fonts  grubenv  i386-pc  locale
[root@localhost grub2]# reboot

丢失grub配置文件,无法登录

微信截图_20240414204010.png

重新启动,在读条界面按 Esc 键进入启动菜单,改用光盘启动,引导界面进入急救模式

微信截图_20240414204116.png

微信截图_20240414201831.png

微信截图_20240414201857.png

微信截图_20240414202132.png

微信截图_20240414205018.png

reboot重启

修复完成

linux运行级别

运行级别(runlevel)指的是系统的工作状态,定义了系统启动时运行的服务和进程. init 0-6 可以切换到 对应的运行级别

[root@localhost ~]# systemctl get-default  ##获得当前的运行级别
​
[root@localhost ~]# systemctl isolate xxx.target  ##不重启切换运行级别
  • 运行级别0(halt)

    • 系统关机状态,所有服务已停止,可以安全地关闭电源。
  • 运行级别1(single user mode)

    • 单用户模式,只有root用户可以登录,用于系统修复和维护。
  • 运行级别2(multi-user mode)

    • 文本模式多用户模式,登录后进入命令行界面, 功能不全的字符界面。
  • 运行级别3(full multi-user mode)

    • 文本模式多用户模式,登录后进入命令行界面, 完整的字符界面。
  • 运行级别4(unused)

    • 保留,未分配特定用途,可以根据需要进行自定义。
  • 运行级别5(graphical multi-user mode)

    • 图形多用户模式,登录后进入 图形界面。
  • 运行级别6

    • 重启

服务

什么是服务程序

为其他机器提供服务的程序,服务程序启动后,会持续监听端口。

使用systemctl 命令管理systemd服务。

什么程序能被systemd程序管理

  1. 系统必要的程序
  2. yum和rpm安装的程序
  3. 编译安装的程序,需要手写配置文件后才可以

服务程序的保存路径:/usr/lib/systemd/system/服务名.service的文件

[root@localhost system]# find /usr/lib/systemd/system "*.service"
/usr/lib/systemd/system
/usr/lib/systemd/system/printer.target
/usr/lib/systemd/system/teamd@.service
/usr/lib/systemd/system/rc-local.service
/usr/lib/systemd/system/fstrim.service
/usr/lib/systemd/system/quotaon.service
/usr/lib/systemd/system/fstrim.timer
/usr/lib/systemd/system/initrd-parse-etc.service
/usr/lib/systemd/system/netcf-transaction.service
/usr/lib/systemd/system/initrd-root-fs.target
/usr/lib/systemd/system/blk-availability.service
/usr/lib/systemd/system/initrd.target.wants
/usr/lib/systemd/system/initrd.target.wants/dracut-cmdline.service
/usr/lib/systemd/system/initrd.target.wants/dracut-initqueue.service
/usr/lib/systemd/system/initrd.target.wants/dracut-mount.service
/usr/lib/systemd/system/initrd.target.wants/dracut-pre-mount.service
/usr/lib/systemd/system/initrd.target.wants/dracut-pre-pivot.service
/usr/lib/systemd/system/initrd.target.wants/dracut-pre-trigger.service
/usr/lib/systemd/system/initrd.target.wants/dracut-pre-udev.service
/usr/lib/systemd/system/shutdown.target.wants
/usr/lib/systemd/system/shutdown.target.wants/dracut-shutdown.service
/usr/lib/systemd/system/initrd-switch-root.service
/usr/lib/systemd/system/dracut-cmdline.service
...

yum和rpm会自动生成service的文件,此类文件本身可以被systemd管理

编译安装需要手写“服务名.service"文件来被systemd管理

systemd服务

1.对比5 6 可以解决依赖关系并行启动

2.按需启动

3.自动解决依赖关系

负责在系统启动或运行时,激活系统资源,服务器进程和其它进程

systemctl命令

systemctl是用于管理systemd系统和服务管理器的命令行工具。

命令格式

systemctl [选项] 程序名

start开启
stop关闭
status查看程序状态
reload重新加载配置文件而不重启
restart重启
enable开机自启
disable开机不自启
systemctl enable --now 程序名开机自启并立即启动
systemctl start nginx ##启动nginx
​
systemctl status nginx ##查看nginx运行状态
​
systemctl stop firewalld ##关闭防火墙
​
setenforce 0  ##永久关闭

Tips: systemctl命令后面最好跟选项,可以批量操作程序开启或关闭,如果将程序名放在命令后面,只能单一操作某个程序开启或关闭。

systemd单元类型(unit)

systemd 核心概念:unit

unit表示不同类型的systemd对象,通过配置文件进行标识和配置;文件中主要包含了系统服务、监听

socket、保存的系统快照以及其它与init相关的信息

# systemd单元文件最初默认存放的位置
[root@localhost ~]# cd /etc/systemd/system/
[root@localhost system]# ls
basic.target.wants                                       getty.target.wants
bluetooth.target.wants                                   graphical.target.wants
dbus-org.bluez.service                                   multi-user.target.wants
dbus-org.fedoraproject.FirewallD1.service                printer.target.wants
dbus-org.freedesktop.Avahi.service                       remote-fs.target.wants
dbus-org.freedesktop.ModemManager1.service               sockets.target.wants
dbus-org.freedesktop.NetworkManager.service              spice-vdagentd.target.wants
dbus-org.freedesktop.nm-dispatcher.service               sssd.service.d
default.target                                           sysinit.target.wants
default.target.wants                                     system-update.target.wants
dev-virtio\x2dports-org.qemu.guest_agent.0.device.wants  vmtoolsd.service.requires
display-manager.service
​
# systemctl命令支持的单元类型
[root@localhost system]# systemctl -t help
Available unit types:
service
socket
busname
target
snapshot
device
mount
automount
swap
timer
path
slice
scope