Skip to content

Git SSH 配置

发布于 2024-05-05

记录一下我使用 Git SSH 的配置方式及问题总结

关联远程 Git

查看 ssh 是否已存在

bash
ls -al ~/.ssh

默认情况下,GitHub支持的公共密钥文件名如下

  • id_rsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub

生成新的 SSH 密钥

bash
ssh-keygen -t ed25519 -C "your_email@example.com"

如果你使用的是不支持 Ed25519 算法的旧系统,请使用:

bash
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

将公钥添加到你的 Git ssh 配置

复制公钥内容

bash
cat ~/.ssh/id_ed25519.pub

复制后在远程仓库的设置中找到 SSH 设置,新建一个 SSH 密钥

测试 SSH 连接

运行

bash
ssh -T git@github.com

如果你的服务器在本地,可以运行:

bash
ssh -T -p 2222 git@localhost

你可能会看到这样的警告

bash
> The authenticity of host 'github.com (IP ADDRESS)' can't be established.
> ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
> Are you sure you want to continue connecting (yes/no)?

输入 yes 确认,如果匹配,会显示以下内容:

bash
> Hi USERNAME! You've successfully authenticated, but GitHub does not
> provide shell access.

关于 ssh 默认端口

如果本地部署的端口默认开放为 2222 而不是 22,不能直接使用 ssh 提交,

解决方法

方法 1:需要在提交时指定端口,如:

bash
git remote set-url origin ssh://git@localhost:2222/jack/demo1.git

方法 2:配置主机的 SSH 客户端,默认将 localhost 的 Git 请求转发到 2222 端口

编辑 ~/.ssh/config

bash
Host localhost
  HostName localhost
  User git
  Port 2222

之后可直接用原 URL:

bash
git remote set-url origin git@localhost:jack/demo1.git

ssh 连接错误

端口错误

远程连接提示以下报错:

bash
github ssh: connect to host github.com port 22: Connection refused

修改 SSH 配置

bash
# 修改 ~/.ssh/config
Host github.com
  HostName ssh.github.com  # 关键修改:指向正确的服务域名
  User git
  Port 443
  IdentityFile ~/.ssh/id_ed25519  # 注意:必须是私钥文件(去掉 .pub 后缀)
  IdentitiesOnly yes

验证命令

bash
ssh -T git@github.com

验证步骤

bash
# 1. 测试连接(应看到成功消息)
ssh -T git@github.com

# 2. 测试仓库克隆
git clone git@github.com:yourname/yourrepo.git

# 3. 查看实际使用的配置
ssh -vT git@github.com 2>&1 | grep -i "connecting to"

WSL2 DNS 错误

在 WSL2 中连接远程仓库提示以下报错

ssh: Could not resolve hostname ssh.github.com: Temporary failure in name resolution

临时修复 DNS(推荐先试试),在 WSL2 中执行:

bash
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'

然后测试:

bash
ssh -T git@github.com

如果能成功连接,说明是 DNS 问题。你可以通过以下命令让该配置在每次启动 WSL2 时自动生效:

bash
sudo chattr +i /etc/resolv.conf

⚠️ 注意:这可能会影响你切换网络时的 DNS 自动更新,建议仅在你当前网络稳定时使用 。

密钥验证失败

报错内容

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
SHA256:fccc85faf87ae70db09ce3545dfc48ff896a5d6e5109.
Please contact your system administrator.
Add correct host key in /c/Users/jack/.ssh/known_hosts to get rid of this message.
Offending RSA key in /c/Users/jack/.ssh/known_hosts:4
Host key for [localhost]:2222 has changed and you have requested strict checking.
Host key verification failed.

解决方法

删除旧的密钥记录

bash
ssh-keygen -R [localhost]:2222

这会从你的 known_hosts 文件中删除该主机的旧密钥记录