使用 Cygwin OpenSSH 的 Windows 上的 Paramiko 找不到 known_hosts 文件

Paramiko on Windows with Cygwin OpenSSH doesn't find known_hosts files

我有一个设置,我想通过 private/public 密钥通过无密码 SSH 从 Windows7 VM 连接到 Linux 服务器。

SSH 连接通过 Cygwin 终端正常工作。

使用 Paramiko,如果我接受丢失的主机密钥,我可以从我的客户端连接到我的主机,但是如果我尝试使用 known_hosts,我会收到错误消息:

Server XXX not found in known_hosts.

Paramiko 是否能够读取 Windows/Cygwin 中的 known_hosts 文件?我能以某种方式提供路径吗?

我的 .ssh 文件夹位于 C:\cygwin64\home\<user>\.ssh

非常感谢您的帮助,
托马斯

我假设您正在使用 SSHClient.load_system_host_keys

默认情况下,方法会加载 os.path.expanduser('~/.ssh/known_hosts') 文件。与您的 C:\cygwin64\home\<user>\.ssh 位置不匹配的地方,例如 Windows os.path.expanduser 使用 USERPROFILE 环境变量,指向 C:\Users\<user>.


您可以通过以下方式解决该问题:

  • 以某种方式更改您的 Cygwin 配置(但它可能会破坏其他东西);
  • 或创建从 C:\Users\<user>\.sshC:\cygwin64\home\<user>\.ssh 的目录连接(可能是最便携的解决方案);
  • 或在对 SSHClient.load_system_host_keys 的调用中明确指定 filename 参数(不可移植);
  • 或设置 HOME 环境变量以在 运行 之前指向 C:\cygwin64\home\<user> 您的 Python 脚本 - os.path.expanduser 更喜欢 HOME 而不是 USERPROFILE - 并且 Windows 不使用 HOME(可以破坏其他东西)。