github、gitee冲突配置ssh key

背景

当有多个git账号时,比如:

a. 两个gitee,一个账号是用于公司内部的工作开发,一个账号是自己学习的个人账号;
b. 一个github,用于自己进行一些开发活动;

操作:

生成不同的key

ssh-keygen -t rsa -C "whm2416@qq.com" -f ~/.ssh/gitee_haimait_id_rsa #gitee个人
ssh-keygen -t rsa -C "company" -f ~/.ssh/gitee_company_admin_id_rsa  #gitee公司
ssh-keygen -t rsa -C "github_whm2416@qq.com" -f ~/.ssh/github_haimait_id_rsa  #github个人 
haima@haima-PC:~/.ssh$ ll
总用量 64
drwx------  2 haima haima  4096 1月  19 23:51 .
drwxr-xr-x 94 haima haima 28672 1月  19 23:46 ..
-rw-r--r--  1 root  root    377 1月  19 23:48 config
-rw-------  1 haima haima  1679 1月  16 01:30 gitee_haimait_id_rsa
-rw-r--r--  1 haima haima   396 1月  16 01:30 gitee_haimait_id_rsa.pub
-rw------- 1 root root 1823 1月  20 23:55 github_hm_bmh_id_rsa
-rw-r--r-- 1 root root  398 1月  20 23:55 github_hm_bmh_id_rsa.pub
-rw-------  1 haima haima  1675 4月  23  2020 gitee_company_id_rsa
-rw-r--r--  1 haima haima   396 4月  23  2020 gitee_company_id_rsa.pub

添加配置文件config

sudo vim ~/.ssh/config

添加如下内容

# gitee公司密钥
Host gitee_company.com #自己启的别名,下载clone时需要用到
HostName gitee.com  #写死定值 码云的域名
PreferredAuthentications publickey #写死定值
IdentityFile ~/.ssh/gitee_company_id_rsa  #对应的密钥名称

# gitee self密钥
Host gitee_haimait_id_rsa
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_haimait_id_rsa


# github self密钥
Host github_hm_bmh_id_rsa
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_hm_bmh_id_rsa

分别把公钥添加到对应账号的仓库里

测试

haima@haima-PC:~/.ssh$ ssh -T git@gitee_haimait_id_rsa
Hi haimait! You've successfully authenticated, but GITEE.COM does not provide shell access.
haima@haima-PC:~/.ssh$ ssh -T git@gitee_company.com
Hi LinkBook! You've successfully authenticated, but GITEE.COM does not provide shell access.
root@haima-PC:~/.ssh# ssh -T git@github_hm_bmh_id_rsa
Hi haimait! You've successfully authenticated, but GitHub does not provide shell access..

使用ssh方式下载项目

  1. 工作帐号示例:

    git clone git@gitee_company.com:xxx/yyy.git

  2. 个人帐号示例:

    git clone git@gitee_haimait_id_rsa.com:aaa/bbb.git

    例如:

    git clone git@gitee_haimait_id_rsa:haima1004/mindoc.git

    or

    git@github_hm_bmh_id_rsa:haimait/docker-compose.git

    最后的最后注意:在项目的git配置文件覆盖全局的git配置文件,主要是用户名的配置。

参考:
Git配置多个SSH-Key
https://gitee.com/help/articles/4229#article-header0

如何在同一台电脑上设置两个gitee帐号的ssh-key
https://www.cnblogs.com/verdent/p/13736662.html

https://www.jianshu.com/p/842dfea81bf5?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

https://www.yuque.com/menew/kd0ic7/evlktg

posted @ 2021-01-16 01:01  HaimaBlog  阅读(395)  评论(0编辑  收藏  举报