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

本文主要是介绍由于SSH配置文件的错误,导致的Permission denied (publickey)及其解决方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

读者如要转载,请标明出处和作者名,谢谢。
地址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.

 

 

这篇关于由于SSH配置文件的错误,导致的Permission denied (publickey)及其解决方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/729332

相关文章

PyInstaller打包selenium-wire过程中常见问题和解决指南

《PyInstaller打包selenium-wire过程中常见问题和解决指南》常用的打包工具PyInstaller能将Python项目打包成单个可执行文件,但也会因为兼容性问题和路径管理而出现各种运... 目录前言1. 背景2. 可能遇到的问题概述3. PyInstaller 打包步骤及参数配置4. 依赖

鸿蒙中Axios数据请求的封装和配置方法

《鸿蒙中Axios数据请求的封装和配置方法》:本文主要介绍鸿蒙中Axios数据请求的封装和配置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.配置权限 应用级权限和系统级权限2.配置网络请求的代码3.下载在Entry中 下载AxIOS4.封装Htt

解决SpringBoot启动报错:Failed to load property source from location 'classpath:/application.yml'

《解决SpringBoot启动报错:Failedtoloadpropertysourcefromlocationclasspath:/application.yml问题》这篇文章主要介绍... 目录在启动SpringBoot项目时报如下错误原因可能是1.yml中语法错误2.yml文件格式是GBK总结在启动S

Redis实现延迟任务的三种方法详解

《Redis实现延迟任务的三种方法详解》延迟任务(DelayedTask)是指在未来的某个时间点,执行相应的任务,本文为大家整理了三种常见的实现方法,感兴趣的小伙伴可以参考一下... 目录1.前言2.Redis如何实现延迟任务3.代码实现3.1. 过期键通知事件实现3.2. 使用ZSet实现延迟任务3.3

idea maven编译报错Java heap space的解决方法

《ideamaven编译报错Javaheapspace的解决方法》这篇文章主要为大家详细介绍了ideamaven编译报错Javaheapspace的相关解决方法,文中的示例代码讲解详细,感兴趣的... 目录1.增加 Maven 编译的堆内存2. 增加 IntelliJ IDEA 的堆内存3. 优化 Mave

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

如何解决mmcv无法安装或安装之后报错问题

《如何解决mmcv无法安装或安装之后报错问题》:本文主要介绍如何解决mmcv无法安装或安装之后报错问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mmcv无法安装或安装之后报错问题1.当我们运行YOwww.chinasem.cnLO时遇到2.找到下图所示这里3.

浅谈配置MMCV环境,解决报错,版本不匹配问题

《浅谈配置MMCV环境,解决报错,版本不匹配问题》:本文主要介绍浅谈配置MMCV环境,解决报错,版本不匹配问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录配置MMCV环境,解决报错,版本不匹配错误示例正确示例总结配置MMCV环境,解决报错,版本不匹配在col

Spring Security方法级安全控制@PreAuthorize注解的灵活运用小结

《SpringSecurity方法级安全控制@PreAuthorize注解的灵活运用小结》本文将带着大家讲解@PreAuthorize注解的核心原理、SpEL表达式机制,并通过的示例代码演示如... 目录1. 前言2. @PreAuthorize 注解简介3. @PreAuthorize 核心原理解析拦截与

一文详解JavaScript中的fetch方法

《一文详解JavaScript中的fetch方法》fetch函数是一个用于在JavaScript中执行HTTP请求的现代API,它提供了一种更简洁、更强大的方式来处理网络请求,:本文主要介绍Jav... 目录前言什么是 fetch 方法基本语法简单的 GET 请求示例代码解释发送 POST 请求示例代码解释