由于SSH配置文件的错误,导致的Permission denied (publickey)及其解决方法

本文介绍在使用GitHub for Windows 2.0时遇到SSH密钥认证失败的问题及解决方案。主要原因是默认配置文件中的密钥文件路径与实际路径不符,提供三种解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读者如要转载,请标明出处和作者名,谢谢。
地址01:http://space.itpub.net/25851087
地址02:http://www.cnblogs.com/zjrodger/
作者名:zjrodger

 
【问题发生环境和相关参数】

(1)OS:Win7 32Bit.

(2)Git:GitHub for Windows 2.0.

    下载地址:https://windows.github.com/

(3)Command Shell:Git Shell.

 

【问题重现描述】

    在Win7本地平台上安装完Github for Windows 2.0后,要在本地生成密匙,向Github上上传密匙并且进行网络连通性测试。

    ①利用命令“ssh-keygen -t rsa -C "zjrodger@163.com" 生成SSH密匙(id_rsaid_rsa.pub)后,将本地的“id_rsa.pub”文件中的内容上传到Github上的个人“SSH Keys”管理项中,从而生成一个新的SSH Keys。
    ②之后,进行本地与Remote Server(Github网站)的连接测试,命令和结果如下所示:
F:\Workspaces\Github_Workspace> ssh -T git@github.com 
Warning: Permanently added 'github.com,192.30.252.131' (RSA) to the list of know 
n hosts. 
Permission denied (publickey).
 在Windows的PowerShell中输入“ssh -T git@github.com”,结果出现“Permission Denied(publickey)
 
 
【问题原因】

    在Github for Windows 2.0默认的安装配置中,

SSH的配置文件ssh_config中的IdentityFile“与实际情况不相符。
 
