ssh-keygen -t rsa -C "gitlab 用戶郵箱地址" ←┘ Generating public/private rsa key pair. Enter file in which to save the key (/Users/user/.ssh/id_rsa): ←┘ Enter passphrase (empty for no passphrase): ←┘ Enter same passphrase again: ←┘ ...
ssh-keygen -t rsa -C "github 用戶郵箱地址" ←┘ Generating public/private rsa key pair. Enter file in which to save the key (/Users/user/.ssh/id_rsa): id_rsa_github ←┘ Enter passphrase (empty for no passphrase): ←┘ Enter same passphrase again: ←┘ ...
- id_rsa - id_rsa.pub - id_rsa_github - id_rsa_github.pub
分別將 id_rsa.pub 和 id_rsa_github.pub 內容複製到 gitlab 或 github 的 ssh key 中git
# gitlab Host gitlab HostName gitlab.com IdentityFile ~/.ssh/id_rsa # github Host github HostName github.com IdentityFile ~/.ssh/id_rsa_github
ssh -T git@gitlab.com ←┘ The authenticity of host 'gitlab.com (52.167.219.168)' can't be established. ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw. Are you sure you want to continue connecting (yes/no)? yes ←┘ ... Welcome to GitLab, username!
ssh -T git@github.com ←┘ The authenticity of host 'github.com (52.167.219.168)' can't be established. ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw. Are you sure you want to continue connecting (yes/no)? yes ←┘ ... Hi username! You've successfully authenticated, but GitHub does not provide shell access.
會在 ~/.ssh/ 目錄下自動生成 known_hosts 文件github
緣由是,咱們自定義了 id_rsa_github 鑰匙名,默認狀況下,鏈接會搜索 id_rsa 鑰匙名,因此這裏會失敗
能夠經過 ssh -T -v git@github.com 瞭解鏈接失敗的具體緣由shell
# 開啓 agent eval $(ssh-agent -s) ←┘ Agent pid 8428
# 添加 鑰匙名 ssh-add ~/.ssh/id_rsa_github ←┘ Identity added: /c/Users/user/.ssh/id_rsa_github (/c/Users/user/.ssh/id_rsa_github)
# 查看 agent list ssh-add -l ←┘ 8428 SHA256:A9UcJMadwTpF7TvsT5LEXsSssTs5qehmV9Q2Q8Ntw3o /c/Users/user/.ssh/id_rsa_github (RSA)
# 不用時能夠關閉 agent eval $(ssh-agent -k) ←┘ Agent pid 8428 killed
注意:使用github時,記得在本身的項目下定義 local user.name 和 local user.emailssh
git config --local user.name '' git config --local user.email ''