今天遇到一個有趣的問題:Fork Git 客戶端無法連接到 AWS CodeCommit,即使命令行 SSH 測試完全正常。
問題現象
- 命令行
ssh git-codecommit.ap-east-1.amazonaws.com測試成功 - Fork 執行 pull 時出現
Permission denied (publickey)錯誤 - 錯誤訊息顯示 Fork 使用了錯誤的用戶名:
AzureAD+Canis而非正確的 SSH Key ID
準備工作
1. 從 PuTTY 建立 OpenSSH 私鑰
- 在 PuTTYgen 中加載你的
.ppk私鑰 - Conversions → Export OpenSSH key
- 保存為
codecommit_rsa(無副檔名) - 將檔案放到
C:\Users\YOUR_USER_NAME\.ssh\codecommit_rsa
2. 建立 SSH Config 文件
在
C:\Users\YOUR_USER_NAME\.ssh\config
中添加:
Host git-codecommit.*.amazonaws.com User APKA__YOURAWS_SSH_KEY IdentityFile ~/.ssh/codecommit_rsa IdentitiesOnly yes PreferredAuthentications publickey
3. 獲取 SSH Key ID
- 登入 AWS IAM 控制台
- 進入 Users → 你的用戶 → Security credentials
- 在 SSH public keys for AWS CodeCommit 區域找到你的 SSH Key ID
- 重要筆記:這個
APKA...開頭的字串就是你的 SSH Key ID,在 IAM 中上傳公鑰後自動生成
解決方案
將 SSH Key ID 直接嵌入到遠程 URL 中:
git remote set-url origin ssh://APKA__YOURAWS_SSH_KEY@git-codecommit.ap-east-1.amazonaws.com/v1/repos/repo-name
原因分析
Fork 似乎無法正確讀取 SSH config 文件中的 User 配置,而是使用了系統的登入用戶名。通過在 URL 中明確指定 SSH Key ID 作為用戶名,可以繞過這個問題。
總結
雖然 SSH config 配置正確,但某些 Git 客戶端可能需要在 URL 中明確指定 SSH Key ID 才能正常工作。記住:APKA 開頭的字串是 AWS 為你的 SSH 公鑰分配的唯一識別碼,必須作為用戶名使用。