標籤:
參考連結:http://higrid.net/c-art-github_share_ssh.htm
http://yijiebuyi.com/blog/f18d38eb7cfee860c117d629fdb16faf.html
1、使用命令建立個ssh-key
tingdeMacBook-Pro:ContinuousIntegration ting$ ssh-keygen -t rsa -C ‘[email protected]‘
2、注意命令運行時輸入新的檔案名稱:
tingdeMacBook-Pro:ContinuousIntegration ting$ ssh-keygen -t rsa -C ‘[email protected]‘Generating public/private rsa key pair.Enter file in which to save the key (/Users/ting/.ssh/id_rsa): /Users/ting/.ssh/id_rsa_xxxx163_githubEnter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /Users/ting/.ssh/id_rsa_xxxx163_github.Your public key has been saved in /Users/ting/.ssh/id_rsa_xxxx163_github.pub.The key fingerprint is:……
3、預設SSH只會讀取id_rsa,所以為了讓SSH識別新的私密金鑰,需要將其添加到SSH agent
ssh-add ~/.ssh/id_rsa_xxxx163_githubIdentity added: /Users/ting/.ssh/id_rsa_xxxx163_github (/Users/ting/.ssh/id_rsa_xxxx163_github)
4、完成以上步驟後在~/.ssh目錄建立config檔案,該檔案用於配置私密金鑰對應的伺服器。內容如下:
# Default github user([email protected])Host github.comHostName github.com User git IdentityFile C:/Users/username/.ssh/id_rsa# second user([email protected])Host github-second HostName github.com User git IdentityFile C:/Users/username/.ssh/id_rsa_second
5、Host隨意即可,方便自己記憶,後續在添加remote是還需要用到。 配置完成後,在串連非預設帳號的github倉庫時,遠程庫的地址要對應地做一些修改,比如現在添加second帳號下的一個倉庫test,則需要這樣添加:
git remote add test [email protected]:second/test.git #並非原來的[email protected]:second/test.git
注意: github根據設定檔的user.email來擷取github帳號顯示author資訊,所以對於多帳號使用者一定要記得將user.email改為相應的email([email protected])。
同一台電腦使用多個sshkey