Server Hardening ความปลอดภัย VPS Linux

เสริมความปลอดภัย VPS Linux: CIS Hardening ฉบับปฏิบัติ

มิถุนายน 2026 · อ่าน 11 นาที

CIS (Center for Internet Security) Benchmark คือมาตรฐานการ hardening server ที่องค์กรและ cloud provider ทั่วโลกใช้เป็นอ้างอิง บทความนี้สรุป checklist ที่ทำได้จริงบน VPS Ubuntu/Debian ตั้งแต่ kernel parameter, user account policy, service audit ไปจนถึง auditd logging — เหมาะสำหรับ server ที่รันงาน production ที่ต้องการความปลอดภัยสูงกว่า basic hardening

1. Kernel Network Security (sysctl)

Parameter เหล่านี้ป้องกัน network-level attacks เช่น SYN flood, IP spoofing และ ICMP redirect:

sudo nano /etc/sysctl.d/99-hardening.conf
# ป้องกัน IP spoofing
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1

# ปิด ICMP redirect (ป้องกัน routing manipulation)
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0

# ป้องกัน SYN flood
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_synack_retries = 2

# ปิด IP source routing
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0

# ซ่อน kernel pointer ใน dmesg (ป้องกัน KASLR bypass)
kernel.kptr_restrict = 2
kernel.dmesg_restrict = 1

# จำกัด ptrace (ป้องกัน process snooping)
kernel.yama.ptrace_scope = 1
# Apply ทันที
sudo sysctl -p /etc/sysctl.d/99-hardening.conf

2. User Account Security

# ตั้ง password policy (ต้องติดตั้ง libpam-pwquality)
sudo apt install libpam-pwquality -y
sudo nano /etc/security/pwquality.conf
# Password requirements
minlen = 12
dcredit = -1    # อย่างน้อย 1 ตัวเลข
ucredit = -1    # อย่างน้อย 1 ตัวพิมพ์ใหญ่
lcredit = -1    # อย่างน้อย 1 ตัวพิมพ์เล็ก
ocredit = -1    # อย่างน้อย 1 special character
maxrepeat = 3   # ห้ามตัวอักษรซ้ำเกิน 3 ตัวติดกัน
# ตั้ง account lockout หลัง login fail 5 ครั้ง
sudo nano /etc/pam.d/common-auth
# เพิ่มบรรทัดนี้ก่อน auth ตัวอื่น
auth required pam_faillock.so preauth silent audit deny=5 unlock_time=900
# ล็อค account ที่ไม่มี password (shell account ที่ควรปิด)
sudo passwd -l daemon
sudo passwd -l bin
sudo passwd -l sync

# ดู user ที่มี shell และ UID >= 1000 (user จริง)
awk -F: '($3 >= 1000) && ($7 != "/usr/sbin/nologin") && ($7 != "/bin/false")' /etc/passwd

3. Disable Service ที่ไม่จำเป็น

# ดู service ที่รันอยู่ทั้งหมด
systemctl list-units --type=service --state=running

# ปิด service ที่ไม่จำเป็นบน VPS ทั่วไป
sudo systemctl disable --now avahi-daemon 2>/dev/null
sudo systemctl disable --now cups 2>/dev/null
sudo systemctl disable --now bluetooth 2>/dev/null
sudo systemctl disable --now isc-dhcp-server 2>/dev/null
sudo systemctl disable --now rpcbind 2>/dev/null

# ตรวจสอบ listening port ทั้งหมด
sudo ss -tlnp

4. File System Hardening

# ตั้ง noexec, nosuid, nodev บน /tmp
sudo nano /etc/fstab
# เพิ่ม options สำหรับ /tmp
tmpfs   /tmp    tmpfs   defaults,noexec,nosuid,nodev    0 0
# ค้นหา SUID/SGID files ที่ผิดปกติ
find / -xdev \( -perm -4000 -o -perm -2000 \) -type f -print 2>/dev/null | \
  grep -v -E "^/(usr|bin|sbin)/"

# ค้นหาไฟล์ที่ไม่มีเจ้าของ
find / -xdev \( -nouser -o -nogroup \) -type f -print 2>/dev/null

5. auditd — Audit Logging

auditd บันทึกทุก system call สำคัญ เหมาะสำหรับ forensics และ compliance:

sudo apt install auditd -y
sudo systemctl enable auditd --now
sudo nano /etc/audit/rules.d/hardening.rules
# บันทึกการแก้ไข passwd/sudoers/shadow
-w /etc/passwd -p wa -k identity
-w /etc/group -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/sudoers -p wa -k sudoers

# บันทึก sudo usage
-w /usr/bin/sudo -p x -k sudo_usage

# บันทึกการ login/logout
-w /var/log/lastlog -p wa -k logins
-w /var/run/faillock -p wa -k logins

# บันทึก SSH config changes
-w /etc/ssh/sshd_config -p wa -k sshd_config

# บันทึก cron changes
-w /etc/crontab -p wa -k cron
-w /etc/cron.d/ -p wa -k cron
# Load rules ใหม่
sudo augenrules --load
sudo systemctl restart auditd

# ดู audit events ล่าสุด
sudo ausearch -ts today -i | tail -30

6. Automatic Security Updates

sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgrades

