Linux 系统启动过程
从上电到登录提示符之间发生了什么:BIOS/UEFI、GRUB、内核、initramfs、systemd。
Key Points上电 → UEFI → GRUB → 内核 + initramfs → 挂真实根文件系统 → systemd 拉起服务 → 登录。
六个阶段
Text
1. 固件(BIOS / UEFI) 自检,找可启动设备
2. 引导程序(GRUB2) 选内核,加载内核与 initramfs
3. 内核初始化 驱动、挂载 initramfs 为临时根
4. initramfs 加载磁盘/RAID 驱动,挂载真实根
5. systemd(PID 1) 按 target 拉起服务
6. 登录(getty / sshd) 出现登录界面各阶段能查什么
| Stage | Check | Typical failure |
|---|---|---|
| Firmware | Boot menu, efibootmgr | Wrong boot order |
| GRUB | grub2-mkconfig, /boot/grub2/grub.cfg | Kernel missing after upgrade |
| Kernel | dmesg, journalctl -k | Missing driver, panic |
| initramfs | lsinitrd / lsinitramfs | Root not found |
| systemd | systemctl list-units --failed | Service failed to start |
启动耗时分析
Bash
systemd-analyze
systemd-analyze blame | head -10
systemd-analyze critical-chain
journalctl -b -p err # 本次启动的错误日志常用的两种修复
Bash
# 1. GRUB 菜单不出现:开机按住 Shift(BIOS)或 Esc(UEFI)
# 2. 进入 rescue 模式改配置:启动时在 GRUB 里给内核加参数
# systemd.unit=rescue.target
# rd.break (RHEL 系改 root 密码)When a server fails to come back, read
journalctl -b -1 -p err (errors from the previous boot) before guessing.六个阶段
Text
1. 固件(BIOS / UEFI) 自检,找可启动设备
2. 引导程序(GRUB2) 选内核,加载内核与 initramfs
3. 内核初始化 驱动、挂载 initramfs 为临时根
4. initramfs 加载磁盘/RAID 驱动,挂载真实根
5. systemd(PID 1) 按 target 拉起服务
6. 登录(getty / sshd) 出现登录界面各阶段能查什么
| 阶段 | 查看方式 | 典型故障 |
|---|---|---|
| 固件 | 启动菜单、efibootmgr | 启动顺序不对 |
| GRUB | grub2-mkconfig | 升级后内核丢失 |
| 内核 | dmesg、journalctl -k | 缺驱动、panic |
| initramfs | lsinitrd | 找不到根分区 |
| systemd | systemctl --failed | 服务起不来 |
启动耗时分析
Bash
systemd-analyze
systemd-analyze blame | head -10
systemd-analyze critical-chain
journalctl -b -p err # 本次启动的错误日志常用的两种修复
Bash
# 1. GRUB 菜单不出现:开机按住 Shift(BIOS)或 Esc(UEFI)
# 2. 进入 rescue 模式改配置:启动时在 GRUB 里给内核加参数
# systemd.unit=rescue.target
# rd.break (RHEL 系改 root 密码)服务器重启后起不来,先看
journalctl -b -1 -p err(上一次启动的错误),比盲猜快得多。