Git-服务器主机密钥未缓存

我试图把变化从我的本地回购推到远程回购。当我输入:

git push origin

我得到以下错误:

The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
Connection abandoned.
fatal: The remote end hung up unexpectedly

我怎样才能解决这个问题呢? 我在 Windows7的命令行中使用 git。

Edit

当我尝试做一个简单的 ssh 时

ssh user@hostname

我得到以下错误:

Could not create directory '/c//%HOMEDRIVE%%HOMEPATH%/.ssh'.
percent_expand: unknown key %H

它不会创建目录,因为路径是无效的。如何解决这个问题?

@ eckes: Edit2

我的家被设置为 %HOMEDRIVE%%HOMEPATH%是正确的吗?

149869 次浏览

该消息意味着 origin的主机密钥不存在于您的受信任主机文件中。

为了解决这个问题,打开一个到 origin的普通 SSH 连接,SSH 会询问您是否想要信任远程主机(从 Git 控制台) :

$ ssh 127.0.0.1
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
RSA key fingerprint is <FINGERPRINT>.
Are you sure you want to continue connecting (yes/no)?

如果您信任远程主机(例如,类型 yes) ,SSH 将把它的密钥添加到已知主机的列表中。

之后,你应该能够做你的 git push origin

作为替代方案,您也可以手动将 origin的键添加到 .ssh/known_hosts,但是这要求您遵守 sshd的手册页(密钥文件格式部分)中描述的 known_hosts文件的格式。

仅仅发送 ssh 到主机是不够的,至少在 Windows 上是这样。这将主机密钥添加到 ssh/known_hosts,但错误仍然存在。

您需要关闭 git bash 窗口并打开一个新窗口。然后清除注册表缓存,推/拉就可以工作了。

尝试在 Git Bash 提示符下执行“ set | grep-i ssh”

如果你的设置和我的一样,你可能有这些设置:

GIT_SSH='C:\Program Files (x86)\PuTTY\plink.exe'
PLINK_PROTOCOL=ssh
SVN_SSH='"C:\\Program Files (x86)\\PuTTY\\plink.exe"'

我做了一个

unset GIT_SSH
unset PLINK_PROTOCOL
unset GIT_SVN

然后就成功了。.我猜腻子把它的钥匙保存在别的地方作为 $HOME/。Ssh 或者别的什么... ... (我还遇到一个问题,$HOME 被设置为“ C: Users usrnam”而不是“/C/Users/usrnam/”

无论如何,你的英里数可能会有所不同,但这对我来说是固定的。 : -)

(可能只是执行未设置的 GIT _ SSH 就足够了,但是我已经很顺利了)

注意: 如果取消设置对您不起作用,请尝试以下方法:

set GIT_SSH=

Rene,您的 HOME变量设置不正确。请将其改为 c:\Users\(your-username)或仅改为 %USERNAME%

Solution with Plink

这个巨蟒脚本保存到 known_hosts.py:

#! /usr/bin/env python


# $Id$
# Convert OpenSSH known_hosts and known_hosts2 files to "new format" PuTTY
# host keys.
#   usage:
#     kh2reg.py [ --win ] known_hosts1 2 3 4 ... > hosts.reg
#       Creates a Windows .REG file (double-click to install).
#     kh2reg.py --unix    known_hosts1 2 3 4 ... > sshhostkeys
#       Creates data suitable for storing in ~/.putty/sshhostkeys (Unix).
# Line endings are someone else's problem as is traditional.
# Developed for Python 1.5.2.


import fileinput
import base64
import struct
import string
import re
import sys
import getopt


def winmungestr(s):
"Duplicate of PuTTY's mungestr() in winstore.c:1.10 for Registry keys"
candot = 0
r = ""
for c in s:
if c in ' \*?%~' or ord(c)<ord(' ') or (c == '.' and not candot):
r = r + ("%%%02X" % ord(c))
else:
r = r + c
candot = 1
return r


def strtolong(s):
"Convert arbitrary-length big-endian binary data to a Python long"
bytes = struct.unpack(">%luB" % len(s), s)
return reduce ((lambda a, b: (long(a) << 8) + long(b)), bytes)


def longtohex(n):
"""Convert long int to lower-case hex.


Ick, Python (at least in 1.5.2) doesn't appear to have a way to
turn a long int into an unadorned hex string -- % gets upset if the
number is too big, and raw hex() uses uppercase (sometimes), and
adds unwanted "0x...L" around it."""


