在公司开发中,有时候会存在公司账户跟私人账户共存,并随时需要切换的情况,这种情况下git可以配置多个ssh-key,无缝切换账号。
假如有两个github账号,一个是私人github账号,一个是公司github账号
私人账号:
公司账号:
秘钥与公钥如果用户目录下没有.ssh目录,则需要新建一个
cd ~/.ssh
ssh-keygen -t rsa -f id_rsa_my
ssh-keygen -t rsa -f id_rsa_company
一路回车即可
上面ssh-keygen 命令参数:
rsa 类型秘钥id_rsa,公钥id_rsa.pub。因此上面的命令需要指定-f,否则生成两次后,私钥跟公钥会覆盖因此上面的命令调用完后至少会生成四个文件:
公钥配置到对应的github账号中公钥即.pub文件可以直接用文本打开,内容粘贴到github的 Settings -> SSH and GPG keys -> New SSH Key,Title随便起,自己能认出来即可,Key里面填写复制的.pub里的内容,这样公钥就配置好了

私钥配置到git中ssh-agent bash
ssh-add id_rsa_my
ssh-add id_rsa_company
ssh-agent bash(或eval $(ssh-agent))会启动一个进程在内存里管理这些私钥,可以把它理解成一个私钥管理中心ssh-add分别把两个私钥添加进来调用 ssh-add -l查看添加结果,添加成功的话如下所示:
2048 SHA256:X5HtSLQFgc55bQJ8kNwChmcfTOS6vvjIpAmj9PCX2Fs id_rsa_my (RSA)
2048 SHA256:C+UmpLUGhSgoSdMDebJeJ6hDrodYFjtkyCcBqvFWO1o id_rsa_company (RSA)
在.ssh目录下创建config 文件,git通过这个文件才知道哪个私钥去对应哪个公钥
touch config
config文件内容:
# my
Host my
HostName github.com
IdentityFile ~/.ssh/id_rsa_my
# company
Host company
HostName github.com
IdentityFile ~/.ssh/id_rsa_company
config文件部分参数含义,仅做记录-
# Host: 主机别名
# HostName: 托管平台域名地址,如github.com
# IdentityFile : 指明上面User对应的identityFile路径
# User: 托管平台用户名
# Port: 端口号,可不填(如果不是默认22号端口则需要指定)
# PreferredAuthentications publickey
ssh -T git@my
ssh -T git@company
成功的情况下会返回:
Hi xxx! You‘ve successfully authenticated, but GitHub does not provide shell access.
此时私钥 和 公钥 都配置正常了
此时测试一下clone私人git仓库,必须使用SSH方式clone。SSH类型的链接格式为:
git@github.com:github用户名/仓库名.git
比如:
git@github.com:convict/my-repo.git
若要clone这个仓库,应使用git clone git@my:convict/my-repo.git,即把github.com 换成my,此时clone成功,同理需要clone公司账户下的仓库,需要把github.com 换成company即可
若直接使用git clone git@github.com:convict/my-repo.git,会clone失败:
Cloning into ‘test‘...
Warning: Permanently added the RSA host key for IP address ‘13.250.177.223‘ to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
解释:
@与:之间就是Host,因此在git clone git@github.com:convict/my-repo.git中,Host就是github.com,但在前面配置的config文件中,指定了两个Host,分别为my与company,而没有一个加github.com的Host!这是尤其需要注意的。git clone git@my:convict/my-repo.git时,会在config中找到一个值为my的Host,接着到其HostName上找到与其私钥对应的公钥的仓库地址。在本例中,就是根据其私钥id_rsa_my,在github.com托管平台上,匹配对应的公钥,然后匹配到convict/my-repo.git这个仓库。原文:https://www.cnblogs.com/convict/p/14887421.html