1人参与 • 2025-06-25 • Windows
在 windows 环境下解决 ssh 连接 github 持续要求密码的问题需要特别注意 windows 系统的特性。以下是针对 windows 的完整解决方案:
所有命令都应在 git bash 中执行(不是 cmd 或 powershell)
# 打开配置文件 notepad ~/.ssh/config
确保内容为:
host github.com hostname github.com user git identityfile ~/.ssh/id_ed25519_github identitiesonly yes
ls -la ~/.ssh
应该看到:
id_ed25519_github
(私钥)id_ed25519_github.pub
(公钥)# 设置 .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"
# 启动 ssh 代理 eval $(ssh-agent -s) # 添加 github 密钥 ssh-add ~/.ssh/id_ed25519_github # 验证密钥已添加 ssh-add -l
# 安装 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
ssh -tv git@github.com
在输出中查找:
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
文件中,使用以下格式:
identityfile c:/users/yourusername/.ssh/id_ed25519_github
而不是:
identityfile ~/.ssh/id_ed25519_github # 在 windows 可能有问题
windows 可能需要手动启动 ssh 代理服务:
# 确保 ssh 代理运行 eval $(ssh-agent -s) # 如果提示找不到代理,手动启动 ssh-agent -s
确保 git bash 的环境变量设置正确:
# 检查 home 变量 echo $home # 如果未设置,手动设置 export home=/c/users/yourusername
ssh-add /c/users/yourusername/.ssh/id_ed25519_github
显示公钥内容:
cat ~/.ssh/id_ed25519_github.pub
登录 github > settings > ssh and gpg keys
确保:
# 备份旧配置 mv ~/.ssh ~/.ssh_backup # 创建新配置 mkdir ~/.ssh notepad ~/.ssh/config
粘贴:
host github.com hostname github.com user git identityfile c:/users/yourusername/.ssh/id_ed25519_github identitiesonly yes
# 生成新密钥 ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_github -c "your_email@example.com" # 添加到 github clip < ~/.ssh/id_ed25519_github.pub # 复制到剪贴板
# 删除所有保存的凭据 git credential-manager reject https://github.com git credential-manager reject git@github.com # 清除缓存 git credential-manager clear
ssh -t git@github.com
成功时应显示:
hi yourusername! you've successfully authenticated, but github does not provide shell access.
# 设置全局使用 https git config --global url."https://github.com/".insteadof "git@github.com:"
安装 github desktop 并登录您的账户,它会自动处理认证
# 安装 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
的完整输出,我可以进一步帮助诊断。
到此这篇关于windows环境git配置多个ssh的实现步骤的文章就介绍到这了,更多相关git配置多个ssh内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论