注意事项:
1)以下所有命令都是在git bash中执行;
2)由于默认.ssh目录路径中包含中文,执行ssh-keygen时不使用默认路径,指定另外的密钥文件路径,比如:/c/.ssh/id_rsa ;
3)需要提前在c盘生成.ssh目录,并生成一个空白的/c/.ssh/id_rsa文件;
4)"xxxxxx@xxx.com"需要修改为你个人email;
首先执行ssh看看域名解析有没有问题,如果有问题,如下:
$ ssh -T git@github.com
ssh: Could not resolve hostname github.com: No address associated with hostname
这时手动设置dns ip地址为8.8.8.8及8.8.4.4
参考下面的文章:
https://zhongguo.eskere.club/%E5%A6%82%E4%BD%95%E5%9C%A8-windows-11-%E4%B8%AD%E6%9B%B4%E6%94%B9-dns-%E8%AE%BE%E7%BD%AE/2021-10-13/
设置完后,再次执行ssh,提示permission denied,如下:
$ ssh -T git@github.com
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
git@github.com: Permission denied (publickey).
然后执行ssh-keygen并指定key文件为/c/.ssh/id_rsa,如下:
$ ssh-keygen -t rsa -b 4096 -C "xxxxxx@xxx.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/xxxxx/.ssh/id_rsa): /c/.ssh/id_rsa
/c/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/.ssh/id_rsa
Your public key has been saved in /c/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:RKP9snwKcQ+nqBnS+I6tMt8LOXcUXXrnZd51bN6HeKk xxxxxx@xxx.com
The key's randomart image is:
+---[RSA 4096]----+
| o . |
| = + . |
| o = . . o =|
| o o o = *+|
| o S o o = =|
| + . = B o .|
| * + + + o E |
|o X = . o |
| ++oO. . |
+----[SHA256]-----+
然后,拷贝id_rsa.pub文件内容并粘贴到github profile上去:
Github profile -> Settings -> SSH and GPG Keys -> Add new SSH key
接着执行ssh-add,出现如下提示:
$ ssh-add /c/.ssh/id_rsa
Could not open a connection to your authentication agent.
然后执行:
$ eval $(ssh-agent) 或者 eval $(ssh-agent -s)
Agent pid 1118
OK后,接着又执行ssh-add,这次OK了:
$ ssh-add /c/.ssh/id_rsa
Identity added: /c/.ssh/id_rsa (xxxxxx@xxx.com)
继续执行:
$ ssh -T git@github.com
Hi xxxxxx! You've successfully authenticated, but GitHub does not provide shell access.
对了,如果没有配置.gitconfig的话,要配置:
git config --global user.name "xxxxxx"
git config --global user.email "xxxxxx@xxx.com"
然后就可以git clone啦。