github上拉取自己的私有项目,现在不允许使用 password方式,改用ssh方式
1>将自己主机上的 ~/.ssh/id_rsa.pub 复制出来,粘贴到github的个人settings下的 ssh key下
2>直接 git clone git@github.com:xxx/vue-blog.git
可能会报:
Cloning into 'vue-blog'...
ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
这个可能是本机的22端口被屏蔽(通过 telnet github.com 22 测试一下,大概率超时),解决方案使用443:
$ sudo vim ~/.ssh/config
在里面配置如下内容(port也可以配置为22,然后试一试):
Host github.com
Hostname ssh.github.com
User git
Port 443
IdentityFile ~/.ssh/id_rsa
然后保存,通过下面的命令测试是否可以连接github
$ ssh -T git@github.com
Hi gspgsp! You've successfully authenticated, but GitHub does not provide shell access.
3>再次执行 git clone git@github.com:xxx/vue-blog.git 就可以拉下代码
4>配置分析
User git 这一行表示 连接 GitHub 时使用 git 用户,这是 GitHub SSH 访问的标准方式。
在 ~/.ssh/config 里,User git 告诉 SSH 连接 GitHub 时使用 git 用户
相当于让 SSH 在执行 ssh git@github.com 时 默认使用 git 作为用户名。
ssh -T git@github.com
这里等价于
ssh -T github.com
因为 ~/.ssh/config 里已经指定 User git
5>如果需要重新生成ssh-key,可以执行
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
也可以执行
ssh-keygen -t ed25519 -C "your_email@example.com"
二者对比:
RSA:
算法:RSA(Rivest–Shamir–Adleman)
密钥长度:你指定了 -b 4096(推荐,默认是 2048)
兼容性:适用于所有 SSH 服务器和旧系统(兼容性最好)
安全性:4096 位密钥仍然安全,但 RSA 算法随着计算能力提升,未来可能不如更现代的算法
速度:相比 Ed25519,RSA 密钥较长,加密/解密稍慢
适用场景:如果你需要在一些 旧系统(比如老版本 OpenSSH) 上使用 SSH,建议选 RSA
Ed25519(更现代的算法):
算法:Ed25519(基于椭圆曲线)
密钥长度:固定 256 位(更短,但安全性比 4096 位 RSA 还强)
兼容性:需要 OpenSSH 6.5+(2014 年之后的系统一般都支持)
安全性:比 RSA 2048/4096 更安全,抗量子攻击能力更强
速度:更快(计算效率高,适合高并发)
适用场景:适用于 GitHub、GitLab、服务器 SSH 登录,如果你不需要支持 非常旧的 SSH 服务器,推荐使用 Ed25519
