ssh配置多账号(Mac)

一. 应用场景

当存在同时需要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配置多账号(Mac)_第1张图片

生成并配置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文件路径

三、验证

ssh -T [email protected]

在这里插入图片描述

你可能感兴趣的:(git,ssh)