Mac下一台電腦管理多個SSH KEY(轉)


一、關於ssh是什么?

http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html

二、需求:

一台電腦上(Mac os)管理多個ssh key,可以任意切換,達到多用戶(賬號)使用不同ssh提交代碼。

以下利用gerrit和github賬號來做例子。

1)生成ssh key

ssh-keygen -t rsa -C "youremail@yourcompany.com" 

若一路回車(密碼可以不寫),這樣只會在~/.ssh/ 目錄下生成 id_rsa 和 id_rsa.pub 兩個文件。為了區分,我們在第一個回車后設置路徑:

Enter file in which to save the key (/root/.ssh/id_rsa):~/.ssh/文件名 

由此我們分別為gerrit和github生成對應的公鑰和私鑰,完成后的目錄:

id_rsa_gerrit
id_rsa_gerrit.pub
id_rsa_github
id_rsa_github.pub

2)設置ssh key的代理

1、 首先查看代理

ssh-add -l 

若提示

Could not open a connection to your authentication agent.

則系統代理里沒有任何key,執行如下操作

exec ssh-agent bash 

若系統已經有ssh-key 代理 ,可以刪除

ssh-add -D

2、 添加私鑰

ssh-add ~/.ssh/id_rsa_gerrit 
ssh-add ~/.ssh/id_rsa_github

3、添加公鑰

在對應的gerrit和github的ssh管理頁面,添加對應的公鑰(.pub 文件內容),保存到代碼管理服務器。

4、添加和編輯配置文件config

在 ~/.ssh 目錄下新建一個config文件

touch ~/.ssh/config 

添加內容

# gerrit 
Host gerrit.xxxx.com 
HostName gerrit.xxxx.com 
PreferredAuthentications publickey 
IdentityFile ~/.ssh/id_rsa_gerrit 
user gerrit 

# github 
Host github.com 
HostName github.com 
PreferredAuthentications publickey 
IdentityFile ~/.ssh/id_rsa_github 
user git 

當然也可以利用nano命令來創建和編輯

nano ~/.ssh/config 

如此,ssh就會根據登陸的不同域,來讀取對應的私鑰文件

5、測試

ssh -T git@github.com 

若出現

Hi XXX! You've successfully authenticated, but GitHub does not provide shell access. 

則表示成功。

若出現

permission denied (publickey) 

請檢查github的ssh管理里添加的公鑰是否正確。

6、其他

提交代碼到gerrit失敗,報錯為

remote: ERROR: In commit xxxxxxxxxxxxxxxxxxxxxxxx 
remote: ERROR: committer email address aaaaa@aaa.com 
remote: ERROR: does not match your user account. 
remote: ERROR: 
remote: ERROR: The following addresses are currently registered: 
remote: ERROR: bbbbbbb@bbbbbb.com 
remote: ERROR: 
remote: ERROR: To register an email address, please visit: 
remote: ERROR: http://xxxx/xxxx 

此報錯並非你之前設置管理多個ssh出現問題,而是因為你當前git用戶信息和你提交代碼的服務器注冊的用戶信息不一致,即服務器檢查了你的用戶信息,進行了拒絕。

解決方法如下:

1.編輯.gitconfig

打開~/.gitconfig文件
將原來的
name = aaaaa
email = aaaaa@aaa.com
改成你當前的
name = bbbbb
email = bbbbb@ bbb.com

2.更改提交

git commit --amend --author 'bbbbb <bbbbb@ bbb.com>' 

當然你也可以這樣重新提交

git reset HEAD^ 
git add -A 
git commit -m “xxxx”

轉自:http://blog.csdn.net/wwmusic/article/details/51027458


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM