SSH (Secure Shell) เป็นโปรโตคอลหลักที่ใช้เข้าถึง Server จากระยะไกล การตั้งค่า SSH ให้ปลอดภัยจึงเป็นสิ่งสำคัญอย่างยิ่ง เพราะผู้โจมตีมักใช้ SSH เป็นช่องทางเข้าถึง Server ระบบ
การตั้งค่า SSH Key Authentication
การใช้ SSH Key แทน Password เป็นวิธีที่ปลอดภัยกว่ามาก เนื่องจากไม่สามารถ Brute Force ได้:
# สร้าง SSH Key Pair บนเครื่อง Client
ssh-keygen -t ed25519 -C "[email protected]"
# หรือใช้ RSA 4096 bit
ssh-keygen -t rsa -b 4096 -C "[email protected]"
# คัดลอก Public Key ไปยัง Server
ssh-copy-id -i ~/.ssh/id_ed25519.pub username@server_ip
# หรือ Copy ด้วยตนเอง
cat ~/.ssh/id_ed25519.pub | ssh username@server_ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
การตั้งค่า SSH (/etc/ssh/sshd_config)
การปรับ Configuration หลักสำหรับเสริมความปลอดภัย SSH:
# /etc/ssh/sshd_config
# เปลี่ยน Port จาก 22 (ลด Automated Scanning)
Port 2222
# ปิดการ Login ด้วย Root
PermitRootLogin no
# ปิด Password Authentication (ใช้ Key เท่านั้น)
PasswordAuthentication no
ChallengeResponseAuthentication no
# เปิด Key Authentication
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
# กำหนด User ที่อนุญาตให้ SSH
AllowUsers deploy admin
# Limit จำนวนครั้ง Authentication
MaxAuthTries 3
# Timeout สำหรับ Session ที่ไม่เคลื่อนไหว
ClientAliveInterval 300
ClientAliveCountMax 2
# ปิดสิ่งที่ไม่ใช้
X11Forwarding no
AllowAgentForwarding no
AllowTcpForwarding no
# แสดง Login Banner
Banner /etc/issue.net
# Restart SSH Service หลังแก้ไข
# (อย่า Logout ก่อน Test เสมอ)
sudo sshd -t # ตรวจสอบ Syntax
sudo systemctl restart sshd
การติดตั้งและตั้งค่า Fail2ban
Fail2ban จะ Block IP ที่พยายาม Login ผิดหลายครั้งอัตโนมัติ:
sudo apt install fail2ban
# สร้าง /etc/fail2ban/jail.local
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3
destemail = [email protected]
action = %(action_mwl)s
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
# Restart Fail2ban
sudo systemctl restart fail2ban
# ตรวจสอบสถานะ
sudo fail2ban-client status sshd
การใช้ SSH Jump Host / Bastion Host
ใน Production Environment ควรใช้ Bastion Host เป็นจุดเข้าถึงเดียวสำหรับทุก Server:
# ตั้งค่าใน ~/.ssh/config บนเครื่อง Client
Host bastion
HostName bastion.yourdomain.com
User deploy
IdentityFile ~/.ssh/bastion_key
Port 2222
Host internal-server
HostName 10.0.1.50
User deploy
IdentityFile ~/.ssh/internal_key
ProxyJump bastion
# เชื่อมต่อ internal-server ผ่าน Bastion
ssh internal-server
SSH Audit และการตรวจสอบ
# ตรวจสอบการล็อกอินสำเร็จ
sudo grep "Accepted" /var/log/auth.log
# ตรวจสอบการล็อกอินผิด
sudo grep "Failed password" /var/log/auth.log
# ดู IP ที่ Fail2ban บล็อกอยู่
sudo fail2ban-client status sshd
# ใช้ ssh-audit Tool ตรวจสอบ Configuration
pip install ssh-audit
ssh-audit localhost
SSH Security Checklist
| รายการ | คำอธิบาย | ความสำคัญ |
|---|---|---|
| ใช้ SSH Key Auth | แทน Password | สูงมาก |
| ปิด Root Login | PermitRootLogin no | สูงมาก |
| เปลี่ยน Port | จาก 22 เป็นอื่น | ปานกลาง |
| ติดตั้ง Fail2ban | ป้องกัน Brute Force | สูง |
| จำกัด AllowUsers | ระบุ User ที่อนุญาต | สูง |
| ใช้ Bastion Host | จุดเข้าถึงจุดเดียว | สูง |
| ติดตาม Auth Logs | ดู Login Events | สูง |
สรุป
SSH Security เป็นสิ่งแรกที่ควรตั้งค่าหลังจากรับเซิร์ฟเวอร์ใหม่ การใช้ SSH Key ร่วมกับการปิด Password Authentication และติดตั้ง Fail2ban จะช่วยลดความเสี่ยงได้อย่างมีนัยสำคัญ สำหรับ Cloud VPS ของ Dot Enterprise รองรับ SSH Key Authentication และ Security Features ครบครันตั้งแต่เริ่มต้นใช้งาน

