Linux Kernel เป็นส่วนหลักของระบบปฏิบัติการที่ทำหน้าที่จัดการ Hardware, Memory, Process และ System Call การอัพเดต Kernel สม่ำเสมอช่วยให้ได้รับ Security Patches, Bug Fixes และ Driver Support ใหม่ ขณะเดียวกันการจัดการ Kernel หลายเวอร์ชันช่วยให้สามารถ Rollback ได้หาก Kernel ใหม่มีปัญหา
บทความนี้ครอบคลุมการดู Kernel Version ที่ใช้งาน, วิธีอัพเดต Kernel ผ่าน Package Manager บน Ubuntu และ RHEL/Rocky, การตั้งค่า Automatic Security Updates, การจัดการ Kernel Parameters ด้วย sysctl, การ Compile Kernel จาก Source สำหรับกรณีพิเศษ และการ Rollback เมื่อ Kernel ใหม่มีปัญหา
ตรวจสอบ Kernel Version ปัจจุบัน
# ดู Kernel Version ที่รันอยู่
uname -r # เช่น 6.8.0-51-generic
uname -a # ละเอียดกว่า: kernel, hostname, date, arch
# ดู Kernel ที่ติดตั้งทั้งหมด
# Ubuntu/Debian
dpkg --list | grep linux-image
# RHEL/Rocky/CentOS
rpm -q kernel
# ดู Default Kernel ที่จะ Boot
# Ubuntu (GRUB)
grep "set default" /boot/grub/grub.cfg
# RHEL/Rocky
grubby --default-kernel
# ดูข้อมูล Kernel ละเอียด
cat /proc/version
cat /proc/sys/kernel/osrelease
hostnamectl | grep Kernel
อัพเดต Kernel บน Ubuntu/Debian
Ubuntu จัดการ Kernel ผ่าน Package Manager เหมือน Package ทั่วไป การอัพเดตทำได้ง่ายด้วย apt
# อัพเดต Package List และ อัพเดต Kernel (พร้อม Package อื่น)
sudo apt update
sudo apt upgrade -y
# อัพเดตเฉพาะ Kernel
sudo apt upgrade linux-image-generic linux-headers-generic
# ดู Kernel Version ที่มีใน Repository
apt-cache search linux-image | grep generic
# ติดตั้ง Kernel เวอร์ชันเฉพาะ
sudo apt install linux-image-6.8.0-52-generic linux-headers-6.8.0-52-generic
# ดู Kernel ที่ติดตั้งทั้งหมดหลังอัพเดต
dpkg --list | grep linux-image | grep "^ii"
# Reboot เพื่อใช้ Kernel ใหม่
sudo reboot
# ลบ Kernel เก่าที่ไม่ใช้แล้ว (Ubuntu)
# วิธีที่ 1: autoremove อัตโนมัติ
sudo apt autoremove --purge
# วิธีที่ 2: ลบด้วยตนเอง (ระวัง! ห้ามลบ Kernel ที่รันอยู่)
# ดู Kernel ที่รันอยู่ก่อน
uname -r
# ลบ Kernel ที่ไม่ใช้
sudo apt purge linux-image-6.8.0-49-generic linux-headers-6.8.0-49-generic
# อัพเดต GRUB หลังลบ
sudo update-grub
อัพเดต Kernel บน RHEL/Rocky/CentOS
# อัพเดต Kernel ผ่าน dnf/yum
sudo dnf update kernel -y # Rocky 8/9, RHEL 8/9
sudo yum update kernel -y # CentOS 7
# ดู Kernel ที่ติดตั้ง
rpm -q kernel
rpm -q kernel --queryformat '%{VERSION}-%{RELEASE}.%{ARCH}\n'
# ดูว่า Kernel ใหม่มีใน Repository ไหม
sudo dnf check-update kernel
# ติดตั้ง Kernel เวอร์ชันเฉพาะ
sudo dnf install kernel-5.14.0-427.el9.x86_64
# จำกัดจำนวน Kernel ที่เก็บไว้ (default = 3)
# แก้ไขใน /etc/dnf/dnf.conf หรือ /etc/yum.conf
sudo sed -i 's/^#installonly_limit=.*/installonly_limit=3/' /etc/dnf/dnf.conf
# Reboot เพื่อใช้ Kernel ใหม่
sudo reboot
# จัดการ Default Kernel ด้วย grubby (RHEL/Rocky)
# ดู Kernel ทั้งหมด
grubby --info=ALL
# ดู Default Kernel ปัจจุบัน
grubby --default-kernel
grubby --default-index
# ตั้ง Default Kernel เป็น Kernel ล่าสุด
sudo grubby --set-default=$(ls /boot/vmlinuz-* | sort -V | tail -1)
# ตั้ง Default Kernel เป็น Index เฉพาะ
sudo grubby --set-default-index=0
# ลบ Kernel เก่า
sudo dnf remove kernel-5.14.0-362.el9.x86_64
Automatic Security Updates
การตั้งค่า Automatic Security Updates ช่วยให้ Kernel และ Package ได้รับ Security Patch อัตโนมัติโดยไม่ต้องทำเอง
# Ubuntu: ตั้งค่า Unattended Upgrades
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgrades
# ตรวจสอบ Config
cat /etc/apt/apt.conf.d/50unattended-upgrades
# เปิดใช้งาน Auto-Update
cat /etc/apt/apt.conf.d/20auto-upgrades
# ควรมี:
# APT::Periodic::Update-Package-Lists "1";
# APT::Periodic::Unattended-Upgrade "1";
# ตรวจสอบ Log
cat /var/log/unattended-upgrades/unattended-upgrades.log
# ทดสอบ Dry Run
sudo unattended-upgrade --dry-run -d
# RHEL/Rocky: ตั้งค่า dnf-automatic
sudo dnf install dnf-automatic -y
# Config: /etc/dnf/automatic.conf
# แก้ไขให้ apply security updates อัตโนมัติ
sudo sed -i 's/apply_updates = no/apply_updates = yes/' /etc/dnf/automatic.conf
sudo sed -i 's/upgrade_type = default/upgrade_type = security/' /etc/dnf/automatic.conf
# เปิดใช้งาน Timer
sudo systemctl enable --now dnf-automatic.timer
sudo systemctl status dnf-automatic.timer
# ดู Log
journalctl -u dnf-automatic
Kernel Parameters ด้วย sysctl
sysctl ใช้ปรับ Kernel Parameters ขณะ Runtime โดยไม่ต้อง Reboot ค่าที่ปรับบ่อย ได้แก่ Network Stack, Memory Management และ Security Settings
# ดู Kernel Parameters ทั้งหมด
sysctl -a
# ดู Parameter เฉพาะ
sysctl net.ipv4.ip_forward
sysctl vm.swappiness
sysctl kernel.pid_max
# ปรับ Parameter ชั่วคราว (Reset เมื่อ Reboot)
sudo sysctl -w vm.swappiness=10
sudo sysctl -w net.ipv4.ip_forward=1
sudo sysctl -w net.core.somaxconn=65535
# ตั้งค่าถาวรใน /etc/sysctl.conf หรือ /etc/sysctl.d/
sudo tee /etc/sysctl.d/99-custom.conf <<'EOF'
# Reduce swap usage
vm.swappiness = 10
# Increase max connections queue
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
# Improve network performance
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_intvl = 30
# Security: disable IP forwarding (เว้นแต่เป็น Router/VPN)
net.ipv4.ip_forward = 0
# Security: ignore ICMP broadcast
net.ipv4.icmp_echo_ignore_broadcasts = 1
EOF
# Apply โดยไม่ต้อง Reboot
sudo sysctl -p /etc/sysctl.d/99-custom.conf
sudo sysctl --system # Apply ทุกไฟล์ใน sysctl.d/
# Kernel Parameters สำคัญสำหรับ Web Server / Cloud VPS
# ดู Swappiness ปัจจุบัน (0-100, ค่าน้อย = ใช้ Swap น้อย)
sysctl vm.swappiness
# แนะนำ: 10 สำหรับ Server ที่มี RAM เพียงพอ
# ดู inotify limit (สำคัญสำหรับ Application ที่ Watch files มาก)
sysctl fs.inotify.max_user_watches
# ถ้าพบ "Too many open files" ให้เพิ่ม:
sudo sysctl -w fs.inotify.max_user_watches=524288
# Transparent Hugepages (แนะนำ disable สำหรับ Database)
cat /sys/kernel/mm/transparent_hugepage/enabled
echo madvise | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
# ดู OOM Score ของ Process
cat /proc/$(pgrep nginx | head -1)/oom_score
# ปรับ OOM Score (ค่าสูง = OOM Killer จะ Kill ก่อน)
echo -500 | sudo tee /proc/$(pgrep mysql | head -1)/oom_score_adj
Kernel Modules — ติดตั้งและจัดการ
# ดู Module ที่โหลดอยู่
lsmod
lsmod | grep -i net
# ดูข้อมูล Module เฉพาะ
modinfo 8021q
modinfo overlay
# โหลด Module ชั่วคราว
sudo modprobe 8021q
sudo modprobe overlay
# โหลด Module พร้อม Options
sudo modprobe bonding mode=active-backup miimon=100
# ให้ Module โหลดอัตโนมัติเมื่อ Boot
echo "8021q" | sudo tee /etc/modules-load.d/8021q.conf
echo "overlay" | sudo tee /etc/modules-load.d/overlay.conf
# ส่ง Option ให้ Module เมื่อ Load
sudo tee /etc/modprobe.d/bonding.conf <<'EOF'
options bonding mode=active-backup miimon=100
EOF
# ลบ Module (ต้องไม่มีอะไรใช้งานอยู่)
sudo modprobe -r 8021q
# Blacklist Module (ป้องกันไม่ให้โหลด)
echo "blacklist pcspkr" | sudo tee /etc/modprobe.d/blacklist-pcspkr.conf
Kernel Rollback — กลับไปใช้ Kernel เก่า
# วิธีที่ 1: เลือก Kernel เก่าตอน Boot
# กดค้าง Shift (BIOS) หรือ Esc (UEFI) ระหว่าง Boot เพื่อแสดง GRUB Menu
# เลือก "Advanced options" → เลือก Kernel เก่า
# วิธีที่ 2: ตั้ง Default Kernel เป็น Version เก่า (Ubuntu)
# ดู Kernel ที่ติดตั้ง
dpkg --list | grep linux-image | grep "^ii"
# ตัวอย่าง: ตั้ง Kernel 6.8.0-49 เป็น Default
# แก้ไข /etc/default/grub
# GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 6.8.0-49-generic"
sudo nano /etc/default/grub
sudo update-grub
# วิธีที่ 3: Rollback บน RHEL/Rocky ด้วย grubby
# ดู Kernel ทั้งหมด
grubby --info=ALL
# ตั้ง Default กลับไปเป็น Kernel เก่า
sudo grubby --set-default=/boot/vmlinuz-5.14.0-362.13.1.el9_3.x86_64
sudo reboot
# ตรวจสอบว่า Boot ด้วย Kernel เก่าแล้ว
uname -r
Live Kernel Patching ด้วย kpatch/livepatch
Live Kernel Patching ช่วยให้ apply Security Patch ได้โดยไม่ต้อง Reboot เหมาะสำหรับ Production Server ที่ต้องการ High Availability
# Ubuntu: Canonical Livepatch (ฟรีสำหรับ Personal Use)
# ต้องมี Ubuntu Advantage Token (ubuntu.com/security/livepatch)
# ติดตั้ง snapd และ canonical-livepatch
sudo snap install canonical-livepatch
sudo canonical-livepatch enable <TOKEN>
# ตรวจสอบสถานะ
sudo canonical-livepatch status
sudo canonical-livepatch status --verbose
# RHEL: kpatch (ต้องมี RHEL subscription)
sudo dnf install kpatch kpatch-dnf -y
sudo systemctl enable --now kpatch
# ดู Patch ที่ Apply แล้ว
kpatch list
# ทางเลือกฟรี: kpatch-build สำหรับ Compile Patch เอง
# (ใช้สำหรับ Test environment)
sudo dnf install kpatch-build gcc elfutils-devel -y
# ตรวจสอบว่ารองรับ kpatch
kpatch-build --help
# วิธีทั่วไปสำหรับ VPS ทั่วไปคือ Scheduled Maintenance Window
# ตั้ง cron อัพเดตและ Reboot ช่วงกลางดึก
# 0 3 * * 0 apt-get update && apt-get upgrade -y && reboot
ตรวจสอบ Kernel Crash และ Panic
# ดู Kernel Message Log
dmesg # ข้อความทั้งหมดนับจาก Boot ปัจจุบัน
dmesg -T # แสดง Timestamp แบบอ่านได้
dmesg --level=err,warn # แสดงเฉพาะ Error และ Warning
dmesg | grep -i "error\|fail\|panic\|oops"
# ดู Kernel Log ผ่าน journald
journalctl -k # Kernel messages ทั้งหมด
journalctl -k -b -1 # Kernel messages จาก Boot ครั้งก่อน
journalctl -k --since "1 hour ago"
# ดู Kernel Oops / Crash ในอดีต
ls /var/crash/ # Ubuntu crash dumps
ls /var/lib/systemd/coredump/ # systemd coredump
# ตั้งค่า kdump — บันทึก Crash Dump เมื่อ Kernel Panic (Ubuntu)
sudo apt install linux-crashdump -y
sudo systemctl enable kdump-tools
# แก้ไข /etc/default/grub เพิ่ม crashkernel=256M
sudo update-grub
sudo reboot
Script ตรวจสอบสถานะ Kernel
#!/bin/bash
# /usr/local/bin/kernel-status.sh
# ตรวจสอบสถานะ Kernel และแจ้งเตือนถ้ามี Update
CURRENT_KERNEL=$(uname -r)
ALERT_EMAIL="[email protected]"
HOSTNAME=$(hostname)
echo "=== Kernel Status Report: ${HOSTNAME} ==="
echo "Current Kernel: ${CURRENT_KERNEL}"
echo ""
# ตรวจสอบ Reboot Required
if [ -f /var/run/reboot-required ]; then
echo "⚠️ REBOOT REQUIRED"
if [ -f /var/run/reboot-required.pkgs ]; then
echo " Packages requiring reboot:"
cat /var/run/reboot-required.pkgs | sed 's/^/ - /'
fi
echo ""
echo "Reboot required on ${HOSTNAME} (kernel update pending)" \
| mail -s "[Notice] Reboot Required: ${HOSTNAME}" "$ALERT_EMAIL"
fi
# ดู Kernel ที่ติดตั้งทั้งหมด (Ubuntu)
if command -v dpkg >/dev/null; then
echo "Installed Kernels:"
dpkg --list | grep "^ii.*linux-image" | awk '{print " " $2}'
echo ""
fi
# ดู Kernel Errors ล่าสุด
ERRORS=$(dmesg --level=err,warn -T 2>/dev/null | tail -10)
if [ -n "$ERRORS" ]; then
echo "Recent Kernel Errors/Warnings:"
echo "$ERRORS"
fi
echo ""
echo "Uptime: $(uptime -p)"
สรุป
การจัดการ Kernel บน Linux ทำได้ผ่าน Package Manager ซึ่งปลอดภัยและง่ายที่สุด Ubuntu ใช้ apt กับ unattended-upgrades สำหรับ Auto Security Update ส่วน RHEL/Rocky ใช้ dnf กับ dnf-automatic การ Rollback ทำได้โดยเลือก Kernel เก่าจาก GRUB Menu หรือใช้ grubby เพื่อตั้ง Default Kernel ใหม่ Kernel Parameters ปรับได้ผ่าน sysctl โดยค่าถาวรใส่ใน /etc/sysctl.d/ และ Live Kernel Patching ด้วย Canonical Livepatch หรือ kpatch ช่วยลด Downtime จาก Security Update บน Production Server
แนะนำบริการ DE
การอัพเดตและจัดการ Kernel ต้องการสิทธิ์ Root และการเข้าถึง Package Manager Cloud VPS ของ DE ให้ Root Access เต็มรูปแบบ รองรับการอัพเดต Kernel, ตั้งค่า sysctl parameters และทำ Kernel Rollback ได้อย่างอิสระ พร้อม VNC Console สำหรับกู้คืนระบบเมื่อ Kernel ใหม่มีปัญหา
สำหรับผู้ที่ต้องการโฮสต์เว็บไซต์โดยไม่ต้องจัดการ Kernel เอง Cloud Hosting ของ DE ดูแล System Updates และ Security Patches ให้อัตโนมัติ โดยไม่กระทบ Uptime ของเว็บไซต์

