前言
系统:Ubuntu 22.04
参考链接:
- Generating a new SSH key and adding it to the ssh-agent - GitHub Docs
- 一文让你了解如何为 Git 设置代理 - Eric (ericclose.github.io)
目的:由于使用HTTP协议进行git提交/克隆,需要每次验证密码。并且由于Github的政策变化,commit的时候出现如下错误
Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.因此本文将对git配置SSH协议。
过程
-
配置基本信息
git config --global user.name "your_name" git config --global user.name "your_email@example.com" git config --global http.proxy http://127.0.0.1:7890 # 设置git的http协议通过代理使用 git config --list # 查看是否成功配置 -
生成SSH钥匙:执行如下命令,有提示全部默认Enter就好了
ssh-keygen -t ed25519 -C "your_email@example.com" -
添加SSH私匙到ssh-agent里
eval "$(ssh-agent -s)" # 启动ssh-agent ssh-add ~/.ssh/id_rsa # 添加私钥到ssh-agent,这样进行git的时候不用每次输密码 -
将SSH公钥加入github中:Settings ——> SSH and GPG keys ——> New SSH key ——> 将公钥输入
-
由于
天朝局域网的情况,此时还不能正常访问Github,会出现如下could not resolve hostname github.com: temporary failure in name resolution错误,表示无法连接到github。不信可以使用ssh -T git@github.com进行测试。需要对SSH连接进行代理设置。修改
~/.ssh/config文件,输入如下内容Host github.com User git ProxyCommand nc -X connect -x 127.0.0.1:7890 %h %p%pHost后面 接的github.com是指定要走代理的仓库域名。- 在 ProxyCommand 中,Linux 和 macOS 用户用的是
nc。 -X选项后面接的是connect的意思是 HTTPS 代理。-x选项后面加上代理地址和端口号。- 在调用 ProxyCommand 时,
%h和%p将会被自动替换为目标主机名和 SSH 命令指定的端口(%h和%p不要修改,保留原样即可)。
-
使用
ssh -T git@github.com测试是否可以正常访问github。 -
可以愉快地在Ubuntu上提交代码给全球最大的同性交友网站了。
git clone的时候,请使用SSH协议。