SSH Key-based Authentication Setup บน Linux

การ Login ผ่าน SSH ด้วย Password เป็นวิธีที่สะดวกแต่มีความเสี่ยงสูง เพราะ Password สามารถถูกเดา, Brute Force หรือถูกขโมยได้ SSH Key-based Authentication เป็นวิธีที่ปลอดภัยกว่ามาก โดยใช้คู่กุญแจ Public/Private Key แทน Password ทำให้แม้แต่ Brute Force Attack ก็ไม่สามารถเจาะระบบได้

บทความนี้อธิบายหลักการทำงานของ SSH Key, วิธีสร้างและติดตั้ง Key, การตั้งค่า Server และ Client รวมถึง Best Practices ที่ใช้ใน Production จริง

SSH Key ทำงานอย่างไร

SSH Key ทำงานบนหลักการ Asymmetric Cryptography โดยใช้คู่กุญแจ 2 ดอก ได้แก่ Private Key ที่เก็บบน Client (ห้ามแชร์) และ Public Key ที่ติดตั้งบน Server เมื่อ Client ส่ง Connection มา Server จะสร้าง Challenge ที่เข้ารหัสด้วย Public Key กลับไป Client ต้องใช้ Private Key เพื่อถอดรหัส Challenge นั้นแล้วส่งหลักฐานกลับ Server จึงยืนยันตัวตนได้โดยไม่มีการส่ง Password ผ่านเครือข่ายเลย

สร้าง SSH Key Pair

ใช้คำสั่ง ssh-keygen บนเครื่อง Client เพื่อสร้าง Key Pair โดย Key ที่แนะนำในปัจจุบันคือ ED25519 ซึ่งมีความปลอดภัยสูงและขนาดเล็กกว่า RSA

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

# สร้าง RSA 4096-bit Key (ใช้เมื่อ Server เก่าไม่รองรับ ED25519)
ssh-keygen -t rsa -b 4096 -C "[email protected]"

# ระบุไฟล์ปลายทางเอง (เหมาะสำหรับหลาย Key)
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_deploy -C "deploy@prod"

หลังรันคำสั่ง จะได้ไฟล์ 2 ไฟล์ คือ ~/.ssh/id_ed25519 (Private Key — ห้ามแชร์เด็ดขาด) และ ~/.ssh/id_ed25519.pub (Public Key — ใช้ติดตั้งบน Server)

Passphrase — การป้องกัน Private Key

ระหว่างสร้าง Key ระบบจะถาม Passphrase เพื่อเข้ารหัส Private Key บน Disk การตั้ง Passphrase ทำให้แม้ไฟล์ Private Key ถูกขโมยไป ผู้ขโมยก็ยังใช้งานไม่ได้หากไม่รู้ Passphrase สำหรับการใช้งานแบบ Automation ที่ต้องการ Login โดยไม่ต้องกดอะไร สามารถใช้ ssh-agent เพื่อ Cache Passphrase ไว้ในระหว่าง Session

ติดตั้ง Public Key บน Server

Public Key ต้องถูกเพิ่มเข้าไปในไฟล์ ~/.ssh/authorized_keys ของ User บน Server ซึ่งทำได้หลายวิธี

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

# วิธีที่ 2: Copy ด้วยมือ
cat ~/.ssh/id_ed25519.pub | ssh user@server-ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

# วิธีที่ 3: ทำบน Server โดยตรง
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "ssh-ed25519 AAAA... [email protected]" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Permission ของ ~/.ssh และ authorized_keys ต้องถูกต้องเสมอ ไม่เช่นนั้น SSH จะปฏิเสธ Key โดยไม่บอกสาเหตุ

ทดสอบการ Login ด้วย Key

# Login โดยระบุ Key file เอง
ssh -i ~/.ssh/id_ed25519 user@server-ip

# Login แบบปกติ (ถ้า Key ชื่อ default id_ed25519 จะเลือกให้อัตโนมัติ)
ssh user@server-ip

# Debug ถ้า Key ไม่ทำงาน
ssh -vvv user@server-ip

ปิด Password Login บน Server (สำคัญมาก)

หลังจาก Key ใช้งานได้แล้ว ขั้นตอนสำคัญที่สุดคือการปิด Password Authentication บน SSH Server เพื่อป้องกัน Brute Force Attack แก้ไขไฟล์ /etc/ssh/sshd_config ดังนี้