# ตั้งค่า auto-install security patches เท่านั้น
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades
Unattended-Upgrade::Allowed-Origins {
    "${distro_id}:${distro_codename}-security";
};
Unattended-Upgrade::AutoFixInterruptedDpkg "true";
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Automatic-Reboot "false";  // ไม่ reboot อัตโนมัติ
ระวัง: ทดสอบ kernel parameter และ PAM changes บน server test ก่อนทำบน production เสมอ การตั้งค่าผิดโดยเฉพาะ pam.d อาจทำให้ login ไม่ได้ ควร snapshot VM ก่อนทำ hardening

ตรวจสอบว่า Hardening ทำงานถูกต้อง

# ตรวจสอบ sysctl parameters ที่ตั้งไว้
sysctl net.ipv4.tcp_syncookies net.ipv4.conf.all.rp_filter kernel.kptr_restrict

# ตรวจสอบ service ที่ disable แล้ว
systemctl is-enabled avahi-daemon cups rpcbind 2>/dev/null

# ตรวจสอบ auditd ว่า log ทำงาน
sudo auditctl -l | head -20
sudo ausearch -k sudoers -ts today 2>/dev/null | head -10

# รัน Lynis security audit (เครื่องมือ automated check)
sudo apt install lynis -y
sudo lynis audit system
Tip: Lynis จะให้ Hardening Index score (0-100) และแสดง suggestion ที่ยังขาดอยู่ เป้าหมายสำหรับ production server ควรอยู่ที่ 70+ ถ้าต่ำกว่านี้ให้ดู suggestions ในส่วน "Suggestions" ของ output

แก้ปัญหาที่พบบ่อย

Error: Login ไม่ได้หลังตั้ง pam_faillock

Account ถูก lock อัตโนมัติ ต้อง unlock ด้วย root หรือ sudo user อื่น:

# ดู account ที่ถูก lock
sudo faillock

# Unlock account เฉพาะ user
sudo faillock --user username --reset

# ถ้าทุก user ถูก lock และเข้าไม่ได้ ต้อง boot single-user mode
# หรือใช้ VPS console จาก control panel ของ provider

Error: sysctl parameter ไม่มีผลหลัง reboot

ต้องวางไฟล์ใน /etc/sysctl.d/ ไม่ใช่แก้ /etc/sysctl.conf โดยตรง บน Ubuntu 22.04+ sysctl.conf มี priority ต่ำกว่าไฟล์ใน sysctl.d:

# ตรวจสอบว่า file อยู่ถูกที่
ls /etc/sysctl.d/

# Apply ทุก config ใหม่
sudo sysctl --system

# ตรวจสอบค่าจริงที่ใช้งาน
sysctl net.ipv4.conf.all.rp_filter

auditd ทำให้ I/O choke บน server ที่ busy

auditd เขียน log มาก อาจกระทบ disk I/O ปรับ rate limit ใน /etc/audit/auditd.conf:

# ใน /etc/audit/auditd.conf
rate_limit = 1000      # max events/second (ค่า default 0 = unlimited)
max_log_file_action = ROTATE
max_log_file = 50      # MB ต่อ log file
num_logs = 5           # เก็บ log ย้อนหลัง 5 ไฟล์

sudo systemctl restart auditd

Lynis ให้ Hardening Index ต่ำมากทั้งที่ทำตาม checklist แล้ว

Lynis ตรวจ parameter หลายร้อยอย่าง score ต่ำแรกเริ่มเป็นเรื่องปกติ ดู warning สีแดงก่อน แล้วค่อยแก้ suggestion สีเหลือง สิ่งที่มักทำให้ score ต่ำบน VPS ใหม่:

# ดู Warnings ทั้งหมดจาก Lynis
sudo lynis audit system 2>&1 | grep "Warning\|Suggestion" | head -30

# สิ่งที่แก้ง่ายและ score ขึ้นเร็ว:
# - ติดตั้ง fail2ban
# - ตั้ง GRUB password
# - ตั้ง umask 027 ใน /etc/profile
# - ปิด root SSH login

คำถามที่พบบ่อย

ต้องทำ CIS Hardening ทุกข้อไหม?

ไม่จำเป็น CIS Benchmark แบ่งออกเป็น Level 1 (จำเป็น ไม่กระทบการใช้งาน) และ Level 2 (เข้มข้น อาจ break บาง service) สำหรับ VPS ทั่วไปทำ Level 1 ให้ครบก็เพียงพอ บทความนี้ครอบคลุม Level 1 ทั้งหมดแล้ว

ทำ Hardening แล้วต้อง reboot ไหม?

ส่วนใหญ่ไม่ต้อง sysctl และ auditd rules apply ได้ทันทีโดยไม่ต้อง reboot ส่วน /etc/fstab options สำหรับ /tmp และ GRUB settings ต้องการ reboot จึงจะมีผล แนะนำให้ทดสอบใน maintenance window ก่อน reboot production

Hardening server แล้ว performance จะช้าลงไหม?

ผลกระทบต่อ performance น้อยมาก kernel network parameters เปลี่ยนแค่ behavior ไม่เพิ่ม overhead, auditd อาจกระทบ I/O เล็กน้อยถ้าตั้ง rule เยอะมาก ส่วนใหญ่ไม่สังเกตได้ในสภาวะปกติ