windows - 在 Windows 上通过 ssh 连接到 Git 失败

标签 windows git ssh

虽然我的 Linux 计算机可以使用其主机名访问 NAS 上的托管存储库,但我的 Windows 计算机无法从命令行/Visual Studio Code 访问它。在调试尝试期间,我找到了一种方法,以便至少 TortoiseGit 可以访问存储库:我必须添加一个环境变量 GIT_SSH_COMMAND=ssh -vvv (除了已经存在的 GIT_SSH=C:\Program Files\TortoiseGit\bin\TortoiseGitPlink.exe)。

当我通过 cmd 对通过 url = ssh://gituser@my-devices-hostname/volume1/git/reponame.git 在 Git 配置中访问的存储库发出 git pull 我得到以下输出:

OpenSSH_8.1p1, OpenSSL 1.1.1d  10 Sep 2019
debug1: Reading configuration data /etc/ssh/ssh_config
debug2: resolving "my-devices-hostname" port 22
debug2: ssh_connect_direct
debug1: Connecting to my-devices-hostname [192.168.0.50] port 22.
getaddrinfo: atch: Name or service not known
ssh: connect to host my-devices-hostname port 22: failure
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

当我用静态 IP 替换主机名时,出现相同的错误:

OpenSSH_8.1p1, OpenSSL 1.1.1d  10 Sep 2019
debug1: Reading configuration data /etc/ssh/ssh_config
debug2: resolve_canonicalize: hostname 192.168.0.50 is address
debug2: ssh_connect_direct
debug1: Connecting to 192.168.0.50 [192.168.0.50] port 22.
getaddrinfo: atch: Name or service not known
ssh: connect to host 192.168.0.50 port 22: failure
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

当用 TortoiseGit 做同样的事情时,一切正常,我得到:

OpenSSH_8.1p1, OpenSSL 1.1.1d  10 Sep 2019
debug1: Reading configuration data /etc/ssh/ssh_config
debug2: resolving "my-devices-hostname" port 22
debug2: ssh_connect_direct
debug1: Connecting to my-devices-hostname [192.168.0.50] port 22.
debug1: Connection established.
...

我最近设置了一个新的 DSL 路由器 (Fritz!Box 7590),也许这有某种关系。由于我猜测问题可能与 IPv6 有关,因此我在 Synology NAS 上禁用了 IPv6,但没有任何改变。

更新: Fedrik 让我执行手动 ssh。结果如下:

ssh gituser@my-devices-hostname
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to my-devices-hostname closed.

令人惊讶的是:

ssh <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aacdc3dedfd9cfd8ea9b939c849b9c92849a849f9a" rel="noreferrer noopener nofollow">[email protected]</a>
ssh: connect to host 196.168.0.50 port 22: Connection timed out

我更新了我的 Git 和 Tortoise 安装,现在我明白了

ssh <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cebe5f8f9ffe9feccbdb5bea2bdbab4a2bca2b9bc" rel="noreferrer noopener nofollow">[email protected]</a>
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to 192.168.0.50 closed.

基于主机名的 ssh 保持不变。

受到 VonC 答案的启发,我检查了我的系统上是否安装了多个 ssh 实例。我发现了以下内容:

C:\Windows\System32\OpenSSH\ssh.exe
C:\Program Files\Git\usr\bin\ssh.exe

所以我检查了(在更新 Windows 版的 Tortoise 和 Git 后),使用的是哪一个。首先,我留下了 GIT_SSH_COMMAND=ssh -vvv:

git pull
OpenSSH_8.4p1, OpenSSL 1.1.1h  22 Sep 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts' -> '/c/Users/user/.ssh/known_hosts'
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts2' -> '/c/Users/user/.ssh/known_hosts2'
debug2: resolving "my-devices-hostname" port 22
debug2: ssh_connect_direct
debug1: Connecting to my-devices-hostname [192.168.0.50] port 22.
getaddrinfo: atch: Name or service not known
ssh: connect to host my-devices-hostname port 22: failure
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

"C:\Program Files\Git\usr\bin\ssh.exe" -vvv <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d7b0bea3a2a4b2a597e6eee5f9e6e1eff9e7f9e2e7" rel="noreferrer noopener nofollow">[email protected]</a>
OpenSSH_8.4p1, OpenSSL 1.1.1h  22 Sep 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug2: resolve_canonicalize: hostname 192.168.0.50 is address
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts' -> '/c/Users/bjoer/.ssh/known_hosts'
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts2' -> '/c/Users/bjoer/.ssh/known_hosts2'
debug2: ssh_connect_direct
debug1: Connecting to 192.168.0.50 [192.168.0.50] port 22.
debug1: Connection established.

更改环境变量后:GIT_SSH_COMMAND=C:/Windows/System32/OpenSSH/ssh.exe -vvv 我得到了这个:

git pull
OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5
debug3: Failed to open file:C:/Users/user/.ssh/config error:2
debug3: Failed to open file:C:/ProgramData/ssh/ssh_config error:2
debug2: resolving "my-devices-hostname" port 22
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to my-devices-hostname [192.168.0.50] port 22.
getaddrinfo: atch: Der angegebene Host ist unbekannt.
ssh: connect to host my-devices-hostname port 22: failure
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

C:\Windows\System32\OpenSSH\ssh.exe -vvv <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b8dfd1cccdcbddcaf889818a96898e809688968d88" rel="noreferrer noopener nofollow">[email protected]</a>
OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5
debug3: Failed to open file:C:/Users/user/.ssh/config error:2
debug3: Failed to open file:C:/ProgramData/ssh/ssh_config error:2
debug2: resolve_canonicalize: hostname 192.168.0.50 is address
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to 192.168.0.50 [192.168.0.50] port 22.
debug1: Connection established.

当我取消设置 GIT_SSHGIT_SSH_COMMAND 环境变量时,我得到:

git pull
getaddrinfo: atch: Name or service not known
ssh: connect to host my-devices-hostname port 22: failure
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

因此,我可以通过 SSH 连接到 Git 服务器,但是当 Git 可执行文件本身出现问题时,它会失败。 当我在同一台计算机上启动 WSL 时,我可以毫无问题地执行 git pull

关于如何解决这个问题有什么建议吗?

最佳答案

由于设置 git config --global ssh.variant plink,我收到此错误。通过设置git config --global ssh.variant ssh ,这个错误就这样消失了。问题似乎是 git 传递了 -batch选项,plink 将其解释为 -b选项(因此涉及“atch”的奇怪错误。

关于windows - 在 Windows 上通过 ssh 连接到 Git 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65464980/

相关文章:

Windows Server 2012 R2 长文件路径不起作用?

git - 新的 git diff 压实启发式方法不起作用

php - 使用 ssh 和 shell 从另一个 php 脚本在 Linux 服务器中运行 php 脚本

python - 绑定(bind)按键

java - gradle 命令失败,因为它无法启动守护进程

python - WMIC + Python 子进程/(Windows 10)

c++ - 试图了解进程特权属性

git - 安装后源树崩溃

Git:使用 Git 管道命令在裸仓库中编辑文件

batch-file - .bat 中通过 Plink 的脚本表现不同