Search code examples
pythonsshcygwinparamikoopenssh

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


I have a setup, where I want to connect from a Windows7 VM to a Linux Server through passwordless SSH with private/public keys.

The SSH connection works fine through the Cygwin terminal.

With Paramiko, I can connect from my client to my host if I accept missing host keys, but if I try to use known_hosts instead, I get the error:

Server XXX not found in known_hosts.

Is Paramiko at all able to read the known_hosts file in Windows/Cygwin? Can I somehow provide the path?

My .ssh folder is located under C:\cygwin64\home\<user>\.ssh.

Thank you a lot for your help,
Thomas


Solution

  • I assume you are using SSHClient.load_system_host_keys.

    By default, the methods loads os.path.expanduser('~/.ssh/known_hosts') file. What does not match your C:\cygwin64\home\<user>\.ssh location, as on Windows os.path.expanduser uses USERPROFILE environment variable, which points to C:\Users\<user>.


    You can fix that by:

    • Changing your Cygwin configuration somehow (but it can break other things);
    • or creating directory junction from C:\Users\<user>\.ssh to C:\cygwin64\home\<user>\.ssh (probably the most portable solution);
    • or explicitly specifying filename argument in a call to SSHClient.load_system_host_keys (not portable);
    • or setting HOME environment variable to point to C:\cygwin64\home\<user> before running your Python script - os.path.expanduser prefers HOME to USERPROFILE - and Windows does not use HOME (can break other things).