Mac生成多個ssh並配置不一樣域名

1.前言

有時候咱們會有多個git帳號,如GitHub,GitLab,這時若是使用同一個郵件註冊,那不會有問題,可是假如用的是不一樣的郵件註冊帳號,這就須要生成不一樣的ssh文件併爲其配置相應的域名。git

2.生成一個SSH-Key

$ ssh-keygen -t rsa -C "youremail@email.com"//本身git帳號對應的郵箱
複製代碼

如若一路enter,你會獲得:github

id_rsa
id_rsa.pub
複製代碼

這樣不是不能夠,可是咱們要生成多個,因此最好起有區分的名字。 vim

3.設置密碼

能夠不設置,也能夠鍵入密碼

4.重複步驟2和3,生成對應的rsa和rsa.pub文件

//GitHub生成的對應ssh-key
id_github_ras   //私鑰
id_github_ras.pub   //公鑰

//Gitlab生成的對應ssh-key
id_gitlab_ras
id_gitlab_ras.pub
複製代碼

5.配置ssh-key到對應的域名

5.1在~/.ssh目錄下生成一個config文件
cd ~/.ssh
vim config
複製代碼

5.2在config文件中鍵入

Host github
HostName github.com
User git
PreferredAuthentications publickey
#下面填寫的是私鑰名,沒有pub後綴
IdentityFile ~/.ssh/id_github_rsa

Host gitlab
HostName gitlab.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_gitlab_rsa 
複製代碼

6.將專有密鑰添加到 ssh-agent 中

ssh-add ~./ssh/id_github_rsa
ssh-add ~./ssh/id_gitlab_rsa
複製代碼

tips:
把專有密鑰添加到 ssh-agent 中bash

ssh-add ~./ssh/id_rsa
複製代碼

從 ssh-agent 中刪除密鑰ssh

ssh-add -d ./ssh/id_rsa.pub
複製代碼

查看 ssh-agent 中的密鑰gitlab

ssh-add -l
複製代碼

7.將rsa.pub加入到GitHub/Gitlab等網站

cat ~./ssh/id_github_rsa.pub
複製代碼

將該字符串拷貝粘貼到Git網站對應添加ssh-key的地方:
GitHub:網站

Setting->SSH and GPG keys
複製代碼

GitLab:ui

Setting->SSH keys
複製代碼

其餘網站本身找到添加ssh-key的位置,添加便可。spa

8.如今就能夠使用ssh而不是https愉快的玩耍git了

git clone git@github.com:XXX/demo.git
git clone git@gitlab.com:XXX/demo.git
複製代碼
相關文章
相關標籤/搜索