Boot Process คือลำดับขั้นตอนที่ Hardware และ Software ทำงานร่วมกันตั้งแต่กดปุ่มเปิดเครื่องจนถึง Linux Kernel พร้อมรับคำสั่ง ความเข้าใจ Boot Process ช่วยให้ Debug ปัญหาที่ Server ไม่สามารถ Boot ได้ กำหนดค่า Boot Options และจัดการ Kernel หลายเวอร์ชันบน Server เดียวกันได้
บทความนี้อธิบาย Boot Process บน Linux ตั้งแต่ BIOS/UEFI, GRUB2 Bootloader, Kernel Initialization, systemd Target จนถึงการตั้งค่า GRUB เพื่อเลือก Default Kernel, แก้ไข Boot Parameters, และ Rescue Mode สำหรับการกู้ System ที่ Boot ไม่ได้
ภาพรวม Linux Boot Process
# Linux Boot Process มี 6 ขั้นตอนหลัก:
#
# 1. POWER ON → BIOS/UEFI
# - POST (Power-On Self Test): ตรวจสอบ Hardware
# - UEFI: อ่าน EFI Partition (/boot/efi) หา Bootloader
# - Legacy BIOS: อ่าน MBR (Master Boot Record) ของ Disk
#
# 2. BOOTLOADER (GRUB2)
# - โหลด GRUB2 จาก /boot/grub2/ หรือ /boot/grub/
# - แสดง Boot Menu (ถ้า timeout > 0)
# - โหลด Kernel Image (vmlinuz) และ initrd/initramfs
#
# 3. KERNEL INITIALIZATION
# - Kernel แตก initramfs ใน tmpfs
# - โหลด Essential Drivers
# - Mount Root Filesystem จริง
# - เรียก /sbin/init (หรือ systemd)
#
# 4. SYSTEMD (PID 1)
# - อ่าน Default Target (multi-user.target หรือ graphical.target)
# - เริ่ม Services ตาม Dependency Graph
# - Mount Filesystems (/etc/fstab)
#
# 5. GETTY / LOGIN
# - เริ่ม getty สำหรับ Console Login
# - หรือเริ่ม SSH daemon สำหรับ Remote Login
#
# 6. USER SESSION
# - Login สำเร็จ → Shell หรือ Desktop Environment
# ดู Boot Log
journalctl -b # Log ตั้งแต่ Boot ครั้งล่าสุด
journalctl -b -1 # Log ของ Boot ครั้งก่อนหน้า
journalctl -b --priority=err # เฉพาะ Error
systemd-analyze # เวลา Boot รวม
systemd-analyze blame # Service ที่ใช้เวลา Boot นานที่สุด
GRUB2 — ทำความเข้าใจโครงสร้าง
# ไฟล์สำคัญของ GRUB2
ls /boot/grub/ # Ubuntu/Debian
ls /boot/grub2/ # RHEL/Rocky/Fedora
# ไฟล์ Config หลัก (ห้ามแก้ไขตรงๆ)
cat /boot/grub/grub.cfg # Generated file — อ่านอย่างเดียว
# ไฟล์ที่แก้ไขได้
cat /etc/default/grub # GRUB Settings
ls /etc/grub.d/ # Scripts ที่ใช้สร้าง grub.cfg
# ดู Kernel ที่มีอยู่
ls /boot/vmlinuz*
ls /boot/initrd*
# ดู GRUB Menu Entries
grep -E "^menuentry|submenu" /boot/grub/grub.cfg | head -20
ตั้งค่า GRUB2 — /etc/default/grub
# /etc/default/grub — ตั้งค่า GRUB2 หลัก
# แก้ไขไฟล์นี้แล้วรัน update-grub หรือ grub2-mkconfig
# ตัวอย่างการตั้งค่าสำหรับ Server:
GRUB_DEFAULT=0 # Boot Kernel ตัวแรก (index เริ่มที่ 0)
GRUB_TIMEOUT=5 # รอ 5 วินาที ก่อน Boot อัตโนมัติ
GRUB_TIMEOUT_STYLE=menu # แสดง Menu เสมอ (แทน countdown)
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_CMDLINE_LINUX_DEFAULT="" # Boot Parameters สำหรับ Default Boot
GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8" # Serial Console
# Boot Parameters ที่มีประโยชน์:
# quiet — ซ่อน Boot messages (Desktop)
# splash — แสดง Boot screen
# nomodeset — ปิด GPU Mode Setting (แก้ปัญหาหน้าจอดำ)
# net.ifnames=0 biosdevname=0 — ใช้ชื่อ eth0/eth1 แทน ens3/enp2s0
# systemd.unit=rescue.target — Boot เข้า Rescue Mode
# หลังแก้ไข ต้อง Regenerate grub.cfg เสมอ
sudo update-grub # Ubuntu/Debian
sudo grub2-mkconfig -o /boot/grub2/grub.cfg # RHEL/Rocky
เลือก Default Kernel
# ดู Kernel ทั้งหมดใน GRUB Menu
sudo awk -F\' '/^menuentry |^submenu /{print NR":", $2}' /boot/grub/grub.cfg
# ตัวอย่าง Output:
# 1: Ubuntu
# 2: Ubuntu, with Linux 6.8.0-52-generic
# 3: Ubuntu, with Linux 6.8.0-52-generic (recovery mode)
# 4: Ubuntu, with Linux 6.8.0-51-generic
# 5: Ubuntu, with Linux 6.8.0-51-generic (recovery mode)
# วิธีที่ 1: ตั้ง Index (0-based ใน /etc/default/grub)
# GRUB_DEFAULT=0 → Boot Kernel ใหม่สุด
# GRUB_DEFAULT=2 → Boot Kernel 6.8.0-51-generic
# วิธีที่ 2: ตั้งด้วยชื่อ (ดีกว่าเพราะไม่เปลี่ยนเมื่อมี Kernel ใหม่)
# GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 6.8.0-51-generic"
# วิธีที่ 3: ใช้ grub-set-default (บน RHEL/Rocky)
sudo grub2-set-default 0
sudo grub2-editenv list # ดูค่าปัจจุบัน
# ใช้ grubby บน RHEL/Rocky
sudo grubby --default-kernel # ดู Default Kernel ปัจจุบัน
sudo grubby --set-default /boot/vmlinuz-5.14.0-427.13.1.el9_4.x86_64
sudo grubby --info=ALL # ดูทุก Kernel Entry
แก้ไข Boot Parameters สำหรับ Kernel เฉพาะ
# เพิ่ม Boot Parameter ให้ Kernel เฉพาะ (RHEL/Rocky)
sudo grubby --args="net.ifnames=0 biosdevname=0" \
--update-kernel=/boot/vmlinuz-$(uname -r)
# ลบ Boot Parameter
sudo grubby --remove-args="rhgb quiet" \
--update-kernel=/boot/vmlinuz-$(uname -r)
# ดู Parameters ของ Kernel ปัจจุบัน
sudo grubby --info=/boot/vmlinuz-$(uname -r)
# บน Ubuntu — แก้ GRUB_CMDLINE_LINUX_DEFAULT ใน /etc/default/grub
# แล้วรัน:
sudo update-grub
# ดู Kernel Parameters ที่กำลังใช้งานอยู่
cat /proc/cmdline
GRUB Rescue Mode — กู้ System ที่ Boot ไม่ได้
เมื่อ GRUB หรือ System มีปัญหาจนไม่สามารถ Boot ได้ตามปกติ GRUB Rescue Mode ช่วยให้เข้าถึง Filesystem และแก้ไขได้โดยตรง
# วิธีเข้า GRUB Rescue:
# - กดค้าง Shift ระหว่าง Boot (Legacy BIOS)
# - กด Esc หลาย ๆ ครั้ง (UEFI)
# - ตั้ง GRUB_TIMEOUT=10 ใน /etc/default/grub ล่วงหน้า
# ใน GRUB Menu กด 'e' เพื่อ Edit Boot Entry ชั่วคราว:
# ค้นหาบรรทัดที่ขึ้นต้นด้วย "linux"
# เพิ่มที่ท้ายบรรทัด: init=/bin/bash
# กด Ctrl+X เพื่อ Boot
# เมื่อได้ Shell:
mount -o remount,rw / # Mount Root แบบ Read-Write
# แก้ไขปัญหา เช่น แก้ /etc/fstab, รีเซ็ต Password
passwd root # เปลี่ยน Root Password
exec /sbin/init # Boot ต่อไป
# Boot เข้า Rescue Target ของ systemd
# เพิ่มในบรรทัด linux ของ GRUB: systemd.unit=rescue.target
# หรือ: rd.break (หยุดก่อน Switch Root ใน initramfs)
Reinstall GRUB — กรณี GRUB เสียหาย
# กรณี GRUB เสีย ต้อง Boot จาก Live CD/USB แล้ว Chroot
# 1. Boot จาก Live Ubuntu/Debian USB
# 2. เปิด Terminal
# ดู Partition
lsblk
fdisk -l
# Mount Root Partition (สมมติ /dev/sda1)
sudo mount /dev/sda1 /mnt
# Mount EFI Partition (ถ้าใช้ UEFI, สมมติ /dev/sda2)
sudo mount /dev/sda2 /mnt/boot/efi
# Bind Mount
sudo mount --bind /dev /mnt/dev
sudo mount --bind /proc /mnt/proc
sudo mount --bind /sys /mnt/sys
sudo mount --bind /run /mnt/run
# Chroot เข้า System
sudo chroot /mnt
# Reinstall GRUB (UEFI)
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=ubuntu
update-grub
# Reinstall GRUB (Legacy BIOS ลง Disk /dev/sda)
grub-install /dev/sda
update-grub
# ออกจาก Chroot
exit
sudo reboot
ดู Boot Performance และ Debug
# วิเคราะห์เวลา Boot
systemd-analyze # เวลา Boot รวม
# Startup finished in 2.845s (kernel) + 8.234s (userspace) = 11.080s
systemd-analyze blame # Service ที่ช้าที่สุด
# 3.827s apt-daily-upgrade.service
# 2.143s NetworkManager-wait-online.service
# 1.234s mysql.service
systemd-analyze critical-chain # Critical Path ของ Boot
systemd-analyze plot > boot.svg # สร้าง Timeline Chart
# ดู Service ที่ล้มเหลวระหว่าง Boot
systemctl --failed
journalctl -b -p err
# ดู Boot Messages ของ Kernel
dmesg | grep -E "error|fail|warn" -i | head -20
dmesg --level=err,warn
dmesg -T | grep "hardware" # ดู Hardware Detection
Serial Console — สำหรับ Cloud VPS ที่ไม่มีหน้าจอ
# บน Cloud VPS ที่เข้าถึงได้ผ่าน Serial Console (VPS Console)
# ต้องตั้งค่า GRUB ให้ส่ง Output ไปที่ Serial Port ด้วย
# แก้ไข /etc/default/grub
GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"
GRUB_TERMINAL="console serial"
GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"
# Apply
sudo update-grub # Ubuntu/Debian
sudo grub2-mkconfig -o /boot/grub2/grub.cfg # RHEL/Rocky
# เปิดใช้งาน Getty บน Serial Port (systemd)
sudo systemctl enable [email protected]
sudo systemctl start [email protected]
# ตรวจสอบ
systemctl status [email protected]
สรุป
Linux Boot Process เริ่มจาก BIOS/UEFI → GRUB2 → Kernel → systemd → Login โดย GRUB2 เป็น Bootloader หลักที่ตั้งค่าผ่าน /etc/default/grub และต้องรัน update-grub หรือ grub2-mkconfig ทุกครั้งหลังแก้ไข การเลือก Default Kernel ทำได้ผ่าน GRUB_DEFAULT หรือ grubby บน RHEL/Rocky เมื่อ System Boot ไม่ได้ ใช้ GRUB Edit Mode เพิ่ม init=/bin/bash หรือ Boot จาก Live USB เพื่อ Chroot แล้ว Reinstall GRUB บน Cloud VPS ควรตั้งค่า Serial Console เพื่อให้เข้าถึง Boot Output ผ่าน VPS Console ได้
แนะนำบริการ DE
การจัดการ GRUB และ Boot Process ต้องการสิทธิ์ Root เต็มรูปแบบ Cloud VPS ของ DE ให้ Root Access พร้อม Serial Console ในตัว ทำให้สามารถ Debug Boot ปัญหาได้แม้ Server จะ Boot ไม่ผ่าน เหมาะสำหรับการทดสอบ Kernel ใหม่และการ Tune Boot Parameters
สำหรับผู้ที่ต้องการโฮสต์เว็บไซต์โดยไม่ต้องจัดการ Boot Process เอง Cloud Hosting ของ DE ดูแล Server Infrastructure ทั้งหมด ให้บริการที่พร้อมใช้งานโดยไม่ต้องกังวลเรื่อง Kernel และ GRUB

