SSH Configuration Best Practices บน Linux

SSH (Secure Shell) เป็นโปรโตคอลหลักที่ System Administrator ใช้เข้าถึง Server แต่การติดตั้งค่าเริ่มต้นมักไม่ได้ Optimize เพื่อความปลอดภัยสูงสุด การปรับ sshd_config อย่างถูกต้องเป็นหนึ่งในขั้นตอนแรกที่ต้องทำเมื่อ Setup Server ใหม่ ก่อนที่จะ Expose Service ใดๆ บน Internet

บทความนี้รวบรวม SSH Configuration Best Practices ที่ Admin ควรตั้งค่าทุก Server เพื่อลด Attack Surface และเพิ่มความปลอดภัยให้กับระบบ

ไฟล์ Configuration หลัก

การตั้งค่า SSH Server ทั้งหมดอยู่ที่ /etc/ssh/sshd_config ส่วนการตั้งค่า SSH Client (สำหรับ outgoing connections) อยู่ที่ /etc/ssh/ssh_config หรือ ~/.ssh/config ทุกครั้งที่แก้ไข sshd_config ควรทดสอบ Syntax ก่อน Reload เสมอ

# ทดสอบ syntax ก่อน restart (สำคัญมาก)
sudo sshd -t

# Reload configuration โดยไม่ต้องตัด Connection ที่มีอยู่
sudo systemctl reload sshd

# หรือ restart (ตัด Connection ทั้งหมด)
sudo systemctl restart sshd

Authentication Settings

ส่วนที่สำคัญที่สุดคือการ Disable Authentication Method ที่ไม่ปลอดภัย และ Enable เฉพาะที่จำเป็น

# /etc/ssh/sshd_config

# ปิด Password Authentication — บังคับใช้ Key เท่านั้น
PasswordAuthentication no

# เปิด Public Key Authentication
PubkeyAuthentication yes

# ปิด root Login โดยตรง
PermitRootLogin no

# ปิด Empty Password (ป้องกัน account ที่ไม่มี password)
PermitEmptyPasswords no

# ปิด Challenge-Response Authentication
ChallengeResponseAuthentication no

# ปิด PAM Authentication (ถ้าไม่ต้องการ)
UsePAM no

Port และ Network Settings

การเปลี่ยน Port จาก 22 เป็น Port อื่นช่วยลด Noise จาก Automated Scanner แต่ไม่ใช่มาตรการรักษาความปลอดภัยหลัก เพราะ Port Scanner สามารถค้นหา Port ที่เปิดอยู่ได้อยู่ดี อย่างไรก็ตามช่วยลด Log ที่ไม่จำเป็นได้มาก

# เปลี่ยน Port (ตัวอย่างใช้ 2222)
Port 2222

# รับ Connection เฉพาะ IPv4 (ถ้าไม่ใช้ IPv6)
AddressFamily inet

# รับ Connection เฉพาะ Interface ที่ต้องการ
ListenAddress 0.0.0.0

Connection และ Session Limits

การจำกัดจำนวน Connection และ Session ช่วยป้องกัน Resource Exhaustion Attack และลด Attack Surface

# จำนวน Authentication Attempt สูงสุดต่อ Connection
MaxAuthTries 3

# จำนวน Session สูงสุดพร้อมกัน
MaxSessions 5

# จำนวน Connection ที่ยังไม่ได้ Authenticate พร้อมกัน
# รูปแบบ: start:rate:full
MaxStartups 10:30:100

# Timeout สำหรับ Connection ที่ไม่มี Activity
ClientAliveInterval 300
ClientAliveCountMax 2

# Login Timeout (วินาที)
LoginGraceTime 30

จำกัดสิทธิ์การเข้าถึง

สามารถกำหนดได้ว่า User หรือ Group ใดบ้างที่อนุญาตให้ Login ผ่าน SSH ซึ่งเป็นอีกชั้นของการป้องกัน

# อนุญาตเฉพาะ User ที่ระบุ
AllowUsers alice bob deploy

# อนุญาตเฉพาะ Group ที่ระบุ
AllowGroups sshusers

# ห้าม User ที่ระบุ
DenyUsers baduser tempuser

# ห้าม Group ที่ระบุ
DenyGroups noremote

Logging และ Monitoring

การตั้งค่า Log Level ที่เหมาะสมช่วยให้ตรวจจับการโจมตีและ Debug ปัญหาได้

# ระดับ Log — INFO เพียงพอสำหรับ Production
# VERBOSE เพิ่ม fingerprint ของ Key ที่ใช้ Login
LogLevel VERBOSE

# ดู Authentication log
sudo journalctl -u sshd -f
sudo tail -f /var/log/auth.log      # Ubuntu/Debian
sudo tail -f /var/log/secure        # RHEL/CentOS

ตัวอย่าง sshd_config ที่ Hardened

นี่คือตัวอย่าง Configuration ที่รวบ Best Practices ข้างต้นทั้งหมด เหมาะสำหรับ Server Production ที่รับ Connection จาก Internet

# /etc/ssh/sshd_config — Hardened Configuration

# Network
Port 22
AddressFamily inet
ListenAddress 0.0.0.0

# Authentication
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
UsePAM yes

# Limits
MaxAuthTries 3
MaxSessions 5
MaxStartups 10:30:100
LoginGraceTime 30

# Keepalive
ClientAliveInterval 300
ClientAliveCountMax 2

# Logging
LogLevel VERBOSE
SyslogFacility AUTH

# Misc
PrintLastLog yes
Banner /etc/ssh/banner.txt

Fail2ban — ป้องกัน Brute Force

แม้จะปิด Password Authentication แล้ว การติดตั้ง Fail2ban ยังเป็นประโยชน์ เพราะจะ Block IP ที่พยายาม Login ผิดซ้ำๆ ลด Noise ใน Log และประหยัด Resource

# ติดตั้ง fail2ban
sudo apt install fail2ban -y         # Ubuntu/Debian
sudo dnf install fail2ban -y         # RHEL/CentOS

# ตั้งค่า SSH jail ใน /etc/fail2ban/jail.local
[sshd]
enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 5
bantime  = 3600
findtime = 600

# เริ่ม service
sudo systemctl enable --now fail2ban

# ดูสถานะ
sudo fail2ban-client status sshd

สรุป

การ Harden SSH Configuration ประกอบด้วยการปิด Password Authentication, ปิด root Login, จำกัด Connection Attempts และ Timeout, กำหนด AllowUsers/AllowGroups, และเปิด Logging ที่เพียงพอ การทำทั้งหมดนี้ร่วมกับ SSH Key Authentication และ Fail2ban ทำให้ SSH เป็น Attack Vector ที่โจมตีได้ยากมาก ก่อนแก้ไขทุกครั้งให้ใช้ sshd -t ตรวจ Syntax เสมอ

แนะนำบริการ DE

การทดสอบและฝึก SSH Hardening ต้องการ Server ที่สามารถแก้ไข Configuration ได้เต็มที่ Cloud VPS ของ DE ให้สิทธิ์ root สมบูรณ์ เหมาะสำหรับทดลอง sshd_config, ติดตั้ง Fail2ban และฝึก Security Best Practices บน Server จริง

สำหรับผู้ที่ต้องการโฮสต์เว็บไซต์โดยไม่ต้องกังวลเรื่องการ Harden SSH Cloud Hosting ของ DE มีระบบความปลอดภัยที่จัดการให้ครบถ้วน รองรับการใช้งานได้ทันที