Git多SSH key开发环境配置
在我们电脑使用Git时可能会遇到要使用GitLab、GitHub、Gitee等不同代码托管平台的情况,之前由于项目工期紧、自己也比较懒,弄了几下多SSH key没有成功嫌麻烦就放弃了,改成直接使用HTTPS方式,这次特意抽点功夫研究一下,总结完贡献出来,不废话了直奔主题…
我使用的Windows系统装Git简单的一批,Git官网下载个安装包一直下一步就好了,其他系统也不困难请自行安装,这里不再多说…
1. 安装完Git先运行Git Bash看下安装成功了没有,然后设置下全局用户名和邮箱
# 查看git版本
git --version
# 添加全局用户名和邮箱
git config --global user.name "yourname"
git config --global user.email "yourname@email.com"
# 查看当前的全局变量
git config -l
2. 生成多个ssh key,用几个平台生成几个,每条命令运行后再2下回车
# 生成ssh key
ssh-keygen -t rsa -C "yourname1@email.com" -f ~/.ssh/id_rsa_gitlab
ssh-keygen -t rsa -C "yourname2@email.com" -f ~/.ssh/id_rsa_github
ssh-keygen -t rsa -C "yourname3@email.com" -f ~/.ssh/id_rsa_gitee
3. 打开公钥~/.ssh/id_rsa_xxx.pub(C:\Users\你的用户名\.ssh)将ssh key添加到Git平台的SSH keys列表中
-
GitLab设置:登陆后点右上角头像 -> Settings -> 左侧菜单SSH Keys -> Title随便写,Key把公钥~/.ssh/id_rsa_gitlab.pub里的ssh key粘贴上 -> 点Add key添加
-
GitHub设置:登陆后点右上角头像 -> Settings -> 左侧菜单SSH and GPG keys -> 点右侧New SSH key按钮 -> Title随便写,Key把公钥~/.ssh/id_rsa_gitlab.pub里的ssh key粘贴上 -> 点Add SSH key添加
-
Gitee设置:登陆后点右上角头像 -> 设置 -> 左侧菜单SSH公钥 -> (标题)随便写,(公钥)把公钥~/.ssh/id_rsa_gitlab.pub里的ssh key粘贴上 -> 点确定添加
4. 添加私钥
# 先运行ssh公钥管理,再添加公钥,否则可能报错Could not open a connection to your authentication agent.
ssh-agent bash
# 添加私钥
ssh-add ~/.ssh/id_rsa_gitlab
ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_gitee
# 查看私钥列表
ssh-add -l
5. 添加配置文件
# 添加配置文件
cat >> ~/.ssh/config <<EOF
# gitlab
Host mygitlab.com
HostName mygitlab.com
Port 22
User yourname
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab
# github
Host github.com
HostName github.com
User yourname
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
# gitee
Host gitee.com
HostName gitee.com
User yourname
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitee
EOF
配置文件参数
Host: 指定Host别名,建议与HostName相同
HostName: 指定Git服务器地址(域名或IP)
Port: 指定Git服务器端口(默认80端口可忽略此项)
User: 指定用户名(自定义)
PreferredAuthentications: 指定优先使用哪种方式验证,支持密码和密钥验证方式
IdentityFile: 指定对应的私钥文件
6. 测试(显示successfully即为成功)
# 测试
ssh -T git@mygitlab.com
ssh -T git@github.com
ssh -T git@gitee.com