首先,創建SSH公鑰(這里用的是mac系統)
添加創建代碼,先添加github賬號
$ssh-keygen -t rsa -C "GitEmail@example.com"
# Creates a new ssh key, using the provided email as a label
# Generating public/private rsa key pair.
Enterfileinwhich to save the key (/Users/you/.ssh/id_rsa):[Pressenter]
這里先別按回車,因為我們需要不同賬號登陸,我們輸入
/Users/you/.ssh/id_rsa_git
回車,記住前面的you每個不同電腦賬號顯示不一樣。改成跟你提示一樣的。
創建coding賬號的,假設我們名字為id_rsa_coding
$ssh-keygen -t rsa -C "CodingEmail@example.com"
# Creates a new ssh key, using the provided email as a label
# Generating public/private rsa key pair.
Enterfileinwhich to save the key (/Users/you/.ssh/id_rsa):[Pressenter] /Users/you/.ssh/id_rsa_coding //回車
創建配置文件,用來區分不同賬號
$vi ~/.ssh/config
用vi編輯器來創建config文件,按鍵盤i,插入文本。復制修改如下
#one(GitEmail@example.com)
Host git
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_git
User yourName
#two(CodingEmail@example.com)
Host coding
HostName git.coding.net
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_coding
User yourName
然后按鍵盤的esc,輸入 :wq 回車 退出保存
image.png
然后再通過命令,把公鑰添加到系統
$ssh-add id_rsa_coding id_rsa_git
如果在第一步有輸入秘鑰的話,這里會讓你輸入
image.png
添加完以后,可以通過如下命令來查看
$ssh-add -l
image.png
然后我們把2份pub的內容放到git跟coding里面
不懂請看文檔:coding創建公鑰
image.png
放好以后,我們開始在命令行測試看有沒有連接成功~
ssh -T git@git
coding測試
ssh -T git@coding
這個其實等同于,還記得我們的config配置文件么
ssh -T git@github.com
如下圖,恭喜你成功了。
image.png
然后以后我們克隆項目命令
git clone git@git.coding.net:huangjianyu/TestCoding.git
改為
git clone git@coding:huangjianyu/TestCoding.git
就是把host對應的,clone后就可以直接cd到目錄下,操作了。不需要指定賬號。
image.png
image.png