Mac 配置多个不同git域名的ssh公钥
一. 应用场景
当存在同时需要git在GitHub、gitee、gitlab等多个不同git托管平台进行ssh
代码操作的时候。
二. 具体操作
- 默认
ssh-keygen -t rsa -C "你的邮箱"
之后一直回车就可以,会默认在~/.ssh目录下生成id_rsa
、id_rsa.pub
- 指定文件
ssh-keygen -t rsa -C '你的邮箱' -f ~/.ssh/tiger_id_rsa
之后一直回车就可以,会在~/.ssh目录下生成tiger_id_rsa
、tiger_id_rsa.pub
- 查看并复制pub中的内容
cat id_rsa.pub
cat tiger_id_rsa.pub
之后复制并copy到GitHub或者gitlab的新建的ssh中。
- 生成并配置ssh的config配置文件
vim ~/.ssh/config
插入内容:
# GitHub
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# gitlab
Host gitlab.com # 如公司内网对应的自定义gitlab域名
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/tiger_id_rsa #指定的rsa文件路径
- 遇到的问题
The authenticity of host ‘github.com (20.205.243.166)’ can’t be established.
ECDSA key fingerprint is SHA256:…
ECDSA key fingerprint is MD5:…
解决方法:要输入yes,不能直接回车。
三. https转ssh
vim ./git/config
将url替换为ssh