การเชื่อมต่อ Git กับ GitHub ด้วย SSH Key แทน HTTPS ช่วยให้คุณไม่ต้องป้อน password ทุกครั้งที่ push หรือ pull โค้ด สำคัญมากสำหรับทีมที่ใช้ VPS หรือระบบ automation ที่ต้อง git pull อัตโนมัติ ทั้งนี้ ผู้ให้บริการโฮสติ้ง (DE) มีบริการ Cloud VPS ที่เหมาะสำหรับการตั้งค่าระบบ deployment อัตโนมัติแบบนี้
ทำไมต้องใช้ SSH Key แทน HTTPS?
- ไม่ต้องป้อน password ทุกครั้ง ทำให้สะดวกในการ automate script
- GitHub ยกเลิกรองรับ password authentication ไปแล้ว ต้องใช้ Personal Access Token หรือ SSH
- ปลอดภัยกว่าเพราะใช้ public/private key cryptography แทนการเก็บ password ในไฟล์
- เหมาะสำหรับ automation script, CI/CD pipeline และ webhook integration บน server
- สามารถใช้เดียว key สำหรับหลาย repository ได้โดยไม่ต้องเปลี่ยน authentication credential
ข้อแตกต่างระหว่าง SSH Key กับ Personal Access Token
SSH Key เป็นการใช้ cryptographic key pair ในการยืนยันตัวตนแบบไม่มีการส่ง password ในระหว่างการสื่อสาร ส่วน Personal Access Token (PAT) เป็นการสร้าง token ที่เหมือน password เพื่อใช้กับ API หรือ HTTPS clone หากต้องการความปลอดภัยสูงสุด ลองศึกษาเพิ่มเติมจากบทความ เปรียบเทียบระหว่าง Personal Access Token และ SSH Key เพื่อเลือกวิธีที่เหมาะสมที่สุดกับสถานการณ์ของคุณ
ขั้นตอนตั้งค่า SSH Key บน VPS
ขั้นที่ 1: สร้าง SSH Key Pair
# SSH เข้าไป VPS
ssh user@your-vps-ip
# ตรวจสอบว่ามี key อยู่แล้วหรือไม่
ls ~/.ssh/
# สร้าง SSH key (ED25519 แนะนำ)
ssh-keygen -t ed25519 -C "[email protected]"
# ถ้าเครื่องเก่าไม่รองรับใช้ RSA
ssh-keygen -t rsa -b 4096 -C "[email protected]"
# จะถามที่เก็บ key (กด Enter ยอมรับ default)
# จะถามรหัส passphrase (ถ้าต้องการให้พิมพ์ ถ้าไม่ต้องกด Enter ข้าม)
เทคนิคสำคัญคือการเลือกวิธีการยืนยันตัวตนที่ถูกต้อง บทความ HTTPS vs SSH สำหรับการเชื่อมต่อ Git Remote อธิบายรายละเอียดเปรียบเทียบให้อย่างละเอียด
ขั้นที่ 2: คัดลอก Public Key
cat ~/.ssh/id_ed25519.pub
# ผลลัพธ์จะได้ประมาณ:
# ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA... [email protected]
# Copy เนื้อหาทั้งหมดเพื่อใช้ในขั้นตอนถัดไป
# บน macOS:
cat ~/.ssh/id_ed25519.pub | pbcopy
# บน Linux:
cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard
ขั้นที่ 3: เพิ่ม Public Key ใน GitHub
- ไปที่ GitHub.com → Settings → SSH and GPG keys → New SSH key
- ตั้งชื่อ (Title) เช่น “My VPS Server” หรือ “Production Server VPS”
- วางเนื้อหาของ public key ลงในช่อง Key (ทั้งหมด เริ่มจาก ssh-ed25519 ถึง email)
- เลือก Key type เป็น “Authentication Key” (สำหรับการ push/pull)
- กด Add SSH key เสร็จ
ขั้นที่ 4: ทดสอบการเชื่อมต่อ
ssh -T [email protected]
# ตอบกลับที่ถูกต้อง:
# Hi username! You've successfully authenticated, but GitHub does not provide shell access.
# ถ้าเจอ error ให้ลอง:
ssh -vvv -T [email protected] # Debug mode
# ตรวจสอบว่า ssh-agent ทำงานหรือไม่
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
ขั้นที่ 5: ใช้ SSH URL ในการ Clone
# ใช้ SSH URL (ไม่ใช้ HTTPS)
git clone [email protected]:username/repository.git
# เปลี่ยน remote URL จาก HTTPS เป็น SSH สำหรับ repo ที่มีอยู่แล้ว
git remote set-url origin [email protected]:username/repository.git
# ตรวจสอบว่า remote ชี้ไปยังที่ถูกต้อง
git remote -v
การตั้งค่า SSH Config สำหรับหลาย GitHub Account
ถ้ามีหลาย account หรือต้องการแยก personal กับ work account สร้างไฟล์ ~/.ssh/config เพื่อจัดการแต่ละ key:
# ~/.ssh/config
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
AddKeysToAgent yes
IdentitiesOnly yes
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
AddKeysToAgent yes
IdentitiesOnly yes
# ใช้ host alias ในการ clone
git clone github-personal:username/my-personal-repo.git
git clone github-work:company/work-repo.git
# ตรวจสอบว่าใช้ account ที่ถูกต้อง
cd my-personal-repo
git config user.email
git config user.name
การ Setup Automated Deployment ด้วย SSH Key
เมื่อตั้งค่า SSH Key ตรงแล้ว สามารถสร้าง deployment script ที่ pull โค้ดอัตโนมัติจาก GitHub ได้ โดยไม่ต้องระบุ password บนเซิร์ฟเวอร์ นี่เป็นขั้นตอนสำคัญสำหรับการสร้าง CI/CD pipeline ลองอ่านบทความ Setup Git Auto Deployment บน VPS ด้วย SSH สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับการอัตโนมัติการ deploy
สรุป
การตั้งค่า SSH Key สำหรับ Git บน VPS เป็นสิ่งจำเป็นสำหรับการทำงานเชิงอัตโนมัติ ช่วยให้ CI/CD และ deployment script ทำงานได้โดยไม่ต้องป้อน password ขั้นตอนหลักคือสร้าง key pair, เพิ่ม public key ใน GitHub, ทดสอบการเชื่อมต่อ และใช้ SSH URL ในการ clone ทีม DevOps โดยทั่วไปจะใช้วิธีนี้เป็นมาตรฐานในทุกโปรเจกต์ที่ต้องการความปลอดภัยและเอกชนในการเชื่อมต่อระหว่าง server กับ repository ต้นทางบนเคลาว์