(1)原来默认情况下的IdentifyFile的值
    在Github for Windows 2.0上(默认安装情况下),SSH的配置文件ssh_config中的IdentityFile(其值为密匙的全路径名这项信息的内容是“~/.ssh/github_rsa”,如下命令所示:
Host github.com
 StrictHostKeyChecking no
 UserKnownHostsFile=/dev/null
 IdentityFile=~/.ssh/github_rsa
 
(2)实际的情形
实际上,通过命令“ssh-keygen -t rsa -C "zjrodger@163.com”生成的新的SSH密匙全路径名为:~/.ssh/id_rsa~/.ssh/id_rsa.pub”。
注意~/.ssh/github_rsa  不等于 ~/.ssh/id_rsa
 
(3)结论

①Git默认安装情况下,ssh_config配置文件中的“IdentityFile”项的值:IdentityFile=~/.ssh/github_rsa

②实际的IdentityFile的值:IdentityFile=~/.ssh/id_rsa
    如上所述,Github for Windows 2.0默认安装情况下,SSH的的配置文件ssh_config中的IdentityFile项的值与实际新创建的密匙全路径名不相符,结果导致本地的SSH工具无法找到到正确的密匙,进而无法同已经上传到Github密匙相匹配,结果就出现了“Permission denied (publickey)”这样的错误。
 
(4)补充
SSH配置文件ssh_config在自己本地的路径:
    C:\Users\Administrator\AppData\Local\GitHub\PortableGit_6d98349f44ba975cf6c762a720f8259a267ea445\etc\ssh
密匙文件的存放路径:
    C:\Users\Administrator\.ssh
ssh_config的原文件(有误的版本):

Host *
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null

Host github.com
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
IdentityFile=~/.ssh/github_rsa

重装Github for windows 2.0后的新发现(重要)
    为了确保正确性,自己将本机的Github for Window 2.0卸载并且重装了一遍,发现SSH的配置文件ssh_config中的IdentityFile依然是“~/.ssh/github_rsa”。
而重装后的密匙文件的存放路径(C:\Users\Administrator\.ssh)下,有四个密匙文件,分别是github_rsagithub_rsa.pubid_rsaid_rsa.pub
这样,用户就不用自己新建密匙文件了,只用将“github_rsa.pub”中的内容上传到Github网站的个人SSH管理中即可。
之后,在本地与Remote端进行网络连通性测试,发现可以联通。
这样,考虑到修改软件原有配置信息所带来的隐患,笔者就不推荐自己手动修改SSH的配置文件ssh_config中的IdentityFile字段这个方法了。
 
【解决方法】
方法一: 
   在生成新的密匙文件后,若新生成的密匙文件名字为“id_rsa”,则将ssh_config配置文件中的“IdentityFile”项的值改为“~/.ssh/id_rsa
方法二: 
    将新生成的密匙文件名字改为“github_rsa”,从而与ssh_config配置文件中的“IdentityFile”项的值相同。
方法三:
    重装Github for Window 2.0,不用新建密匙文件,而是用Github自带的“github_rsa.pub”文件。
总之,不论方法一,放法二还是方法三,一定要保持新生成的密匙文件的名字同“ssh_config”中“IdentityFile”字段的值一致即可。
   
 
【参考文档】
(1)原文链接:http://stackoverflow.com/questions/2127104/permission-denied-publickey-error-using-git-on-windows-7

If it says "Permission denied (publickey)" you will have to put in a passphrase for your key. Do not be tempted to just press enter...this was what worked for me...it took me five hours to realize that pressing enter made OpenSSH feel that your key was too public so that is why it is denying you from going to the next step.

(2)原文链接:http://stackoverflow.com/questions/17383177/permission-denied-publickey-errors-on-windows-when-using-moovweb
So as mentioned in prior answers, the Permission denied error in Windows is because you are trying to use a key other than id_rsa.
Windows lacks the bells and whistles that Linux and Mac have to try out all your public keys when trying to connect to a server via SSH. If you're using the ssh command, you can tell it which key to use by passing the -i flag followed by the path to the key to use:
F:\Workspaces\Github_Workspace> ssh -T git@github.com 
Warning: Permanently added 'github.com,192.30.252.129' (RSA) to the list of know 
n hosts. 
Permission denied (publickey). 
F:\Workspaces\Github_Workspace> ssh -i ~/.ssh/id_rsa git@github.com 
Warning: Permanently added 'github.com,192.30.252.129' (RSA) to the list of know 
n hosts. 
Enter passphrase for key '/c/Users/Administrator/.ssh/id_rsa': 
Hi zjrodger! You've successfully authenticated, but GitHub does not provide shel 
l access. 
Connection to github.com closed.

 

 

<think>好的,我现在需要帮助用户解决Git配置时遇到的“Permission denied (publickey)错误。首先,我得回想一下这个问题的常见原因和解决方法。根据用户提供的引用内容,特别是引用[3]提到SSH_AUTH_SOCK环境变量的问题,还有其他引用提到的密钥生成和配置步骤。 首先,应该检查SSH密钥是否存在。用户可能没有生成正确的密钥,或者生成的密钥没有添加到ssh-agent中。引用[2]提到用户可能自定义了密钥文件名,导致Git无法自动识别。这时候需要确认密钥是否在默认路径~/.ssh下,或者是否在配置中指定了正确的文件。 然后,确保公钥已经正确添加到Git服务器(如GitHub)的账户设置中。如果公钥未添加或添加错误,会导致权限被拒绝。引用[1]中的错误信息可能与此有关。 接下来,检查SSH代理是否运行,并且密钥是否被添加进去。如果ssh-agent没有运行,或者密钥未被添加,SSH连接时无法使用对应的私钥进行认证。引用[3]提到SSH_AUTH_SOCK环境变量的问题,可能需要通过eval命令启动ssh-agent并添加密钥。 另外,需要验证SSH连接是否正常。使用ssh -T命令测试连接到Git服务器,可以查看详细的错误信息,帮助诊断问题。 可能还需要检查文件权限,确保.ssh目录和密钥文件的权限设置正确,过于开放的权限会导致SSH拒绝使用这些密钥。 用户可能的问题步骤包括:生成密钥时使用了非默认名称,未正确配置SSH配置文件,或者未将密钥添加到代理。需要一步步引导用户检查这些方面。 总结下来,解决步骤应该是: 1. 检查密钥是否存在,位置是否正确。 2. 确认公钥已添加到Git服务器。 3. 启动ssh-agent并添加私钥。 4. 测试SSH连接。 5. 检查文件权限。 最后,生成相关问题,帮助用户深入了解相关配置和故障排除方法。</think>### 解决 Git "Permission denied (publickey)" 错误的逐步指南 #### 1. 验证 SSH 密钥是否存在 **操作步骤:** - 打开终端,输入命令查看密钥文件: ```bash ls -al ~/.ssh ``` - 检查是否存在 `id_rsa` 和 `id_rsa.pub`(或自定义名称如 `xxx.github`)文件。若不存在,需生成新密钥: ```bash ssh-keygen -t ed25519 -C "your_email@example.com" # 推荐使用ed25519算法 ``` - 提示保存路径时,**建议使用默认路径**(直接回车)。若使用自定义路径(如 `xxx.github`),需额外配置(见步骤3)[^2]。 #### 2. 添加公钥到 Git 服务器 **操作步骤:** - 复制公钥内容(例如 `id_rsa.pub`): ```bash cat ~/.ssh/id_rsa.pub | pbcopy # Mac 直接复制到剪贴板 ``` - 登录 GitHub/GitLab,进入 `Settings -> SSH and GPG keys`,粘贴公钥并保存[^1]。 #### 3. 配置 SSH 客户端(自定义密钥路径时必需) **操作步骤:** - 创建或修改 `~/.ssh/config` 文件: ```bash Host github.com HostName github.com User git IdentityFile ~/.ssh/xxx.github # 替换为你的自定义密钥路径 IdentitiesOnly yes ``` - 设置文件权限: ```bash chmod 600 ~/.ssh/config ``` #### 4. 启动 ssh-agent 并添加私钥 **操作步骤:** - 确保 ssh-agent 在后台运行: ```bash eval "$(ssh-agent -s)" # 输出应为 "Agent pid XXXX" ``` - 添加私钥到代理: ```bash ssh-add ~/.ssh/id_rsa # 或自定义密钥路径(如 ~/.ssh/xxx.github) ``` - 若提示 `Could not open a connection to your authentication agent`,说明 ssh-agent 未启动,需先执行上一步的 `eval` 命令[^3]。 #### 5. 测试 SSH 连接 **操作步骤:** - 执行连接测试命令: ```bash ssh -T git@github.com # GitHub 测试 ``` - 成功时会显示: ```text Hi username! You've successfully authenticated... ``` - 若仍失败,尝试详细调试模式: ```bash ssh -vT git@github.com # 查看具体错误日志 ``` #### 6. 检查文件权限 **操作要求:** - `.ssh` 目录权限需为 `700`: ```bash chmod 700 ~/.ssh ``` - 私钥文件权限需为 `600`: ```bash chmod 600 ~/.ssh/id_rsa ``` --- ### 关键问题总结表 | 问题现象 | 解决方案 | |---------|----------| | 密钥文件路径错误 | 检查 `~/.ssh` 目录或通过 `ssh -vT` 查看日志 | | 公钥未正确添加 | 重新复制公钥内容到 Git 服务器 | | ssh-agent 未加载私钥 | 执行 `ssh-add` 并检查代理状态 | | 文件权限过大 | 使用 `chmod` 修正权限 | --- §§ 1. 如何为不同的 Git 仓库配置多个 SSH 密钥? 2. 使用 HTTPS 和 SSH 协议克隆仓库有什么区别? 3. 如何彻底卸载并重新配置 Git 的 SSH 设置? : git clone代码出现Permission denied (publickey,password)。 : 【Git】解决 git pull 提示 Permission denied (publickey) 的问题。 [^3]: git配置时Permission denied (publickey)错误解决
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值