【问题标题】:connecting to Git via ssh on Windows fails在 Windows 上通过 ssh 连接到 Git 失败
【发布时间】:2021-10-12 05:57:13
【问题描述】:

虽然我的 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 gituser@196.168.0.50
ssh: connect to host 196.168.0.50 port 22: Connection timed out

我更新了我的 Git- 和 Tortoise 安装,现在我得到了

ssh gituser@192.168.0.50
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 gituser@192.168.0.50
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 gituser@192.168.0.50
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 版本加上新版本的支持库引起的——或者相反。由于您使用的是 Tortoise,这也可能是原因的一部分。你能对主人做一个简单的ssh吗?
  • 感谢您的提示,弗雷德里克!为了提高可读性,我已将我之前的答案合并到问题中。

标签: windows git ssh


【解决方案1】:

虽然这个问题在我安装新路由器后出现了一些,但根本原因是我的 Synology NAS 上的 Git 服务器设置有缺陷。按照这些步骤从头开始设置 Git 服务器后,一切正常:

设置用户和文件夹

  • 通过 Diskstation 包管理器安装 WebDAV 包
  • 配置 WebDAV:在端口 5005 上启用 HTTP,在端口 5006 上启用 HTTPS
  • 控制面板 -> 文件服务 -> SMB -> 高级设置 -> 将最小和最大 SMB 协议设置为 SMB1 和 SMB3
  • 通过 Diskstation 界面创建用户 gituser(组用户,无权访问共享文件夹,具有 File Station 和 WebDAV 权限)
  • 添加名为git(位于/volume1/git)的新共享文件夹,具有gituser 的读/写访问权限。此文件夹将包含所有存储库。
  • 通过 Diskstation 安装 Git Server 包
  • 打开 Git 服务器并允许 gituser 权限(必须以“管理员”身份登录才能查看可用用户)
  • 在 Diskstation 上启用 SSH 访问(控制面板 > 终端和 SNMP > 启用 SSH 服务)
  • 激活用户主服务(控制面板 > 用户 > 扩展 > 用户群 > 激活用户主服务)

配置 SSH 访问

  • 在服务器上为 gituser 创建~/.ssh 文件夹
sudo su
mkdir /volume1/homes/gituser/.ssh
  • 通过 PuTTYgen 创建私钥/公钥对(SSH-2 RSA,2048 位)
  • 将窗口顶部显示的公钥复制到名为 authorized_keys 的文件中
  • 将该文件从本地计算机复制到服务器上的 gituser 帐户
scp authorized_keys username@nas:/volume1/homes/gituser/.ssh
  • 以 root 身份登录时更改权限
cd /volume1/homes/gituser/
sudo chown -R gituser:users .ssh
sudo chmod 700 .ssh
sudo chmod 644 .ssh/authorized_keys
sudo chmod 755 /volume1/homes/gituser
  • 通过在 /etc/ssh/sshd_config 中设置以下属性来激活公钥 SSH 访问
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      %h/.ssh/authorized_keys
  • 禁用然后重新启用 SSH 服务(控制面板 > 终端和 SNMP > 启用 SSH 服务)

【讨论】:

    【解决方案2】:

    我会在不设置 GIT_SSH 的情况下尝试相同的 ssh 调用

    set GIT_SSH=
    

    对于最近的 Windows 10,openSSH 要么直接安装,要么随 Git For Windows 一起提供。
    确保使用最新版本的 Git,并将其 usr/bin 文件夹添加到您的 PATH。

    【讨论】:

    • 感谢您的提示!这导致我进行了进一步的调查,我在原始帖子中记录了这些调查。似乎我已经安​​装了 SSH 版本,但无论我使用哪一个:我总是可以通过 SSH 连接到 Git 服务器,但是 git pull 总是失败:(
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      • 2021-11-02
      • 2023-02-10
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 2021-05-02
      相关资源
      最近更新 更多