虽然github推荐使用https方式进行连接,但是ssh方式咱们也得会,官网上也都详细的文档介绍,步骤比较简单,记录这个目的是给初入门的小伙伴多一个学习的门径,同时也方便日后自己查看。
前言
- 在没有配置ssh的情况下使用ssh连接操作github库的时候会出现如下异常:
$ git clone git@github.com:ShaoZeMing/xunsearch-laravel.git
Cloning into 'xunsearch-laravel'...
Warning: Permanently added the RSA host key for IP address '192.30.252.129' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
~
步骤
- 前提是我们已经新建好了一个库xunsearch-laravel,ssh路径是:git@github.com:ShaoZeMing/xunsearch-laravel.git
检查ssh密钥是否存在
$ ls -al ~/.ssh
如果目录下面没有id_rsa,id_rsa.pub则表示键不存在
生成ssh密钥
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Hunter/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Hunter/.ssh/id_rsa.
Your public key has been saved in /c/Users/Hunter/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:7KwlOZ4yljBZE2ZJ7dr8QGIyQeiPk49L+01fnC0hAZY your_email@example.com
The key's randomart image is:
+---[RSA 4096]----+
| o...=. |
|. . *Eo |
|. + o . |
| .o = o.. |
| +* B .S. |
| ++.. ++o + |
| .+o o+o+= . |
|....B..*o . |
| ooo ++. |
+----[SHA256]-----+
将ssh key添加到ssh-agent
- 先确认ssh-agent是可用的
$ eval $(ssh-agent -s)
Agent pid 20632
- 将ssh key添加到ssh-agent
$ ssh-add ~/.ssh/id_rsa
Identity added: /c/Users/Hunter/.ssh/id_rsa (/c/Users/Hunter/.ssh/id_rsa)
将ssh密钥配置到github
- 复制键内容
$ clip < ~/.ssh/id_rsa.pub
注:如果没有clip命令,可执行命令 vi ~/.ssh/id_rsa.pub
复制秘钥
配置键到github
登录github->选择设置 - > SSH密钥 - >新的SSH密钥
测试ssh键的配置情况
$ ssh -t git@github.com
Warning: Permanently added the RSA host key for IP address '192.30.252.128' to the list of known hosts.
PTY allocation request failed on channel 0
到这里就配置好了
再次执行clone操作:
$ git clone git@github.com:ShaoZeMing/xunsearch-laravel.git
Cloning into 'test-git'...
remote: Counting objects: 56, done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 56 (delta 4), reused 0 (delta 0), pack-reused 8
Receiving objects: 100% (56/56), 5.42 KiB | 0 bytes/s, done.
Resolving deltas: 100% (4/4), done.
Checking connectivity... done.