如何免密操作 github、gitee 远程仓库
一、两种 git clone 远程仓库的方式
git clone 远程仓库有两种方式:
1. 一种是通过https
每次提交都需要输入账户密码,有些编辑器会记录你的账号密码以实现免密操作。但命令行操作时一定每次都需要账号密码。
![在这里插入图片描述](https://img-blog.csdnimg.cn/e76c95a83c424683b6b9d140c218d0c4.png)
2. 一种是通过 ssh
可以添加 ssh 的 pub key 来实现免密操作。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/177c5d70b75940b9aa0c71eac88d3912.png)
二、使用 ssh 方式的实现
这里以 github 为例,解说如何实现 ssh 方式操作仓库。
如果直接使用 git clone 下载某个仓库会提示如下信息,原因是没有添加 ssh 的 pub key 到 github 设置。
root@tw-demo-server:/var/www/html
Cloning into 'work-culture'...
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ECDSA key fingerprint is SHA256:WPIFJWPEROGJQ;ERKWLJQW;G.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) 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.
1. 打开用户设置
点击头像 -> 点击 Settings ![在这里插入图片描述](https://img-blog.csdnimg.cn/fb229f36efaf4797aaae887030149f39.png)
2. 定位到 SSH & GPG keys 菜单
这是用户的控制面板,在左侧菜单栏中定位到 SSH & GPG keys 这个菜单 ![在这里插入图片描述](https://img-blog.csdnimg.cn/115c3bc8445d489b82bf8b11bccd5caa.png)
3. 生成当前机器的密钥
官方说明:https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
你想让哪个机器实现免密登录就在哪个机器上生成密钥,虚拟机也算一台新的机器,因为它最终生成的密钥跟宿主机是不一样的。
打开命令行,执行以下指令
ssh-keygen -t ed25519 -C "这里输入邮箱"
然后一路回车即可。
![在这里插入图片描述](https://img-blog.csdnimg.cn/bf21faac3e304fbdbb1938c60a7640c4.png) 此时在用户目录下的 .ssh/ 目录中就会产出一个名为 id_ed25519.pub 的文件:
Linux 是在 ~/.ssh/ 目录下 Windows 是在 C:\Users\Administrator\.ssh
这是我的 .ssh 目录,里面的文件可能跟你的不同,不要紧,有这个文件就行 ![在这里插入图片描述](https://img-blog.csdnimg.cn/268a1b1b4d8742c7afc1516490ad2eaa.png) 查看并复制这个文件里面的内容
cat id_ed25519.pub
![在这里插入图片描述](https://img-blog.csdnimg.cn/b2673d9c0ff444caab1c1cf136443fb4.png)
4. 添加到 github 控制面板中
上一步已经将生成的 pub key 复制到剪贴板中,点击 New SSH key 打开添加 SSH key 面板。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/4513957750234d1f81893fc0ed3a9b58.png)
Title 用于标识你这个 key 是哪个机器什么的。Key type 用默认的就行。Key 就是上一步复制公开密钥 pub key 。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/d67a487ccc634bd29ae23ed26969e03c.png) 点击 Add SSH key 后,出现以下页面,现在你有两个选择- 一个是直接输入密码
- 一个是通过 github app 验证了
以下是使用 App 验证的过程 ![在这里插入图片描述](https://img-blog.csdnimg.cn/8c899d97de804c60bbb4c1ad12910d2f.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/c5d056582e614ba9af5a6985942cd4c6.png) 验证完成,或者输入密码之后,就直接返回 SSH key 列表了,就算添加完成了。
每次添加新的 SSH 都会收到 github 的邮件通知:
![在这里插入图片描述](https://img-blog.csdnimg.cn/331b01cb601a41acaa8e6a538032eaee.png)
5. 现在就可以直接 clone 仓库了
至此,这个机器以后再操作这个 github 账号的所有操作都不需要输入密码了。 clone commit push pull 都不需要密码。也能 clone 它的所有仓库,私密不私密的都可以。
![在这里插入图片描述](https://img-blog.csdnimg.cn/8ff24891901a443da96a3adf026ece89.png)
三、SSH key 的使用情况
通过查看 ssh key 列表,能看到这些 key 被使用的情况,最后一次使用是什么时候等。
![在这里插入图片描述](https://img-blog.csdnimg.cn/67f104074bbe4c1693964dba5e4de41f.png)
|