plain=string.lower(re.match(r"0x([0-9A-Fa-f]*)l?$", hex(n), re.I).group(1))
return "0x" + plain


output_type = 'windows'


try:
optlist, args = getopt.getopt(sys.argv[1:], '', [ 'win', 'unix' ])
if filter(lambda x: x[0] == '--unix', optlist):
output_type = 'unix'
except getopt.error, e:
sys.stderr.write(str(e) + "\n")
sys.exit(1)


if output_type == 'windows':
# Output REG file header.
sys.stdout.write("""REGEDIT4


[HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\SshHostKeys]
""")


# Now process all known_hosts input.
for line in fileinput.input(args):


try:
# Remove leading/trailing whitespace (should zap CR and LF)
line = string.strip (line)


# Skip blanks and comments
if line == '' or line[0] == '#':
raise "Skipping input line"


# Split line on spaces.
fields = string.split (line, ' ')


# Common fields
hostpat = fields[0]
magicnumbers = []   # placeholder
keytype = ""        # placeholder


# Grotty heuristic to distinguish known_hosts from known_hosts2:
# is second field entirely decimal digits?
if re.match (r"\d*$", fields[1]):


# Treat as SSH-1-type host key.
# Format: hostpat bits10 exp10 mod10 comment...
# (PuTTY doesn't store the number of bits.)
magicnumbers = map (long, fields[2:4])
keytype = "rsa"


else:


# Treat as SSH-2-type host key.
# Format: hostpat keytype keyblob64 comment...
sshkeytype, blob = fields[1], base64.decodestring (fields[2])


# 'blob' consists of a number of
#   uint32    N (big-endian)
#   uint8[N]  field_data
subfields = []
while blob:
sizefmt = ">L"
(size,) = struct.unpack (sizefmt, blob[0:4])
size = int(size)   # req'd for slicage
(data,) = struct.unpack (">%lus" % size, blob[4:size+4])
subfields.append(data)
blob = blob [struct.calcsize(sizefmt) + size : ]


# The first field is keytype again, and the rest we can treat as
# an opaque list of bignums (same numbers and order as stored
# by PuTTY). (currently embedded keytype is ignored entirely)
magicnumbers = map (strtolong, subfields[1:])


# Translate key type into something PuTTY can use.
if   sshkeytype == "ssh-rsa":   keytype = "rsa2"
elif sshkeytype == "ssh-dss":   keytype = "dss"
else:
raise "Unknown SSH key type", sshkeytype


# Now print out one line per host pattern, discarding wildcards.
for host in string.split (hostpat, ','):
if re.search (r"[*?!]", host):
sys.stderr.write("Skipping wildcard host pattern '%s'\n"
% host)
continue
elif re.match (r"\|", host):
sys.stderr.write("Skipping hashed hostname '%s'\n" % host)
continue
else:
m = re.match (r"\[([^]]*)\]:(\d*)$", host)
if m:
(host, port) = m.group(1,2)
port = int(port)
else:
port = 22
# Slightly bizarre output key format: 'type@port:hostname'
# XXX: does PuTTY do anything useful with literal IP[v4]s?
key = keytype + ("@%d:%s" % (port, host))
value = string.join (map (longtohex, magicnumbers), ',')
if output_type == 'unix':
# Unix format.
sys.stdout.write('%s %s\n' % (key, value))
else:
# Windows format.
# XXX: worry about double quotes?
sys.stdout.write("\"%s\"=\"%s\"\n"
% (winmungestr(key), value))


except "Unknown SSH key type", k:
sys.stderr.write("Unknown SSH key type '%s', skipping\n" % k)
except "Skipping input line":
pass

Tested on Win7x64 and Python 2.7.

然后跑:

ssh-keyscan -t rsa bitbucket.org >>~/.ssh/known_hosts
python --win known_hosts.py >known_hosts.reg
start known_hosts.reg

并选择导入到注册表中。Keyscanner 将检索域的公钥(我在 bitbucket 上遇到了问题) ,然后 python 脚本将它转换为 Plink 格式。

对于那些通过标准命令提示符在 Windows 上使用 PuTTY 设置 MSYS Git 的人来说,向 PuTTY 的缓存添加主机的方法是运行

> plink.exe <host>

例如:

> plink.exe codebasehq.com


The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 2e:db:b6:22:f7:bd:48:f6:da:72:bf:59:d7:75:d7:4e
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n)

只要回答 y,然后 Ctrl + C 其余的。

不过一定要检查指纹。这个警告是有原因的。一些 git 服务的指纹(请编辑以添加更多) :

