
วิธีสร้าง SSH Key บน Linux และการใช้งานกับ GitHub และเซิร์ฟเวอร์
อัพเดทเมื่อ: 15 พ.ค. 2025
ผู้เขียน: Saprayworld
- SSH Key
- RSA
- Linux
- Server
แนะนำวิธีสร้าง SSH Key และการนำไปใช้งานบน Linux เพื่อเชื่อมต่อ GitHub, GitLab และเซิร์ฟเวอร์แบบปลอดภัย โดยไม่ต้องใช้รหัสผ่าน
🔐 สร้าง SSH Key
-
เปิด Terminal หรือ Git Bash
-
ใช้คำสั่งต่อไปนี้ในการสร้าง key ใหม่:
ssh-keygen -t ed25519 -C "your_email@example.com"
หากเครื่องคุณไม่รองรับ ed25519 สามารถใช้ rsa แทนได้:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
-
ระบบจะถามว่าจะบันทึกไฟล์ไว้ที่ไหน กด Enter เพื่อใช้ค่ามาตรฐาน:
Enter file in which to save the key (/home/youruser/.ssh/id_ed25519):
-
จากนั้นสามารถตั้ง passphrase เพื่อความปลอดภัยเพิ่มเติม หรือกด Enter ข้ามไปก็ได้
เมื่อเสร็จแล้ว คุณจะได้ไฟล์ 2 ไฟล์:
- 🔑
id_ed25519
(private key ห้ามแชร์) - 📄
id_ed25519.pub
(public key ใช้สำหรับส่งให้ผู้อื่นหรือระบบปลายทาง)
📌 การนำ SSH Key ไปใช้งาน
กรณีใช้งานกับ GitHub / GitLab
-
คัดลอก Public Key ด้วยคำสั่ง:
cat ~/.ssh/id_ed25519.pub
-
เข้า GitHub หรือ GitLab แล้วไปที่:
Settings
>SSH and GPG keys
>New SSH key
- วาง key ที่ได้ลงไป แล้วกด
Add
กรณีใช้งานกับเซิร์ฟเวอร์ (Linux VPS)
-
ใช้คำสั่งต่อไปนี้เพื่อคัดลอก public key ไปยังเซิร์ฟเวอร์:
ssh-copy-id user@your.server.ip
หรือหาก ssh-copy-id ไม่มีในระบบ:
cat ~/.ssh/id_ed25519.pub | ssh user@your.server.ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
-
ทดสอบการเชื่อมต่อ:
ssh user@your.server.ip
ถ้าทุกอย่างถูกต้อง จะสามารถเข้าได้โดยไม่ต้องใส่รหัสผ่าน
🛠️ ทิปส์เพิ่มเติม
-
หากมีหลาย key ให้กำหนดไฟล์ key ที่จะใช้ด้วย:
ssh -i ~/.ssh/your_custom_key user@your.server.ip
-
หรือใช้ SSH Config สำหรับจัดการหลาย key:
# ~/.ssh/config Host myserver HostName your.server.ip User yourusername IdentityFile ~/.ssh/id_ed25519
แล้วใช้:
ssh myserver
หวังว่าคู่มือนี้จะช่วยให้คุณตั้งค่า SSH key ได้อย่างราบรื่น 💻🔐
Saprayworld ขอเป็นกำลังใจให้ในการลุย DevOps ต่อไปนะ 🚀