# แก้ไข /etc/ssh/sshd_config
PasswordAuthentication no
PubkeyAuthentication yes
PermitRootLogin no
AuthorizedKeysFile .ssh/authorized_keys

# Restart SSH service หลังแก้ไข
sudo systemctl restart sshd

# ทดสอบ config ก่อน restart (ป้องกัน syntax error)
sudo sshd -t

คำเตือน: ก่อนปิด Password Authentication ต้องทดสอบว่า SSH Key Login ใช้งานได้จริงใน Session อื่นก่อน มิฉะนั้นอาจถูก Lock ออกจาก Server

SSH Config File — จัดการหลาย Server

ไฟล์ ~/.ssh/config ช่วยให้ตั้งค่า Connection แยกรายละเอียดสำหรับแต่ละ Server ทำให้ไม่ต้องจำ IP, Port และ Key ของทุก Server

# ~/.ssh/config

Host prod-web
    HostName 203.0.113.10
    User deploy
    IdentityFile ~/.ssh/id_ed25519_deploy
    Port 22

Host staging
    HostName 203.0.113.20
    User ubuntu
    IdentityFile ~/.ssh/id_ed25519
    Port 2222

# ใช้งาน
ssh prod-web
ssh staging

ssh-agent — Cache Passphrase

ถ้า Private Key มี Passphrase และต้องการไม่ให้ถาม Passphrase ทุกครั้ง ใช้ ssh-agent เพื่อ Cache ไว้ใน Memory ตลอด Session

# เริ่ม ssh-agent
eval "$(ssh-agent -s)"

# เพิ่ม Key เข้า agent (จะถาม Passphrase 1 ครั้ง)
ssh-add ~/.ssh/id_ed25519

# ดู Key ที่โหลดอยู่ใน agent
ssh-add -l

# บน macOS — บันทึก Passphrase ใน Keychain
ssh-add --apple-use-keychain ~/.ssh/id_ed25519

Best Practices สำหรับ SSH Key

  • ใช้ ED25519 — ปลอดภัยกว่า RSA 2048 และขนาดเล็กกว่า ใช้ RSA 4096 เฉพาะกรณีที่จำเป็นต้อง Compatibility กับระบบเก่า
  • ตั้ง Passphrase เสมอ — ป้องกันการนำ Private Key ที่ขโมยไปใช้โดยไม่รู้ Passphrase
  • Key Per Device/Purpose — สร้าง Key แยกสำหรับแต่ละเครื่องหรือ Use Case เพื่อ Revoke ได้เฉพาะจุด
  • ปิด PasswordAuthentication — หลังติดตั้ง Key สำเร็จ ปิด Password Login ทันที
  • ตรวจสอบ authorized_keys — Review เป็นระยะว่ามี Key ที่ไม่ได้ใช้แล้วหรือไม่รู้จักใน authorized_keys
  • Backup Private Key อย่างปลอดภัย — เก็บ Private Key ในที่ปลอดภัย ถ้าหายจะต้องสร้างใหม่และ Update ทุก Server

สรุป

SSH Key Authentication ปลอดภัยกว่า Password อย่างมากในทุกมิติ ขั้นตอนหลักคือสร้าง Key ด้วย ssh-keygen, คัดลอก Public Key ไปยัง Server ด้วย ssh-copy-id, ทดสอบ Login ให้ผ่านก่อน แล้วจึงปิด PasswordAuthentication ใน sshd_config การใช้ไฟล์ ~/.ssh/config ช่วยจัดการหลาย Server ได้สะดวก และ ssh-agent ช่วยลดการกรอก Passphrase ซ้ำซ้อน

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

การฝึก SSH Key Authentication ต้องการ Server ที่มีสิทธิ์แก้ไข sshd_config ได้เต็มที่ Cloud VPS ของ DE ให้สิทธิ์ root ครบถ้วน เหมาะสำหรับทดลองติดตั้ง SSH Key, ปิด Password Login และฝึก Security Hardening บน Server จริง

หากต้องการโฮสต์เว็บไซต์โดยไม่ต้องจัดการ SSH Configuration เอง Cloud Hosting ของ DE มีระบบรักษาความปลอดภัยที่จัดการให้อัตโนมัติ พร้อม Control Panel ที่ใช้งานง่าย