
Cấu hình SSH key cho nhiều tài khoản GitHub
/ 4 min read
Table of Contents
Lần đầu setup laptop mới, tôi muốn cả tài khoản chính lẫn tài khoản phụ (dùng cho mấy repo thử nghiệm) cùng chạy được trên một máy. Clone repo thứ nhất bình thường. Clone repo thứ hai — Permission denied (publickey). SSH không biết phải dùng key nào vì cả hai tài khoản đều trỏ về git@github.com.
Giải pháp là tạo nhiều SSH key và đặt alias trong ~/.ssh/config để mỗi tài khoản dùng đúng key của nó.
Tạo SSH key cho từng tài khoản
# Xem có key cũ chưa, tránh đặt trùng tên rồi ghi đèls -al ~/.ssh
# Tạo key personalssh-keygen -t ed25519 -C "personal@email.com" -f ~/.ssh/id_ed25519_github_personal
# Tạo key workssh-keygen -t ed25519 -C "work@company.com" -f ~/.ssh/id_ed25519_github_workNên đặt passphrase cho key. --apple-use-keychain sẽ lưu passphrase vào Keychain, không phải gõ lại mỗi lần — tiện như không passphrase nhưng vẫn an toàn nếu mất máy.
Dùng ed25519 thay vì rsa vì key ngắn hơn, ký nhanh hơn, và là chuẩn GitHub khuyến nghị hiện nay.
Nạp key vào ssh-agent
eval "$(ssh-agent -s)"ssh-add --apple-use-keychain ~/.ssh/id_ed25519_github_personalssh-add --apple-use-keychain ~/.ssh/id_ed25519_github_workKiểm tra key đã nạp:
ssh-add -lCấu hình ~/.ssh/config
Host * UseKeychain yes AddKeysToAgent yes
# Personal accountHost github.com-personal HostName github.com User git IdentityFile ~/.ssh/id_ed25519_github_personal
# Work accountHost github.com-work HostName github.com User git IdentityFile ~/.ssh/id_ed25519_github_workQuan trọng: Host ở đây là alias (github.com-personal / github.com-work). Khi clone repo phải dùng alias tương ứng.
Thêm public key vào GitHub
Làm lần lượt từng key (nếu chạy cả 2 lệnh pbcopy liên tiếp thì chỉ key cuối còn trong clipboard):
# 1. Copy key personalpbcopy < ~/.ssh/id_ed25519_github_personal.pubVào tài khoản personal trên GitHub → avatar → Settings → SSH and GPG keys → New SSH key → dán key vừa copy.
# 2. Copy key workpbcopy < ~/.ssh/id_ed25519_github_work.pubVào tài khoản work → làm tương tự.
Kiểm tra kết nối
ssh -T git@github.com-personalssh -T git@github.com-workNếu thành công sẽ hiển thị:
Hi <username>! You've successfully authenticated...Clone repo đúng alias
# Repo cá nhângit clone git@github.com-personal:your-username/your-repo.git
# Repo workgit clone git@github.com-work:company/repo.gitNếu repo đã clone bằng URL sai:
git remote set-url origin git@github.com-work:company/repo.gitNguyên tắc dễ nhớ: không bao giờ dùng git@github.com:... mặc định nếu trên máy có nhiều account.
Cấu hình Git user/email tự động
Phần này dùng includeIf để mỗi repo tự dùng đúng email — tránh trường hợp commit nhầm email work vào repo personal (hoặc ngược lại, càng nguy hiểm).
File ~/.gitconfig (mặc định dùng personal):
[user] name = Your Personal Name email = personal@email.com
[includeIf "gitdir:~/work/"] path = ~/.gitconfig-work
[includeIf "hasconfig:remote.*.url:git@github.com-work:"] path = ~/.gitconfig-workFile ~/.gitconfig-work:
[user] name = Your Work Name email = work@company.comCách hoạt động:
- Repo trong thư mục
~/work/hoặc có remote URL chứa aliasgithub.com-work→ tự dùng email work - Repo khác → dùng email personal
Kiểm tra user/email trong repo
git config user.namegit config user.emailXử lý lỗi thường gặp
Permission denied (publickey).fatal: Could not read from remote repository.Nguyên nhân & cách xử lý:
- Clone chưa dùng alias → sửa lại URL hoặc clone lại bằng
github.com-work/github.com-personal - Key chưa add vào ssh-agent →
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_github_work - Public key chưa add vào GitHub
- Quyền file sai →
chmod 700 ~/.ssh && chmod 600 ~/.ssh/* - Debug chi tiết →
ssh -vT git@github.com-work
15 phút setup, đổi lấy việc không phải debug Permission denied mỗi lần thêm repo mới — và quan trọng hơn, không có commit nào lỡ tay ra dưới nhầm tên.