Linux 系統啟動是一場精密的接力賽:韌體 → 開機載入器 → 核心 → init 系統 → 使用者空間服務。理解每個階段的職責、交接介面與配置檔案,是排查啟動故障、實現啟動加固、客製化嵌入式系統的前提。本文以 RHEL 7+/CentOS 7+ (systemd + GRUB 2) 為主線,兼顧傳統 SysVinit 與 GRUB Legacy 差異。
| 階段 | BIOS (Legacy) | UEFI |
|---|---|---|
| 啟動程式碼位置 | MBR (第 0 磁區 446 bytes) | EFI 系統分區 (ESP, FAT32) /EFI/BOOT/BOOTX64.EFI |
| 分區表 | MBR (最多 4 主分區) | GPT (支援 128+ 分區、>2TB) |
| 核心載入 | GRUB Stage 1 → Stage 1.5 → Stage 2 → 核心 | UEFI 直接載入 grubx64.efi → 核心 (可啟用 Secure Boot 驗證簽名) |
| 硬體初始化 | POST、裝置枚舉、中斷路由 | 同樣,但提供 EFI Runtime Services 供核心呼叫 |
/etc/default/grub # 使用者配置變數 (GRUB_TIMEOUT, GRUB_CMDLINE_LINUX...)
/etc/grub.d/ # 模板腳本目錄
00_header → 環境變數、視頻模式
10_linux → 生成 Linux 核心啟動項
30_os-prober → 偵測其他作業系統 (Windows/BSD)
40_custom # 用戶自定義啟動項 (密碼、特殊參數)
41_custom # 讀取 /boot/grub2/custom.cfg
/boot/grub2/grub.cfg # 由 grub2-mkconfig 生成,**不可直接手編**
/boot/grub2/user.cfg # grub2-setpassword 生成的 PBKDF2 密碼雜湊
GRUB_TIMEOUT=5 # 選單等待秒數
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved # 使用上次選擇
GRUB_SAVEDEFAULT=true
GRUB_DISABLE_SUBMENU=y
GRUB_TERMINAL_OUTPUT="console" # serial/console/gfxterm
GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
# BIOS
grub2-mkconfig -o /boot/grub2/grub.cfg
# UEFI
grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg
# 1. 設定管理員帳號與密碼 (PBKDF2 雜湊)
grub2-setpassword
# 輸入兩次密碼,生成 /boot/grub2/user.cfg
# 2. 鎖定特定啟動項 (需編輯 40_custom)
cat >> /etc/grub.d/40_custom <<'EOF'
menuentry "Rescue Mode (Locked)" --unrestricted {
set root=(hd0,1)
linux /vmlinuz-0-rescue ro root=/dev/mapper/rhel-root
initrd /initramfs-0-rescue.img
}
EOF
# 3. 重新生成配置
grub2-mkconfig -o /boot/grub2/grub.cfg
效果:選單顯示需輸入管理員密碼才能編輯/啟動被鎖定項目;未鎖定項目 (如正常啟動) 仍可直接啟動。
vmlinuz 自解壓至記憶體。init (systemd/dracut) 解析 root= 參數 (UUID/LABEL/裝置路徑),載入儲存/檔案系統驅動,掛載真實根分區,switch_root 切換根目錄。exec /sbin/init (systemd)。systemctl get-default # 當前預設目標 (graphical.target / multi-user.target)
systemctl set-default multi-user.target # 設定預設為多用戶文字模式
basic.target
→ sysinit.target (核心初始化、載入模組、掛載 /sys /proc、udev 裝置就緒)
→ local-fs.target (本地檔案系統掛載完成)
→ sysinit.target 完成後並行啟動:
→ multi-user.target (文字模式多用戶)
→ graphical.target (圖形介面,需 display-manager.service)
→ network-online.target (網路就緒)
→ remote-fs.target (網路檔案系統)
systemctl start|stop|restart|reload|enable|disable|status|is-active|is-enabled servicename
systemctl list-units --type=service --state=running
systemctl list-dependencies multi-user.target
journalctl -u servicename -f # 即時日誌
journalctl -b -1 -u servicename # 上一次開機該服務日誌
getty@tty1.service → /sbin/agetty → login → bash (文字終端)。display-manager.service (gdm/sddm/lightdm) → X11/Wayland 會話 → 桌面環境。sshd.service → 遠端 SSH 登入。| 現象 | 診斷切入點 | 關鍵命令 |
|---|---|---|
GRUB 不顯示/報錯 error: no such partition | 分區表損壞、UUID 變更、GRUB 未安裝至正確裝置 | lsblk -f, blkid, grub2-install /dev/sda |
核心恐慌 VFS: Unable to mount root fs | initramfs 缺少儲存/檔案系統驅動、root= 參數錯誤 | dracut -f --regenerate-all, 檢查 grub.cfg root 參數 |
| 緊急模式 | /etc/fstab 掛載失敗、SELinux 標記錯誤、關鍵服務啟動失敗 | journalctl -xb, systemctl status, autorelabel |
| 服務啟動失敗 | 依賴未就緒、配置語法錯誤、權限不足、埠衝突 | systemctl status servicename, journalctl -u servicename -n 50 |
systemd-analyze:systemd-analyze time (總耗時)、systemd-analyze blame (服務耗時排序)、systemd-analyze critical-chain (關鍵路徑)。systemctl disable --now unwanted.service。After=/Wants=/Requires=,避免人為串行化。systemd-readahead (HDD) 或調整 I/O 調度器 (SSD: none/mq-deadline)。掌握從韌體到 systemd 的完整啟動鏈路,配合 GRUB 2 密碼保護、initramfs 再生成、systemd 目標管理與 journald 日誌分析,即可掌控 Linux 系統生命週期的「第一公里」,實現快速故障恢復與安全合規啟動。