1、創(chuàng)建config文件
cd .ssh
touch config
2、創(chuàng)建ssh-key
ssh-keygen -t rsa -f ~/.ssh/id_rsa.別名 -C “郵箱地址“
示例:
ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C “xxx@xxx.com“
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitlab -C “xxx@xxx.com“
ssh-key文件已經(jīng)生成到指定路徑,接下來需要對他們進(jìn)行配置
3、配置
# gitlab
Host gitlab.xxx.com
HostName gitlab.xxx.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_xxx_id-rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id-rsa
# 配置文件參數(shù)
# Host : Host可以看作是一個你要識別的模式,對識別的模式,進(jìn)行配置對應(yīng)的的主機名和ssh文件
# HostName : 要登錄主機的主機名
# User : 登錄名
# IdentityFile : 指明上面User對應(yīng)的identityFile路徑
Host是別名,如果只是為了區(qū)分github、gitlab等,為了方便使用,建議和HostName一致,這樣在clone git的時候不用考慮修改hostname。
4、通過別名來使用
ssh -T git@gitlab.xxx.com
返回:Welcome to Gitlab, xxx!
5、設(shè)置和去除全局用戶名和郵箱
去除:
git config --global --unset user.name
git config --global --unset user.email
設(shè)置:
git config –-global user.email "xx@xx.com"
git config --global user.name "xxx"