1、创建config文件
cd .ssh
touch config
2、创建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文件已经生成到指定路径,接下来需要对他们进行配置
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
# 配置文件参数
# Host : Host可以看作是一个你要识别的模式,对识别的模式,进行配置对应的的主机名和ssh文件
# HostName : 要登录主机的主机名
# User : 登录名
# IdentityFile : 指明上面User对应的identityFile路径
Host是别名,如果只是为了区分github、gitlab等,为了方便使用,建议和HostName一致,这样在clone git的时候不用考虑修改hostname。
4、通过别名来使用
ssh -T git@gitlab.xxx.com
返回:Welcome to Gitlab, xxx!
5、设置和去除全局用户名和邮箱
去除:
git config --global --unset user.name
git config --global --unset user.email
设置:
git config –-global user.email "xx@xx.com"
git config --global user.name "xxx"
网友评论