同一台主机配置多个ssh key
生成ssh key命令
ssh-keygen -C "xxx@xxx.com"
提示输出保存key的名字和路径
Enter file in which to save the key (/Users/caichenghan/.ssh/id_rsa):
我们可以自己定义
/Users/yourusername/.ssh/id_rsa_github
在对应的目录下我们可以看到生成对应的id_rsa_github
和id_rsa_github.pub
文件,将对应的pub文件复制到远程仓库进行保存即可
增加配置文件config
为了使得不同的远程仓库使用不同的ssh key进行,我们需要新建一个config文件来对不同的hostname指定不同的ssh key文件
# 添加config配置文件
# 文件内容如下:
# github.com - mylove@163.com
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile C:/Users/mylove/.ssh/id_rsa_github_mylove
User mylove
# e.coding.net - mylove@163.com
Host e.coding.net
HostName e.coding.net
PreferredAuthentications publickey
IdentityFile C:/Users/mylove/.ssh/id_rsa_coding_mylove
User mylove
# gitee.com - mylove@163.com
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile C:/Users/mylove/.ssh/id_rsa_gitee_mylove
User mylove
# gitlab.com - mylove@163.com
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile C:/Users/mylove/.ssh/id_rsa_gitlab_mylove
User mylove
新增一个远程仓库和本地key的对应关系我们只要在config文件中增加一个host属性即可