DocsEnterprise RunbooksSSH — Host Key Recovery

SSH — Safe Host Key Change Recovery

OpenSSH บน Linux/macOS/Windows · Security · Networking

Official Reference: https://man.openbsd.org/ssh_config.5


Executive Summary (EN): Security-first recovery runbook for OpenSSH REMOTE HOST IDENTIFICATION HAS CHANGED warnings, emphasizing out-of-band SHA256 fingerprint verification (ssh-keygen -lf) before surgically pruning stale entries with ssh-keygen -R.

Operational Note: Sanitized enterprise reference runbook cross-checked against OpenSSH documentation — validate in a staging/lab environment before production execution.

1. อาการและความหมาย

SSH client จำ public host key ของ server ไว้ใน known_hosts. เมื่อ key เปลี่ยน อาจเกิดจาก reinstall/rotation, IP ถูกนำไปใช้ใหม่, DNS ผิด หรือการโจมตี man-in-the-middle. Host key ต่างจาก user authentication key

ห้ามลบ known_hosts ทั้งไฟล์หรือใช้ StrictHostKeyChecking=no เพื่อให้เข้าได้ทันที ต้องยืนยันตัวตน server ก่อน

2. เก็บหลักฐานและตรวจสอบ

บันทึก hostname/IP, port, fingerprint ที่แจ้งเตือน, เวลา และ change ticket. ตรวจ DNS และ inventory ของเครื่องที่ควรเชื่อมต่อ

ssh-keygen -F server.example.com
ssh-keygen -F '[server.example.com]:2222'
ssh -G server.example.com

ถ้าเป็น port 22 ใช้รูปแบบ hostname ปกติ; หาก non-default port ใช้ [hostname]:port. ssh -G แสดง effective configuration รวมถึง known_hosts path และ proxy settings

3. ยืนยัน fingerprint จากช่องทางอิสระ

เข้า server ผ่าน console ที่เชื่อถือได้ หรือให้ผู้ดูแลระบบตรวจ host public key ของเครื่องเป้าหมาย. ตัวอย่าง OpenSSH Linux:

sudo ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub
sudo ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub

เปรียบเทียบ algorithm และ SHA256 fingerprint ให้ตรงกับที่ client แจ้ง. ถ้า server ใช้ host key path อื่น ให้ตรวจ sshd configuration. ssh-keyscan ดึง key ได้แต่ไม่พิสูจน์ตัวตนของ server. หากไม่ได้รับการยืนยัน หยุดและ escalate security incident

4. อัปเดตเฉพาะ entry ที่ได้รับอนุมัติ

สำรอง known_hosts ก่อน (Linux/macOS):

cp -p ~/.ssh/known_hosts ~/.ssh/known_hosts.prechange
ssh-keygen -R server.example.com
# กรณี port 2222 ใช้คำสั่งนี้แทน
ssh-keygen -R '[server.example.com]:2222'

ถ้าต้องลบ entry ของ IP ให้ตรวจและลบแยกเฉพาะ IP ที่ได้รับอนุมัติ. Windows PowerShell ใช้ ssh-keygen เช่นเดียวกัน; ไฟล์ปกติอยู่ที่ $HOME\.ssh\known_hosts และสำรองด้วย Copy-Item. ถ้าใช้ custom file ให้ระบุ -f ตาม path จริง

5. เชื่อมต่ออีกครั้ง

ssh -o StrictHostKeyChecking=ask user@server.example.com
# non-default port
ssh -p 2222 -o StrictHostKeyChecking=ask user@server.example.com

กดยืนยันเฉพาะเมื่อ fingerprint ตรงกับที่ยืนยันในขั้น 3 เท่านั้น. สำหรับ automation ให้ provision host key ที่ยืนยันแล้วและใช้ strict checking ตาม policy ไม่ใช้ accept-new เพื่อยอมรับ key ที่เปลี่ยน

6. Verification / Rollback

ตรวจ ssh-keygen -F, login เข้าเครื่องที่ถูกต้อง, hostname และ change record. หาก fingerprint ยังไม่ตรง ให้หยุด ไม่พยายาม bypass. หากลบผิด entry ให้คืน backup known_hosts โดยตรวจว่าไม่มีการเปลี่ยนรายการอื่นหลัง backup ก่อน. สำหรับ key ที่ rotate จริงไม่ต้องคืน key เก่า

7. Lab

สร้าง SSH server บน VM แยก บันทึก fingerprint จาก console แล้วเชื่อมต่อ. จำลอง rebuild ใน VM disposable และทำตาม runbook; ทดสอบว่าการไม่ยืนยัน fingerprint ทำให้หยุดงาน

แหล่งอ้างอิงทางการ (Official References)