git添加Github、Gitee、Gitlab秘钥
<h2 id="参考">参考:</h2><p>配置同时使用 Gitlab、Github、Gitee(码云) 共存的开发环境</p>
<hr>
<h2 id="正文">正文:</h2>
<p>下载安装Git:Downloads git</p>
<h2 id="清除git全局设置如果之前装过git并且配置过的话">清除git全局设置(如果之前装过Git并且配置过的话)</h2>
<p>新安装git可跳过。</p>
<p>若之前对 git 设置过全局的 user.name 和 user.email。<br>
类似 (用 git config --global --list 进行查看你是否设置)</p>
<pre><code>$ git config --global user.name "你的名字"
$ git config --global user.email"你的邮箱"
</code></pre>
<p>必须删除该设置</p>
<pre><code>$ git config --global --unset user.name "你的名字"
$ git config --global --unset user.email "你的邮箱"
</code></pre>
<h2 id="生成新的ssh-keys">生成新的SSH Keys</h2>
<p>1)GitHub 的钥匙<br>
指定文件路径,方便后面操作:<code>~/.ssh/id_rsa.gitlab</code></p>
<pre><code>ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "你的邮箱"
</code></pre>
<p>直接回车3下,什么也不要输入,就是默认没有密码。</p>
<p>注意 github 和 gitlab 的文件名是不同的。</p>
<p>2)GitLab 的钥匙</p>
<pre><code>ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitlab -C "你的邮箱"
</code></pre>
<p>3)Gitee 的钥匙</p>
<pre><code>ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "你的邮箱"
</code></pre>
<p>4)完成后会在<code>~/.ssh/ </code>目录下生成以下文件</p>
<pre><code>id_rsa.github
id_rsa.github.pub
id_rsa.gitlab
id_rsa.gitlab.pub
</code></pre>
<h2 id="添加识别-ssh-keys-新的私钥">添加识别 SSH keys 新的私钥</h2>
<p>默认只读取 id_rsa,为了让 SSH 识别新的私钥,需要将新的私钥加入到 SSH agent 中</p>
<pre><code>$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa.github
$ ssh-add ~/.ssh/id_rsa.gitlab
$ ssh-add ~/.ssh/id_rsa.gitee
</code></pre>
<h2 id="多账号必须配置-config-文件重点">多账号必须配置 config 文件(重点)</h2>
<p>若无 config 文件,则需创建 config 文件<br>
创建config文件</p>
<pre><code>$ touch ~/.ssh/config
</code></pre>
<p>config 里需要填的内容<br>
亲测可以不缩进,所以方便看,建议缩进。</p>
<h3 id="最简配置">最简配置</h3>
<pre><code>Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa.github
</code></pre>
<h3 id="完整配置">完整配置</h3>
<pre><code>#Default gitHub user Self
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa.github
#Add gitLab user
Host git@gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa.gitlab
# gitee
Host gitee.com
Port 22
HostName gitee.com
User git
IdentityFile ~/.ssh/id_rsa.gitee
# other settings
Host git@git.startdt.net
Port 22
HostName http://git.startdt.net
User git
IdentityFile ~/.ssh/lab_rsa.startdt
</code></pre>
<p>下面对上述配置文件中使用到的配置字段信息进行简单解释:</p>
<blockquote>
<p>Host<br>
它涵盖了下面一个段的配置,我们可以通过他来替代将要连接的服务器地址。<br>
这里可以使用任意字段或通配符。<br>
当ssh的时候如果服务器地址能匹配上这里Host指定的值,则Host下面指定的HostName将被作为最终的服务器地址使用,并且将使用该Host字段下面配置的所有自定义配置来覆盖默认的/etc/ssh/ssh_config配置信息。</p>
<p>Port<br>
自定义的端口。默认为22,可不配置</p>
<p>User<br>
自定义的用户名,默认为git,可不配置</p>
</blockquote>
<blockquote>
<p>HostName<br>
真正连接的服务器地址</p>
<p>PreferredAuthentications<br>
指定优先使用哪种方式验证,支持密码和秘钥验证方式</p>
<p>IdentityFile<br>
指定本次连接使用的密钥文件</p>
</blockquote>
<h2 id="在-github-和-gitlab-网站添加-ssh">在 github 和 gitlab 网站添加 ssh</h2>
<h3 id="github">Github</h3>
<p><img src="https://img2020.cnblogs.com/blog/1724937/202009/1724937-20200920225435155-565337703.png" alt="" loading="lazy"></p>
<p>Github 添加SSH公钥<br>
直达地址:https://github.com/settings/keys</p>
<p>过程如下:</p>
<ol>
<li>登录 Github</li>
<li>点击右上方的头像,点击 settings</li>
<li>选择 SSH key</li>
<li>点击 Add SSH key</li>
</ol>
<p>在出现的界面中填写 SSH key 的名称,填一个你自己喜欢的名称即可。<br>
将上面拷贝的<code>~/.ssh/id_rsa.xxx.pub</code>文件内容粘帖到 key 一栏,在点击 “add key” 按钮就可以了。</p>
<p>添加过程 github 会提示你输入一次你的 github 密码 ,确认后即添加完毕。</p>
<h3 id="gitlab">Gitlab</h3>
<p><img src="https://img2020.cnblogs.com/blog/1724937/202009/1724937-20200920225630401-1159512208.png" alt="" loading="lazy"></p>
<p>Gitlab 添加SSH公钥<br>
直达地址:https://gitlab.com/profile/keys</p>
<ol>
<li>登录 Gitlab</li>
<li>点击右上方的头像,点击 settings</li>
<li>后续步骤如 Github</li>
</ol>
<h3 id="gitee--码云">Gitee / 码云</h3>
<p><img src="https://img2020.cnblogs.com/blog/1724937/202009/1724937-20200920225742217-951897027.png" alt="" loading="lazy"></p>
<p>码云 添加SSH公钥<br>
直达地址:https://gitee.com/profile/sshkeys</p>
<ol>
<li>登录 Gitee</li>
<li>点击右上方的头像,点击 设置</li>
<li>后续步骤如 Github<br>
添加过程 码云 会提示你输入一次你的 Gitee 密码 ,确认后即添加完毕。</li>
</ol>
<h2 id="测试是否连接成功">测试是否连接成功</h2>
<p>由于每个托管商的仓库都有唯一的后缀,比如 Github 的是 git@github.com:*。</p>
<p>所以可以这样测试:</p>
<pre><code>ssh -T git@github.com
</code></pre>
<p>而 gitlab 的可以这样测试:</p>
<pre><code>ssh -T git@gitlab.corp.xyz.com
</code></pre>
<p>如果能看到一些 Welcome 信息,说明就是 OK 的了</p>
<pre><code>ssh -T git@github.com
ssh -T git@gitlab.com
ssh -T git@gitee.com
</code></pre>
<hr>
<p>$ ssh -T git@github.com</p>
<blockquote>
<p>Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.<br>
Hi dragon! You've successfully authenticated, but GitHub does not provide shell access.</p>
</blockquote>
<p>$ ssh -T git@gitlab.com</p>
<blockquote>
<p>The authenticity of host 'gitlab.com (35.231.145.151)' can't be established.<br>
ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSn.<br>
Are you sure you want to continue connecting (yes/no)? yes<br>
Warning: Permanently added 'gitlab.com,35.231.145.151' (ECDSA) to the list of known hosts.<br>
Welcome to GitLab, @dragon!</p>
</blockquote>
<p>$ ssh -T git@gitee.com</p>
<blockquote>
<p>The authenticity of host 'gitee.com (116.211.167.14)' can't be established.<br>
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrp+KkGYoFgbVr17bmjeyc.<br>
Are you sure you want to continue connecting (yes/no)? yes<br>
Warning: Permanently added 'gitee.com,116.211.167.14' (ECDSA) to the list of known hosts.<br>
Hi 我是x! You've successfully authenticated, but GITEE.COM does not provide shell access.</p>
</blockquote>
<hr>
<p>结果如果出现这个就代表成功:</p>
<pre><code>GitHub -> successfully
GitLab -> Welcome to GitLab
Gitee -> successfully
</code></pre><br><br>
来源:https://www.cnblogs.com/jsdy/p/13702914.html
頁:
[1]