簡體   English   中英

git - 服務器主機密鑰未緩存

[英]git - Server host key not cached

我嘗試將更改從本地存儲庫推送到遠程存儲庫。 當我輸入:

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

我該如何解決這個問題? 我在 Windows 7 的命令行中使用 git。

編輯

當我嘗試做一個簡單的 ssh 時

ssh user@hostname

我收到以下錯誤:

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

它不會以某種方式創建目錄,因為路徑無效。 如何解決這個問題?

@eckes: Edit2

我的家設置為%HOMEDRIVE%%HOMEPATH%是否正確?

對於那些通過標准命令提示符使用 PuTTY 在 Windows 上設置 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 服務的指紋(請編輯以添加更多):

嘗試從 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 就足夠了,但我很順利)

注意:如果 unset 對你不起作用,試試這個:

set GIT_SSH=

該消息意味着您的受信任主機文件中不存在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但這要求您遵守known_hosts文件的格式,如sshd的手冊頁(部分AUTHORIZED_KEYS FILE FORMAT )中所述。

我懷疑您的GIT_SSH環境變量設置為%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並成功連接到遠程主機,您就應該准備就緒。 繼續並再次嘗試推動。

至少在 Windows 上,僅僅通過 ssh 連接到主機是不夠的。 這將主機密鑰添加到ssh/known_hosts但錯誤仍然存​​在。

您需要關閉 git bash 窗口並打開一個新窗口。 然后清除注冊表緩存,然后推/拉工作。

Rene,您的HOME變量設置不正確。 將其更改為c:\\Users\\(your-username)或僅更改為%USERNAME%

使用 Plink 解決方案

將此 python 腳本保存到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

在 Win7x64 和Python 2.7上測試。

然后運行:

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

並選擇導入到注冊表中。 密鑰掃描將檢索域的公鑰(我在使用 bitbucket 時遇到了問題),然后 Python 腳本會將其轉換為 Plink 格式。

遇到了同樣的問題,忘記在實際存儲庫所在的端口上連接到 SSH ,而不僅僅是一般的 SSH 端口,那么主機密鑰就不同了!

只需打開 Putty 並嘗試與要推送代碼的遠程服務器建立連接。 當對話框出現時按是(你信任遠程)然后一切都會好起來的。

工作環境:

  • 視窗 10
  • 混帳
  • 膩子

第一:根據Regedit刪除registy中的putty known_hosts。
然后:在Window的cmd中執行%GIT_SSH% user@hostname命令即可解決問題。

希望對大家有幫助。

當我試圖在我的 Windows 7 機器上克隆一個存儲庫時,我也遇到了同樣的問題。 我嘗試了這里提到的大部分答案。 他們都沒有為我工作。

對我有用的是,運行Pageant (Putty 身份驗證代理)程序。 一旦 Pageant 在后台運行,我就可以從/向存儲庫進行克隆、推送和拉取操作。 這對我有用,可能是因為我已經設置了我的公鑰,這樣無論何時第一次使用它都需要密碼並且 Pageant 啟動。

從 PuTTY 更改為 OpenSSH 為我解決了這個問題,無需取消設置 GIT_SSH 等。

我使用此解決方法解決了類似的問題。

你只需要切換到嵌入式 Git,按下 Yes 按鈕,然后切換回 System Git。

您可以在

Tools -> Options -> Git

正如Roman Starkov所回答的, plink需要將主機添加到它的緩存中。

對於使用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 Extensions 相同的 plink/klink!

直接用Bash添加主機並沒有解決問題,在Git Extensions中使用'Fetch all'時仍然出現錯誤。 通過在一個分支上使用“Pull”,Git Extensions 會自動添加所需的主機,並帶有 Bash 彈出屏幕。 完成此操作后,我能夠再次使用“全部獲取”。 不確定 Git 擴展做了什么不同的事情。

我已經嘗試了上述所有方法,但沒有一個可以在我的筆記本電腦上解決相同的問題。 最后,我沒有在 git bash 中將分支推送到原點,而是使用 TortoiseGit 的推送選項進行推送,然后彈出一個窗口要求我將新的主機密鑰添加到緩存中,單擊是按鈕后,一切順利現在好了。

希望對大家有幫助。

我換了個硬盤,裝了Windows。 當嘗試上傳文件時收到此命令窗口。

我按“y”,然后按 Ctrl + C。打開putty.exe ,添加一個舊鍵然后返回到 git 並推送文件。

在 Windows 7 或 10 中,對我有用的技巧是刪除 GIT_SSH 系統變量。 之前設置為使用Plink,現在改為Putty。 這導致了 Plink.exe 錯誤

還有一個舊的 Git(32 位版本)安裝和更新到 Git(例如 Git-2.20.1-64-bit.exe),因為 PC 是 64 位操作系統。

無論如何,Git 甚至沒有使用 Putty/Plink,因為在 Git 安裝中它默認使用 Open SSH。

如果您在使用 ATLASSIAN SOURCETREE 執行 git push/pull 操作時收到有關無法識別的主機密鑰的消息,您將無法回答 y/n 並且推/拉操作將中止而不緩存密鑰。 但是,轉到 SourceTree 工具->選項(常規選項卡)並將 SSH 客戶端(在 SSH 客戶端配置下)從 PuTTY 更改為 OpenSSH 將允許緩存密鑰而不更改任何其他內容。

只需卸載 Git 擴展並通過選擇 OpenSSH 而不是重新安裝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM