Linux Server Security คู่มือรักษาความปลอดภัย 2026
ความปลอดภัยของ Linux Server เป็นสิ่งที่ SysAdmin ต้องให้ความสำคัญเป็นอันดับแรกเพราะเซิร์ฟเวอร์ที่เชื่อมต่ออินเทอร์เน็ตถูกโจมตีตลอดเวลาตั้งแต่ Brute Force SSH ไปจนถึง Zero-day Exploit ในคู่มือนี้เราจะครอบคลุมทุกแง่มุมของการรักษาความปลอดภัย Linux Server ตั้งแต่พื้นฐานไปจนถึงขั้นสูง
วิดีโอประกอบการเรียนรู้ | YouTube @icafefx
SSH Hardening

SSH เป็นช่องทางหลักที่ SysAdmin ใช้จัดการเซิร์ฟเวอร์และเป็นเป้าหมายอันดับหนึ่งของผู้โจมตีการ Harden SSH ให้แข็งแรงเป็นสิ่งแรกที่ต้องทำ:
1. เปลี่ยนพอร์ต SSH
# แก้ไข /etc/ssh/sshd_config Port 2222
การเปลี่ยนจากพอร์ต 22 ไม่ได้เพิ่มความปลอดภัยจริงแต่ช่วยลด Automated Bot Attack ได้มาก Log ของ SSH จะสะอาดขึ้นอย่างเห็นได้ชัด
2. ปิด Root Login
PermitRootLogin no
ห้าม Login ด้วย Root โดยตรงให้ Login ด้วย User ปกติแล้วใช้ sudo แทน
เนื้อหาเกี่ยวข้อง — บทความที่เกี่ยวข้อง: catch javascript คือ
3. ใช้ SSH Key แทน Password
# สร้าง Key บนเครื่อง Client ssh-keygen -t ed25519 -C "admin@company" # Copy Key ไปยัง Server ssh-copy-id -p 2222 user@server # ปิด Password Authentication PasswordAuthentication no
SSH Key ที่แนะนำคือ Ed25519 เพราะสั้นแต่แข็งแรงกว่า RSA หลังจาก Copy Key แล้วให้ปิด Password Authentication ทันที
4. จำกัด User ที่ Login ได้
AllowUsers admin deploy AllowGroups sshusers
5. ตั้ง Timeout
ClientAliveInterval 300 ClientAliveCountMax 2 LoginGraceTime 30 MaxAuthTries 3
Firewall ด้วย UFW
UFW (Uncomplicated Firewall) เป็น Frontend ของ iptables ที่ใช้งานง่าย:
แนะนำเพิ่มเติม — หนังสือเทรดที่ SiamCafeBook
# เปิด UFW sudo ufw enable # อนุญาตเฉพาะพอร์ตที่จำเป็น sudo ufw allow 2222/tcp # SSH sudo ufw allow 80/tcp # HTTP sudo ufw allow 443/tcp # HTTPS # ปิดทุกอย่างที่เหลือ (Default) sudo ufw default deny incoming sudo ufw default allow outgoing # ดูกฎทั้งหมด sudo ufw status verbose
หลักการคือ Deny All แล้วอนุญาตเฉพาะที่จำเป็นทุกพอร์ตที่เปิดคือช่องทางโจมตีที่เป็นไปได้เปิดให้น้อยที่สุด
Fail2ban ป้องกัน Brute Force

