github/git密钥配置与使用

发布于:2024-10-09 ⋅ 阅读:(136) ⋅ 点赞:(0)

零、前言

因为要在ubuntu上做点东西,发现git clone 的时候必须输账户密码,后来发现密码是token,但是token一大串太烦了,忙了一天发现可以通过配置 公钥 来 替代 http 的 部署方式。


一、生成 ssh 密钥对

我们先测试下能不能 连接上,发现直接 deny 了

root@LAPTOP-292TJJC6:~/.ssh# ssh -T git@github.com
The authenticity of host '[ssh.github.com]:443 ([20.205.243.160]:443)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[ssh.github.com]:443,[20.205.243.160]:443' (ECDSA) to the list of known hosts.
git@ssh.github.com: Permission denied (publickey).

然后在 终端中输入 ssh-keygen

第一个让设置放在哪我们直接回车不用管

然后会让你输入一个密码,确认密码

root@LAPTOP-292TJJC6:~# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub

此时就会给我们生成一个密钥,我这里是在 /root/.ssh/id_rsa

我们进入,发现有还有另一个文件: id_rsa.pub

root@LAPTOP-292TJJC6:~# cd /root/.ssh/
root@LAPTOP-292TJJC6:~/.ssh# ll
total 24
drwx------ 2 root root 4096 Oct  3 21:13 ./
drwx------ 9 root root 4096 Oct  3 21:11 ../
-rw-r--r-- 1 root root   58 Oct  3 21:11 config
-rw------- 1 root root 2655 Oct  3 21:14 id_rsa
-rw-r--r-- 1 root root  574 Oct  3 21:14 id_rsa.pub
-rw-r--r-- 1 root root  888 Oct  3 21:11 known_hosts

pub 即 public,就是公钥(不带pub的那个就是私钥)

我们打开复制,然后进入下一个part

二、配置 GitHub 公钥

进入 settings

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

进入 SSH and GPG keys

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

点击创建一个新的 SSH key

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

然后把我们的公钥粘贴到下面,点确认就好了

三、测试链接

我们回到Ubuntu来测试一下行不行。

root@LAPTOP-292TJJC6:~/.ssh# ssh -T git@github.com
Enter passphrase for key '/root/.ssh/id_rsa':
Hi 58164! You've successfully authenticated, but GitHub does not provide shell access.

这个时候 git clone 也能通过 ssh 来部署了 哈铪(搞一天快哭了

tHub does not provide shell access.


这个时候 git clone 也能通过 ssh 来部署了 哈铪(搞一天快哭了


网站公告

今日签到

点亮在社区的每一天
去签到