在 Windows 环境下解决 SSH 连接 GitHub 持续要求密码的问题需要特别注意 Windows 系统的特性。以下是针对 Windows 的完整解决方案:
所有命令都应在 ??Git Bash?? 中执行(不是 CMD 或 PowerShell)
1 2 |
# 打开配置文件 notepad ~/.ssh/config |
确保内容为:
1 2 3 4 5 |
Host github.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519_github IdentitiesOnly yes |
1 |
ls -la ~/.ssh |
应该看到:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# 设置 .ssh 目录权限 icacls "$HOME/.ssh" /reset icacls "$HOME/.ssh" /inheritance:r icacls "$HOME/.ssh" /grant:r "$USERNAME:(OI)(CI)F"
# 设置私钥权限 icacls "$HOME/.ssh/id_ed25519_github" /reset icacls "$HOME/.ssh/id_ed25519_github" /grant:r "$USERNAME:F"
# 设置公钥权限 icacls "$HOME/.ssh/id_ed25519_github.pub" /reset icacls "$HOME/.ssh/id_ed25519_github.pub" /grant:r "$USERNAME:F"
# 设置配置文件权限 icacls "$HOME/.ssh/config" /reset icacls "$HOME/.ssh/config" /grant:r "$USERNAME:F" |
1 2 3 4 5 6 7 8 |
# 启动 SSH 代理 eval $(ssh-agent -s)
# 添加 GitHub 密钥 ssh-add ~/.ssh/id_ed25519_github
# 验证密钥已添加 ssh-add -l |
1 2 3 4 5 6 |
# 安装 dos2unix(如果未安装) curl -L https://sourceforge.net/projects/dos2unix/files/dos2unix/7.4.1/dos2unix-7.4.1-win64.zip/download -o dos2unix.zip unzip dos2unix.zip -d /usr/bin
# 转换 config 文件格式 dos2unix ~/.ssh/config |
1 |
ssh -Tv git@github.com |
在输出中查找:
1 2 |
debug1: Offering public key: /c/Users/yourusername/.ssh/id_ed25519_github ED25519 SHA256:... debug1: Server accepts key: /c/Users/yourusername/.ssh/id_ed25519_github ED25519 SHA256:... |
在 ~/.ssh/config 文件中,使用以下格式:
1 |
IdentityFile C:/Users/yourusername/.ssh/id_ed25519_github |
而不是:
1 |
IdentityFile ~/.ssh/id_ed25519_github # 在 Windows 可能有问题 |
Windows 可能需要手动启动 SSH 代理服务:
1 2 3 4 5 |
# 确保 SSH 代理运行 eval $(ssh-agent -s)
# 如果提示找不到代理,手动启动 ssh-agent -s |
确保 Git Bash 的环境变量设置正确:
1 2 3 4 5 |
# 检查 HOME 变量 echo $HOME
# 如果未设置,手动设置 export HOME=/c/Users/yourusername |
1 |
ssh-add /c/Users/yourusername/.ssh/id_ed25519_github |
显示公钥内容:
1 |
cat ~/.ssh/id_ed25519_github.pub |
登录 GitHub > Settings > SSH and GPG keys
确保:
1 2 3 4 5 6 |
# 备份旧配置 mv ~/.ssh ~/.ssh_backup
# 创建新配置 mkdir ~/.ssh notepad ~/.ssh/config |
粘贴:
1 2 3 4 5 |
Host github.com HostName github.com User git IdentityFile C:/Users/yourusername/.ssh/id_ed25519_github IdentitiesOnly yes |
1 2 3 4 5 |
# 生成新密钥 ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_github -C "your_email@example.com"
# 添加到 GitHub clip < ~/.ssh/id_ed25519_github.pub # 复制到剪贴板 |
1 2 3 4 5 6 |
# 删除所有保存的凭据 git credential-manager reject https://github.com git credential-manager reject git@github.com
# 清除缓存 git credential-manager clear |
1 |
ssh -T git@github.com |
成功时应显示:
1 |
Hi yourusername! You've successfully authenticated, but GitHub does not provide shell access. |
1 2 |
# 设置全局使用 HTTPS git config --global url."https://github.com/".insteadOf "git@github.com:" |
安装 GitHub Desktop 并登录您的账户,它会自动处理认证
1 2 3 4 5 |
# 安装 GitHub CLI winget install --id GitHub.cli
# 登录 GitHub gh auth login |
步骤 | 命令 | 预期结果 |
---|---|---|
1. 验证配置文件 | cat ~/.ssh/config | 显示正确的 GitHub 配置 |
2. 检查密钥文件 | ls -la ~/.ssh | 显示私钥和公钥文件 |
3. 设置权限 | icacls 命令 | 无错误输出 |
4. 启动代理 | eval $(ssh-agent -s) | 显示代理 PID |
5. 添加密钥 | ssh-add ~/.ssh/id_ed25519_github | "Identity added" 消息 |
6. 测试连接 | ssh -T git@github.com | 成功认证消息 |
请按照这些步骤操作,特别注意 Windows 的文件权限和路径格式问题。如果问题仍然存在,请提供 ssh -Tv git@github.com 的完整输出,我可以进一步帮助诊断。