Fail2ban ตรวจจับการพยายาม Login ที่ล้มเหลวแล้วบล็อก IP อัตโนมัติ:
เนื้อหาเกี่ยวข้อง — ดูเพิ่มเติมเรื่อง Linux eBPF XDP Load Testing Strategy
# ติดตั้ง sudo apt install fail2ban # สร้าง Config sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
ตั้งค่าใน /etc/fail2ban/jail.local:
[sshd] enabled = true port = 2222 filter = sshd logpath = /var/log/auth.log maxretry = 3 bantime = 3600 findtime = 600
Config นี้จะบล็อก IP ที่ Login ผิด 3 ครั้งใน 10 นาทีเป็นเวลา 1 ชั่วโมงสำหรับผู้โจมตีที่ดื้อดึงสามารถเพิ่ม bantime เป็น 86400 (24 ชั่วโมง) หรือใช้ bantime.increment เพื่อเพิ่มเวลาบล็อกทวีคูณ
อัพเดตระบบอย่างสม่ำเสมอ
ช่องโหว่ใหม่ถูกค้นพบทุกวันการอัพเดตระบบเป็นการป้องกันที่สำคัญที่สุด:
แนะนำเพิ่มเติม — แหล่งความรู้ Forex iCafeForex
# อัพเดตทุกวัน (Ubuntu/Debian) sudo apt update && sudo apt upgrade -y # เปิด Automatic Security Updates sudo apt install unattended-upgrades sudo dpkg-reconfigure unattended-upgrades
สำหรับ Production Server แนะนำให้เปิด Automatic Security Updates เฉพาะ Patch ด้านความปลอดภัยส่วน Major Update ทดสอบใน Staging ก่อน
เนื้อหาเกี่ยวข้อง — ทำความเข้าใจ Ceph Storage Cluster Career Development IT
จัดการผู้ใช้และสิทธิ์
หลักการ Least Privilege คือให้สิทธิ์เท่าที่จำเป็นเท่านั้น:
- สร้าง User แยกสำหรับแต่ละบริการอย่าใช้ Root รันบริการ
- จำกัด sudo Access เฉพาะคำสั่งที่จำเป็นไม่ให้ ALL=(ALL) NOPASSWD: ALL
- ตรวจสอบ User ที่มี UID 0 สม่ำเสมอต้องมีเฉพาะ Root
- ลบ User ที่ไม่ใช้แล้วทันที
- ตั้ง Password Policy: ความยาวขั้นต่ำ 12 ตัวมีอักขระพิเศษหมดอายุทุก 90 วัน
# ตรวจสอบ User ที่มี UID 0
awk -F: '($3 == "0") {print}' /etc/passwd
# ตรวจสอบ User ที่มี sudo
getent group sudo
# Lock User ที่ไม่ใช้
sudo usermod -L username
Audit และ Logging
Log เป็นหลักฐานสำคัญเมื่อเกิดเหตุการณ์ด้านความปลอดภัย:
- ตั้ง auditd สำหรับ System Audit:
sudo apt install auditd - ส่ง Log ไปยัง Central Syslog Server เพื่อป้องกันผู้โจมตีลบ Log
- Monitor ไฟล์สำคัญด้วย AIDE หรือ OSSEC
- เก็บ Log อย่างน้อย 90 วันตาม Compliance Requirements
# ติดตั้ง auditd sudo apt install auditd audispd-plugins # Monitor การเปลี่ยนแปลง /etc/passwd sudo auditctl -w /etc/passwd -p wa -k passwd_changes # ดู Audit Log sudo ausearch -k passwd_changes
Network Security
การตั้งค่า Network ที่ปลอดภัยบน Linux Server:
# ปิด IP Forwarding (ถ้าไม่ใช่ Router) echo "net.ipv4.ip_forward = 0" >> /etc/sysctl.conf # ป้องกัน SYN Flood echo "net.ipv4.tcp_syncookies = 1" >> /etc/sysctl.conf # ปิด ICMP Redirect echo "net.ipv4.conf.all.accept_redirects = 0" >> /etc/sysctl.conf # ปิด Source Routing echo "net.ipv4.conf.all.accept_source_route = 0" >> /etc/sysctl.conf # Apply sudo sysctl -p
สำหรับเครือข่ายที่ต้องการความปลอดภัยสูงควรวาง Server ไว้หลัง Firewall Hardware ศึกษาเพิ่มเติมเกี่ยวกับ Firewall Hardware สำหรับ SME สำหรับผู้ที่ใช้ Server เทรด Forex สามารถศึกษาเรื่อง VPS และ EA ได้ที่
เนื้อหาเกี่ยวข้อง — ทำความเข้าใจ คู่มือการออกแบบ Lit Element แบบหลายผู้ใช้ที่มีประสิทธิภาพ
การเข้ารหัสข้อมูล
- Disk Encryption: ใช้ LUKS สำหรับเข้ารหัส Disk ทั้งลูกป้องกันกรณีเครื่องถูกขโมย
- SSL/TLS: ใช้ Let's Encrypt สำหรับ HTTPS บังคับ TLS 1.2 ขึ้นไป
- Database Encryption: เข้ารหัสข้อมูลสำคัญใน Database เช่นรหัสผ่านข้อมูลบัตรเครดิต
- Backup Encryption: เข้ารหัส Backup ก่อนส่งไปเก็บที่อื่น
Monitoring และ Intrusion Detection
การตรวจจับการบุกรุก:
- OSSEC: Host-based IDS ที่ Monitor ไฟล์ Log การเปลี่ยนแปลงไฟล์และ Rootkit
- ClamAV: Antivirus สำหรับ Linux สแกน Malware ใน File Upload
- rkhunter: ตรวจจับ Rootkit บน Linux
- Lynis: Security Audit Tool สแกนและให้คำแนะนำในการแก้ไข
# สแกน Rootkit sudo apt install rkhunter sudo rkhunter --check # Security Audit ด้วย Lynis sudo apt install lynis sudo lynis audit system
Security Checklist สำหรับ Linux Server
- ☑ เปลี่ยนพอร์ต SSH และปิด Root Login
- ☑ ใช้ SSH Key Authentication เท่านั้น
- ☑ ตั้ง Firewall (UFW) Default Deny
- ☑ ติดตั้ง Fail2ban
- ☑ เปิด Automatic Security Updates
- ☑ ลบ User และ Service ที่ไม่ใช้
- ☑ ตั้ง auditd สำหรับ System Audit
- ☑ ส่ง Log ไป Central Syslog
- ☑ ตั้ง SSL/TLS สำหรับทุกบริการ
- ☑ Backup และเข้ารหัส Backup
- ☑ สแกน Rootkit และ Malware สม่ำเสมอ
- ☑ Security Audit ด้วย Lynis ทุกเดือน
Linux ปลอดภัยกว่า Windows จริงหรือ?
Linux มีสถาปัตยกรรมที่ปลอดภัยกว่า Windows ในหลายด้านแต่ไม่ได้หมายความว่าปลอดภัย 100% Linux Server ที่ไม่ได้ Harden อาจถูกแฮกได้ง่ายกว่า Windows ที่ตั้งค่าดีความปลอดภัยขึ้นอยู่กับการตั้งค่ามากกว่าระบบปฏิบัติการ





