บทความนี้อาจมีลิงก์ affiliate — หากสมัครผ่านลิงก์ เราได้รับค่าคอมมิชชั่นโดยไม่มีค่าใช้จ่ายเพิ่มจากคุณ · นโยบายบรรณาธิการ
คู่มือ VPS Security

คู่มือ VPS Security Hardening: ป้องกัน Linux VPS ให้ปลอดภัย 2026

โดย ทีมรีวิว CloudPicked · อัปเดตล่าสุด กันยายน 2026

VPS Security Hardening
สรุปย่อ: VPS ที่ตั้งค่าใหม่จาก cloud provider มักมี port เปิดกว้างและใช้ password auth ซึ่งเสี่ยงมาก บทความนี้ครอบคลุมการ hardening ขั้นพื้นฐานถึงขั้นสูงที่คุณทำเองได้ในเวลาไม่ถึง 1 ชั่วโมง

ทุกวันนี้ internet scanner อย่าง Shodan และ bots จำนวนมากสแกนหา VPS ที่ตั้งค่าผิดตลอด 24 ชั่วโมง เมื่อ deploy VPS ใหม่แล้วไม่ทำ security hardening อาจถูก brute-force หรือ exploit ได้ภายในไม่กี่ชั่วโมง บทความนี้รวบรวมขั้นตอนสำคัญสำหรับ Linux VPS (Ubuntu/Debian) ที่ควรทำทันทีหลัง deploy

1. อัพเดท System ก่อนเป็นสิ่งแรก

ก่อนทำอะไรทั้งหมด อัพเดท package ให้เป็น version ล่าสุดเพื่อปิดช่องโหว่ที่รู้จักแล้ว:

sudo apt update && sudo apt upgrade -y
sudo apt dist-upgrade -y
sudo apt autoremove -y

สำหรับ CentOS/AlmaLinux/Rocky Linux:

sudo dnf update -y
sudo dnf upgrade -y

ตั้งค่า automatic security updates เพื่อให้ระบบอัพเดท security patch อัตโนมัติ:

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

2. สร้าง User ใหม่และ Disable Root Login

การ login ด้วย root โดยตรงเป็นความเสี่ยงอันดับต้นๆ เพราะ root เป็น username แรกที่ hacker ลองเดาเสมอ

สร้าง sudo user ใหม่

# สร้าง user ใหม่ (แทน yourusername ด้วยชื่อที่ต้องการ)
sudo adduser yourusername

# เพิ่มเข้ากลุ่ม sudo
sudo usermod -aG sudo yourusername

# ทดสอบ login ด้วย user ใหม่ก่อน แล้วค่อย disable root

Disable Root SSH Login

แก้ไขไฟล์ /etc/ssh/sshd_config:

sudo nano /etc/ssh/sshd_config

หาและเปลี่ยนบรรทัดต่อไปนี้:

PermitRootLogin no

แล้ว restart SSH:

sudo systemctl restart sshd
คำเตือน: ให้ login ด้วย user ใหม่ให้สำเร็จก่อนที่จะ disable root login และปิด password auth มิฉะนั้นอาจ lock ตัวเองออกจาก server ได้

3. ตั้งค่า SSH Key Authentication

SSH key authentication แข็งแกร่งกว่า password มาก เพราะใช้คู่กุญแจ cryptographic ที่แทบเป็นไปไม่ได้จะ brute-force

สร้าง SSH Key บนเครื่อง local ของคุณ

# สร้าง ed25519 key (แนะนำ — ปลอดภัยและเร็วกว่า RSA 4096)
ssh-keygen -t ed25519 -C "[email protected]"

# key จะถูกบันทึกที่ ~/.ssh/id_ed25519 (private) และ ~/.ssh/id_ed25519.pub (public)

คัดลอก Public Key ไปยัง Server

# วิธีที่ 1: ใช้ ssh-copy-id (ง่ายที่สุด)
ssh-copy-id -i ~/.ssh/id_ed25519.pub yourusername@YOUR_SERVER_IP

# วิธีที่ 2: manual copy
cat ~/.ssh/id_ed25519.pub | ssh yourusername@YOUR_SERVER_IP "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

ปิด Password Authentication

หลังจาก verify ว่า SSH key login ทำงานได้แล้ว ให้ปิด password auth:

sudo nano /etc/ssh/sshd_config

# เปลี่ยนหรือเพิ่มบรรทัดนี้:
PasswordAuthentication no
PubkeyAuthentication yes

# restart SSH
sudo systemctl restart sshd

4. เปลี่ยน SSH Port (ป้องกัน Automated Scan)

Port 22 เป็น default ที่ scanner ทุกตัวรู้จัก การเปลี่ยนเป็น port อื่นลด automated brute-force ได้มาก (แม้จะไม่ใช่ security solution จริงๆ แต่ช่วยลด noise log ได้มหาศาล)

sudo nano /etc/ssh/sshd_config

# เปลี่ยน Port (เลือก port ที่ยังว่าง เช่น 2222 หรือ 49152-65535)
Port 2222

