问题描述
在上一篇博客中,解决了无法从git拉取仓库到本地的问题,但是当时的办法并未完全解决这个问题,当时只能通过HTTPS协议拉取git仓库,下面介绍如何通过SSH从git拉取仓库,过程参考了这篇博客。
生成SSH密钥对
cd ~/.ssh
ssh-keygen -t ed25519 -C "yang1294891635@gmail.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/yukeyang/.ssh/id_ed25519): id_ed25519
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_ed25519
Your public key has been saved in id_ed25519.pub
The key fingerprint is:
SHA256:/V+VhNBWst//P3fRGfF/1mVEOH3UCuOPI5ZwidxEqNo yang1294891635@gmail.com
The key's randomart image is:
+--[ED25519 256]--+
| o.....*+|
| . . +o*.=|
| o + o.= =+|
| . +.+ . +.*|
| o So.. o +X|
| . E +.o .oO|
| . ... .=|
| . o+|
| . B|
+----[SHA256]-----+
执行上面的命令后,会在命令窗输出公钥的信息,我的公钥是SHA256:/V+VhNBWst//P3fRGfF/1mVEOH3UCuOPI5ZwidxEqNo yang1294891635@gmail.com
同时,会在当前目录生成id_ed25519和id_ed25519.pub这两个密钥对文件,pub后缀名的文件是公钥,另一个是私钥。
yukeyang@DESKTOP-QFK2F47:~/myfiles/assignment-2-yang-yuke$ ls -a ~/.ssh/
. .. id_ed25519 id_ed25519.pub id_rsa id_rsa.pub known_hosts
将公钥添加到git
打开github,点击右上角个人图标,然后点击Settings,进入设置界面,然后点击SSH and GPG keys,再点击New SSH Key。
将上面的公钥信息填到Key文本框里,然后点击Add SSH Key。
添加私钥到本地的SSH agent
yukeyang@DESKTOP-QFK2F47:~/myfiles/assignment-2-yang-yuke$ eval "$(ssh-agent -s)"
Agent pid 553
yukeyang@DESKTOP-QFK2F47:~/myfiles/assignment-2-yang-yuke$ ssh-add ~/.ssh/id_ed25519
Identity added: /home/yukeyang/.ssh/id_ed25519 (yang1294891635@gmail.com)
生成配置文件
到这里,虽然把SSH公钥添加到了github上,把私钥也添加到了SSH agent,但还需要配置文件,否则还是可能无法通过SSH连接到github服务器。
输入以下命令,在~/.ssh目录下创建名为config的文件。
yukeyang@DESKTOP-QFK2F47:~/myfiles/assignment-2-yang-yuke$ cd ~/.ssh
yukeyang@DESKTOP-QFK2F47:~/.ssh$ vim config
按照以下文本格式修改你的config文件,User改成你自己的邮箱即可。
Host github.com
User yang1294891635@gmail.com
Hostname ssh.github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519
Port 443
测试SSH连接
完成上述步骤后,应该就可以通过SSH连接git服务器了,输入以下命令测试一下,可以看到,已经可以通过SSH连接git服务器了。
yukeyang@DESKTOP-QFK2F47:~/.ssh$ ssh 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.
PTY allocation request failed on channel 0
Hi yang-yuke! You've successfully authenticated, but GitHub does not provide shell access.
Connection to ssh.github.com closed.
通过SSH协议拉取git仓库
复制git仓库的SSH协议的URL。
执行以下命令,使用SSH类型的URL来拉取git仓库,可以看到,已经可以成功地拉取git仓库到本地了。文章来源:https://www.toymoban.com/news/detail-819533.html
yukeyang@DESKTOP-QFK2F47:~/myfiles/assignment-2-yang-yuke$ git remote add assignments-base git@github.com:cu-ecen-aeld/aesd-assignments.git
yukeyang@DESKTOP-QFK2F47:~/myfiles/assignment-2-yang-yuke$ git fetch assignments-base
remote: Enumerating objects: 339, done.
remote: Counting objects: 100% (87/87), done.
remote: Compressing objects: 100% (41/41), done.
remote: Total 339 (delta 62), reused 46 (delta 46), pack-reused 252
Receiving objects: 100% (339/339), 55.26 KiB | 73.00 KiB/s, done.
Resolving deltas: 100% (130/130), done.
From github.com:cu-ecen-aeld/aesd-assignments
* [new branch] assignment2 -> assignments-base/assignment2
* [new branch] assignment3-part-1 -> assignments-base/assignment3-part-1
* [new branch] assignment3-part-2 -> assignments-base/assignment3-part-2
* [new branch] assignment4 -> assignments-base/assignment4
* [new branch] assignment5 -> assignments-base/assignment5
* [new branch] assignment6 -> assignments-base/assignment6
* [new branch] assignment7 -> assignments-base/assignment7
* [new branch] assignment8 -> assignments-base/assignment8
* [new branch] assignment9 -> assignments-base/assignment9
* [new branch] finder-test-fix -> assignments-base/finder-test-fix
* [new branch] master -> assignments-base/master
* [new branch] update-assignment-autotest -> assignments-base/update-assignment-autotest
* [new branch] yaml-fix-A3P1 -> assignments-base/yaml-fix-A3P1
* [new branch] yaml-fix-A3P2 -> assignments-base/yaml-fix-A3P2
* [new branch] yaml-fix-a1 -> assignments-base/yaml-fix-a1
* [new branch] yaml-fix-a2 -> assignments-base/yaml-fix-a2
参考
[1] 解决git clone无法克隆开源代码问题:ssh: connect to host github.com port 22: Connection refused文章来源地址https://www.toymoban.com/news/detail-819533.html
到了这里,关于WSL无法通过SSH从git拉取仓库代码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!