I suspect that your GIT_SSH environment variable is set to %ProgramFiles(x86)%\putty\plink.exe. 由于某种原因,PLink 不使用用户目录中的 .ssh/known_hosts文件来存储远程主机密钥。

如果这实际上是你的情况,它可能是故意的,如果你想使用选美比赛,你需要使用 PLink 连接到主机第一。

"$GIT_SSH" user@hostname

你应该会收到类似的信息

The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 86:7b:1b:12:85:35:8a:b7:98:b6:d2:97:5e:96:58:1d
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n)

一旦您已经回答了 y的问题并成功连接到远程主机,您应该已经设置好了。继续,再试一次。

Had the same issue, and forget to 在端口上连接到 SSH,其中实际上是存储库, not just general SSH port, then the host key is different!

当我试图在我的 Windows7机器上克隆一个存储库时,我也遇到了同样的问题。这里提到的大部分答案我都试过了。他们都不为我工作。

对我有用的是,运行 选美比赛(Putty 身份验证代理)程序。一旦 Pageant 在后台运行,我就可以从/到存储库进行克隆、推送和拉取操作。这对我来说很有效,可能是因为我设置了我的公开密钥,这样每当它第一次被使用时,就需要一个密码并且选美比赛开始了。

Just open Putty and try to establish connection to remote server you want to push your code. when the dialog appears press Yes(you trust remote) then everything would be OK.

将 PuTTY 改为 OpenSSH 为我解决了这个问题,无需取消 GIT _ SSH 等设置。

我用这个 workaround解决了类似的问题。

您只需切换到嵌入式 Git,按下,按 Yes 按钮,然后切换回 System Git。

您可以在

Tools -> Options -> Git

直接使用 Bash 添加主机并没有解决这个问题,在 Git 扩展中使用‘ Fetchall’时仍然会出现错误。通过在一个分支上使用‘ Pull’,所需的主机就会通过带有 Bash 弹出屏幕的 Git Extended 自动添加。在这样做之后,我可以再次使用‘ FetchAll’。不确定 Git 扩展执行了哪些不同的操作。

I have tried all the methods above but none of them could fix the same issue on my laptop. Finally instead of pushing the branch to origin in git bash, I trun to use TortoiseGit's push option to do the pushing, then a window pops-up to ask me to add the new host key to cache, after clicking the yes button, everything goes fine now.

希望对你们有所帮助。

工作环境:

  • 视窗10
  • 饭桶
  • 油灰

首先: 根据 Regedit 在注册表中删除 putty known _ hosts。
然后: 在 Window 的 cmd 中执行命令 %GIT_SSH% user@hostname解决了这个问题。

希望对你们有所帮助。

我更改了一个硬盘,安装了 Windows。当尝试上传文件时收到这个命令窗口。

我按下“ y”,然后 Ctrl + C 打开 putty.exe,添加一个旧的键,然后返回到 git 并推送文件。

只需卸载 Git 扩展并选择 OpenSSH 而不是

在 Windows7或10中,我使用的技巧是删除 GIT _ SSH 系统变量。它以前被设置为使用叮当,现在被腻子取代。这会导致 Plink.exe 错误

还有一个旧的 Git 安装(32位版本)和更新到 Git (例如 Git-2.20.1-64-bit.exe) ,因为 PC 是64位操作系统。

无论如何,Putty/Plink 甚至没有被 Git 使用,因为在 Git 安装中默认使用 Open SSH。

As answered by 罗曼 · 斯塔科夫, plink needs to add the host to it's cache.

对于使用 Git 扩展的人:

  1. 打开 Git 扩展
  2. 转到工具-> 设置-> SSH
  3. 将路径复制到“ plink.exe”(如果使用 PuTTY)/“ klink.exe”(如果使用 KiTTY)
  4. 在控制台中,运行以下命令:

(用实际路径替换)

<the path to plink/klink.exe> <address to the server>

例如:。

%ProgramData%\chocolatey\lib\kitty\tools\klink.exe codebasehq.com

注意 : 确保使用与 Git Extended 相同的 plink/klink!

如果您在使用 ATLASSian SOURCETREE 执行 git push/pull 操作时收到关于未识别主机密钥的消息,那么您就无法回答 y/n 问题,并且推/拉操作将在不缓存密钥的情况下中止。然而,使用 SourceTree Tools-> Options (General Tab)并将 SSH Client (在 SSH Client Configuration 下)从 PuTTY 更改为 OpenSSH 将允许在不更改任何其他内容的情况下缓存密钥。