github、gitee冲突配置ssh key
<h2 id="背景">背景</h2><p>当有多个git账号时,比如:</p>
<p>a. 两个gitee,一个账号是用于公司内部的工作开发,一个账号是自己学习的个人账号;<br>
b. 一个github,用于自己进行一些开发活动;</p>
<p>操作:</p>
<h2 id="生成不同的key">生成不同的key</h2>
<pre><code class="language-sh">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个人
</code></pre>
<pre><code class="language-sh">haima@haima-PC:~/.ssh$ ll
总用量 64
drwx------2 haima haima4096 1月19 23:51 .
drwxr-xr-x 94 haima haima 28672 1月19 23:46 ..
-rw-r--r--1 rootroot 377 1月19 23:48 config
-rw-------1 haima haima1679 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 root398 1月20 23:55 github_hm_bmh_id_rsa.pub
-rw-------1 haima haima1675 4月232020 gitee_company_id_rsa
-rw-r--r--1 haima haima 396 4月232020 gitee_company_id_rsa.pub
</code></pre>
<h2 id="添加配置文件config">添加配置文件config</h2>
<p><code>sudo vim ~/.ssh/config</code></p>
<p>添加如下内容</p>
<pre><code class="language-sh"># 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
</code></pre>
<h2 id="分别把公钥添加到对应账号的仓库里">分别把公钥添加到对应账号的仓库里</h2>
<p><img src="https://img2020.cnblogs.com/blog/1441611/202101/1441611-20210120000544198-2047543856.png" alt="" loading="lazy"></p>
<p><img src="https://img2020.cnblogs.com/blog/1441611/202101/1441611-20210120000422142-1930975710.png" alt="" loading="lazy"></p>
<h2 id="测试">测试</h2>
<pre><code class="language-sh">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..
</code></pre>
<h2 id="使用ssh方式下载项目">使用ssh方式下载项目</h2>
<ol>
<li>
<p>工作帐号示例:</p>
<p><code>git clone git@gitee_company.com:xxx/yyy.git</code></p>
</li>
<li>
<p>个人帐号示例:</p>
<p><code>git clone git@gitee_haimait_id_rsa.com:aaa/bbb.git</code></p>
<p>例如:</p>
<p><code>git clone git@gitee_haimait_id_rsa:haima1004/mindoc.git</code></p>
<p>or</p>
<p><code>git@github_hm_bmh_id_rsa:haimait/docker-compose.git</code></p>
<p>最后的最后注意:在项目的git配置文件覆盖全局的git配置文件,主要是用户名的配置。</p>
</li>
</ol>
<p>参考:<br>
Git配置多个SSH-Key<br>
https://gitee.com/help/articles/4229#article-header0</p>
<p>如何在同一台电脑上设置两个gitee帐号的ssh-key<br>
https://www.cnblogs.com/verdent/p/13736662.html</p>
<p>https://www.jianshu.com/p/842dfea81bf5?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation</p>
<p>https://www.yuque.com/menew/kd0ic7/evlktg</p>
</div>
<div id="MySignature" role="contentinfo">
http://www.cnblogs.com/haima/<br><br>
来源:https://www.cnblogs.com/haima/p/14284829.html
頁:
[1]