# restart SSH
sudo systemctl restart sshd
สำคัญ: ก่อน restart อย่าลืมเปิด port ใหม่ใน firewall ก่อน มิฉะนั้นจะ lock ตัวเองออก

5. ตั้งค่า UFW Firewall

UFW (Uncomplicated Firewall) เป็น front-end ของ iptables ที่ใช้งานง่าย เปิดเฉพาะ port ที่จำเป็นเท่านั้น

# ติดตั้ง UFW (Ubuntu มักมีอยู่แล้ว)
sudo apt install ufw -y

# ตั้ง default rules (deny all incoming, allow all outgoing)
sudo ufw default deny incoming
sudo ufw default allow outgoing

# อนุญาต SSH port ที่เปลี่ยนไว้ (สำคัญมาก!)
sudo ufw allow 2222/tcp comment 'SSH custom port'

# อนุญาต HTTP/HTTPS ถ้าเป็น web server
sudo ufw allow 80/tcp comment 'HTTP'
sudo ufw allow 443/tcp comment 'HTTPS'

# เปิดใช้งาน UFW
sudo ufw enable

# ตรวจสอบสถานะ
sudo ufw status verbose

ตัวอย่าง Rule เพิ่มเติม

# Rate limiting SSH (ป้องกัน brute-force)
sudo ufw limit 2222/tcp

# อนุญาต IP เฉพาะ
sudo ufw allow from 1.2.3.4 to any port 2222

# ดู numbered rules
sudo ufw status numbered

# ลบ rule
sudo ufw delete 3

6. ติดตั้งและตั้งค่า fail2ban

fail2ban ตรวจ log ไฟล์และ ban IP ที่ login ผิดพลาดเกินจำนวนครั้งที่กำหนด เป็น layer ป้องกันสำคัญโดยเฉพาะสำหรับ SSH และ web services

# ติดตั้ง
sudo apt install fail2ban -y

# backup config เดิม
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.conf.bak

# สร้าง local config ที่ override config เดิม
sudo nano /etc/fail2ban/jail.local

ใส่เนื้อหาต่อไปนี้ในไฟล์ jail.local:

[DEFAULT]
bantime  = 3600        ; ban 1 ชั่วโมง
findtime = 600         ; ดู 10 นาทีย้อนหลัง
maxretry = 5           ; ban หลัง 5 ครั้ง
ignoreip = 127.0.0.1/8 ::1  ; ไม่ ban localhost

[sshd]
enabled  = true
port     = 2222        ; ระบุ port SSH ที่เปลี่ยนไว้
logpath  = /var/log/auth.log
maxretry = 3           ; เข้มงวดกว่า default สำหรับ SSH
# เปิดใช้งานและ restart
sudo systemctl enable fail2ban
sudo systemctl restart fail2ban

# ตรวจสอบสถานะ
sudo fail2ban-client status
sudo fail2ban-client status sshd

# ดู IP ที่ถูก ban
sudo fail2ban-client status sshd | grep 'Banned IP'

# unban IP
sudo fail2ban-client set sshd unbanip 1.2.3.4

7. ปิด Service ที่ไม่จำเป็น

Service ทุกตัวที่รันอยู่คือ attack surface ที่เพิ่มขึ้น ตรวจสอบและปิดสิ่งที่ไม่ต้องการ:

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

# ดู port ที่เปิดอยู่
sudo ss -tlnp

# ปิด service ที่ไม่ต้องการ (เช่น avahi-daemon ถ้าไม่ใช้ mDNS)
sudo systemctl disable avahi-daemon
sudo systemctl stop avahi-daemon

8. ติดตั้ง Intrusion Detection System (IDS)

rkhunter (Rootkit Hunter)

sudo apt install rkhunter -y

# อัพเดทฐานข้อมูล
sudo rkhunter --update

# สแกนระบบ
sudo rkhunter --check

# ดู properties ปัจจุบัน (หลัง clean install)
sudo rkhunter --propupd

Lynis (Security Auditing)

sudo apt install lynis -y

# รัน audit ครบวงจร
sudo lynis audit system

# ดูสรุปคะแนน (Hardening Index)
sudo lynis audit system | grep "Hardening index"

9. ตั้งค่า Automatic Security Updates

sudo apt install unattended-upgrades apt-listchanges -y

# แก้ไข config
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades

Uncomment บรรทัดต่อไปนี้:

Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Remove-New-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "false";  // ตั้ง true ถ้าต้องการ auto reboot
# ทดสอบ
sudo unattended-upgrades --dry-run

# เปิดใช้งาน
sudo dpkg-reconfigure -plow unattended-upgrades

10. Harden Kernel Parameters (sysctl)

ตั้งค่า kernel parameters เพื่อป้องกันการโจมตีระดับ network:

sudo nano /etc/sysctl.d/99-security.conf

เพิ่ม:

# ป้องกัน IP spoofing
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1

# ปิด IP forwarding (ถ้าไม่ใช่ router)
net.ipv4.ip_forward = 0

# ป้องกัน ICMP redirects
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0

# ปิด Source Routing
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0

# Log Martians (suspicious packets)
net.ipv4.conf.all.log_martians = 1

# SYN flood protection
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048
# โหลด config ใหม่
sudo sysctl -p /etc/sysctl.d/99-security.conf

