2015年4月15日 星期三

Multiple SSH keys on your computer

平常用慣了Git來管理程式, 但是
公司跟個人的開發環境應該分清楚一點比較好.

例如說我平常自己個人在github或是bitbucket上開發
一開始會follow教學, 先產生SSH key
cd ~/.ssh
ssh-keygen -t rsa -C "user_name@whatever.com"

然後copy 公鑰id_rsa.pub到server上, 像是:
pbcopy < ~/.ssh/id_rsa.pub
或是手動複製
cat ~/.ssh/id_rsa.pub

但是要新增另外一把SSH key在同一台機器上, 可以使用下面的方法:
ssh-keygen -t rsa -f ~/.ssh/accountB -C "user_name@whatever.com"

然後新增並編輯config:
touch ~/.ssh/config
vi config

config內容大概長得像這樣:
Host bitbucket.org
User git
Hostname bitbucket.org
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa

Host bitbucket-accountB
User git
Hostname bitbucket.org
PreferredAuthentications publickey
IdentitiesOnly yes
IdentityFile ~/.ssh/accountB

如果SSH遇到connection refused很可能是因為拿了錯的key去連.
這時我們需要指定SSH連線用的key:
ssh -vv -i ~/.ssh/accountB -p port username@server.com
-vv:如果有permission denied的話要加
-i ~/.ssh/accountB:指定SSH key的位置
-p:不指定通常是22 port

如果要操作git的話:
git clone git@bitbucket-accountB:username/project.git

還有還有,
要有多個git username以及email的話
在各個repo底下去設定:
git config user.name "your name"
git config user.email "your@email.com"

參考連結:
Evernote helps you remember everything and get organized effortlessly. Download Evernote.

2 則留言:

  1. This article is very detailed and informative on this topic, thanks for sharing!
    We Provide Best SEO Services in Gurgaon Enquire Now!.

    回覆刪除
  2. Thanks for written a beautiful post! It was really good and informational post.
    Gurmeet Ram Rahim Singh Insan is the head of the Indian social group Dera Sacha Sauda.
    You can follow to Spiritual Saint
    Gurmeet Ram Rahim Singh Ji Insan on IMDB.

    回覆刪除

內容回應