目錄git
需求:一臺電腦上(Mac os)管理多個ssh key,能夠任意切換,達到多用戶(帳號)使用不一樣ssh提交代碼。github
如下利用bitbucket
和github
帳號來作例子。shell
ssh-keygen -t rsa -C "youremail@yourcompany.com"
註解:安全
密鑰類型
能夠用 -t 選項
指定。若是沒有指定則默認生成用於SSH-2
的RSA密鑰。這裏使用的是rsa
。註釋字段
,用-C
來指定所指定的註釋,能夠方便用戶標識這個密鑰,指出密鑰的用途或其餘有用的信息。因此在這裏輸入本身的郵箱或者其餘都行
。~/.ssh/ 目錄
下生成 id_rsa 和 id_rsa.pub 兩個文件
。爲了區分,咱們在第一個回車後設置路徑,進行第二步bash
id_rsa_bitbucket id_rsa_bitbucket.pub id_rsa_github id_rsa_github.pub
一個密語字符串(passphrase)
,空表示沒有密語。接着會讓輸入2次口令(password)
,空表示沒有口令。3次回車便可完成當前步驟,此時[~/.ssh]目錄下已經生成好了。
完了以後,大概是這樣:
服務器
到此爲止,你本地的密鑰對就生成了。ssh
ssh-add -l
若提示 ide
Could not open a connection to your authentication agent.
則系統代理裏沒有任何key,執行以下操做測試
exec ssh-agent bash
若系統已經有ssh-key 代理 ,能夠刪除代理
ssh-add -D
ssh-add ~/.ssh/id_rsa_bitbucket ssh-add ~/.ssh/id_rsa_github
在對應的github的ssh管理頁面,添加對應的公鑰(.pub 文件內容),保存到代碼管理服務器。
在 ~/.ssh 目錄
下新建一個config文件
touch ~/.ssh/config
添加內容
# git@bitbucket.org Host bitbucket.org HostName bitbucket.org PreferredAuthentications publickey IdentityFile ~/.ssh/github/id_rsa_bitbucket User git # git@github.com Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_github user git
固然也能夠利用nano命令來建立和編輯
nano ~/.ssh/config
如此,ssh就會根據登錄的不一樣域,來讀取對應的私鑰文件
ssh -T git@github.com
若出現
Hi XXX! You've successfully authenticated, but GitHub does not provide shell access.
則表示成功。
若出現
permission denied (publickey)
請檢查github的ssh管理裏添加的公鑰是否正確。