11. ตั้งค่า Two-Factor Authentication (2FA) สำหรับ SSH

เพิ่ม layer ความปลอดภัยด้วย TOTP (Time-based One-Time Password) เช่น Google Authenticator:

sudo apt install libpam-google-authenticator -y

# ตั้งค่าสำหรับ user ปัจจุบัน (scan QR code ด้วย Google Authenticator)
google-authenticator

แก้ไข PAM config:

sudo nano /etc/pam.d/sshd

# เพิ่มบรรทัดนี้:
auth required pam_google_authenticator.so

แก้ไข sshd_config:

sudo nano /etc/ssh/sshd_config

# เปลี่ยนเป็น:
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive

sudo systemctl restart sshd

12. Monitoring และ Log Management

ดู Failed Login Attempts

# ดู failed logins ล่าสุด
sudo lastb | head -20

# ดู successful logins
sudo last | head -20

# ดู auth log แบบ real-time
sudo tail -f /var/log/auth.log | grep -i "failed\|invalid\|error"

ติดตั้ง Logwatch

sudo apt install logwatch -y

# ส่ง daily report ทาง email
sudo logwatch --output mail --mailto [email protected] --detail high

ต้องการ VPS ที่ใช้งานง่ายสำหรับคนไทย? AsiaGB.com และ Bangmod Cloud มีเซิร์ฟเวอร์ในไทย รองรับ Ubuntu/Debian พร้อม DirectAdmin Control Panel และทีม support ภาษาไทย

ดู AsiaGB VPS   ดู Bangmod Cloud

13. Security Checklist สรุป

งานความสำคัญเวลาประมาณ
อัพเดท Systemสูงมาก5 นาที
สร้าง sudo user + disable rootสูงมาก5 นาที
SSH Key Authenticationสูงมาก10 นาที
เปลี่ยน SSH Portปานกลาง2 นาที
ตั้งค่า UFW Firewallสูงมาก10 นาที
ติดตั้ง fail2banสูง10 นาที
ปิด Service ไม่จำเป็นปานกลาง10 นาที
Kernel Hardening (sysctl)ปานกลาง5 นาที
Automatic Updatesสูง5 นาที
2FA สำหรับ SSHสูง10 นาที
Security Audit (Lynis)แนะนำ15 นาที

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

ทำไมต้อง hardening VPS?

VPS ที่ติดตั้งใหม่มักเปิด port กว้างและใช้ password auth ซึ่งเสี่ยงต่อ brute-force และ exploit ที่แพร่หลายบนอินเทอร์เน็ต การ hardening ลดพื้นผิวการโจมตีให้เหลือน้อยที่สุด

SSH key แตกต่างจาก password อย่างไร?

SSH key ใช้คู่กุญแจ public/private (ed25519 = 256-bit) ซึ่งแทบเป็นไปไม่ได้ที่จะ brute-force เทียบกับ password ที่คน brute-force ได้ภายในไม่กี่ชั่วโมงหรือน้อยกว่านั้น

UFW กับ iptables ต่างกันอย่างไร?

UFW (Uncomplicated Firewall) เป็น front-end สำหรับ iptables ที่ใช้งานง่ายกว่ามาก เหมาะสำหรับ admin ทั่วไป ส่วน iptables มีความยืดหยุ่นสูงกว่าแต่ซับซ้อน

fail2ban ทำงานอย่างไร?

fail2ban ตรวจ log ไฟล์ (เช่น /var/log/auth.log) และ ban IP ที่ login ผิดเกินจำนวนครั้งที่กำหนด โดย block ผ่าน iptables/nftables โดยอัตโนมัติ

ควร disable root login หรือไม่?

ควร disable root login ทาง SSH เสมอ เพราะ root เป็น username ที่ hacker เดาเป็นอันดับแรก ให้ใช้ user ปกติแล้ว sudo แทน

VPS provider ไหนดีสำหรับคนไทย?

AsiaGB.com และ Bangmod Cloud เป็นตัวเลือกยอดนิยมสำหรับคนไทย ทั้งคู่มีเซิร์ฟเวอร์ในไทย รองรับภาษาไทย และมี support 24 ชั่วโมง

สรุป

การ hardening VPS ไม่ใช่เรื่องยากและใช้เวลาไม่ถึง 1 ชั่วโมง แต่สร้างความแตกต่างมหาศาลระหว่างเซิร์ฟเวอร์ที่ถูก compromise กับเซิร์ฟเวอร์ที่ปลอดภัย เริ่มจากสิ่งสำคัญที่สุด: อัพเดทระบบ, ตั้งค่า SSH key, และเปิด firewall จากนั้นค่อยเพิ่ม layer ความปลอดภัยอื่นๆ ตามลำดับ

หากกำลังมองหา VPS provider ที่เหมาะสำหรับคนไทย AsiaGB.com ให้บริการ VPS บนเซิร์ฟเวอร์ในไทยพร้อม DirectAdmin และ support ภาษาไทย และ Bangmod Cloud เป็นอีกตัวเลือกยอดนิยมในราคาที่คุ้มค่า