By rcv

2012-05-20 01:59:50 8 Comments

I'm trying to get started with the Paramiko library, but the library is throwing an exception as soon as I try to connect with the following simple program:

import paramiko
ssh = paramiko.SSHClient()
ssh.connect('', username='boatzart', password='mypassword')

The error I get is:

Traceback (most recent call last):
File "", line 6, in <module>
File "build/bdist.macosx-10.7-intel/egg/paramiko/", line 316, in connect
File "build/bdist.macosx-10.7-intel/egg/paramiko/", line 85, in missing_host_key
paramiko.SSHException: Unknown server

This occurs no matter which server I try.


@Martin Prikryl 2017-03-29 12:44:23

The correct way is either:

@karan.dodia 2020-03-11 19:35:50

This should be higher up! Especially the last note about caching the hostkey from the first attempt: call client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts‌​')) and client.set_missing_host_key_policy(paramiko.AutoAddPolicy())‌​, then connect to the server to save the key. Subsequent connections can then load the host key with client.load_system_host_keys().

@Burhan Khalid 2012-05-20 06:33:14

The exception was raised because you are missing a host key, the rather cryptic "Unknown server" is the clue - since the exception was raised from missing_host_key

Try this instead:

import paramiko

paramiko.util.log_to_file('ssh.log') # sets up logging

client = paramiko.SSHClient()
client.connect('', username=username, password=password)
stdin, stdout, stderr = client.exec_command('ls -l')

@zeugmatis 2013-06-28 15:50:11

I encountered this issue and wanted to post a workaround here. The issue was indeed the ssh server sending ecdsa keys, which are not supported (yet) with paramiko. On my debian Wheezy system I disabled ecdsa by commenting out a single line in /etc/ssh/sshd_config:

# HostKey /etc/ssh/ssh_host_ecdsa_key

Restarted sshd, and it was back to using RSA. There were some ecdsa keys in my known_hosts file so I just deleted it to reset, and logged in manually to recreate the keys. From there, paramiko worked perfectly as expected, with RSA host key checking.

@guettli 2012-09-07 12:16:01

I had this error: I can connect from the shell, but paramiko says "Unknown server workdevel114".

There were two similar entries in known_hosts:

[email protected]> grep workdevel114 ~/.ssh/known_hosts
workdevel114 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC8qGbuI1BaBodi7sKWLfV8Eh+De80Th7HFLD4WiJWo57THl0Q+QcopUaU3pF....
[email protected]> grep I1BaBodi7sKWLfV8Eh+De80Th7HFLD4WiJWo57THl0Q+QcopUaU3pF ~/.ssh/known_hosts
workdevel114 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC8qGbuI1BaBodi7sK...
|1|f/auQ9nY5dFbVtOdY3ocjtVO9dM=|esvazUDTT3VIcLk9DxmPI6FZt1s= ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC8qGbuI1BaBodi7sKWLfV8Eh+De80Th7HFLD4...

The seconds entry (|1|....) seems to confuse paramiko. I guess it is related to this ticket:

I solved it by adding this line:


But this disables host-checking of the ssh protocol in this case: Paramiko thinks the host key is unknown, but it is known. The known Key gets ignored. I don't care because man-in-the-middle attacks are very unlikely in my environment.


@ton 2014-10-10 04:31:50

I know this is an old thread but adding "set_missing_host_key_policy" solved my problem, as using "load_system_host_keys" wasn't enough

@user1224821 2012-07-25 07:01:56

I experienced the same issue and here's the solution that worked out for me:

import paramiko

client = paramiko.SSHClient()
client.connect('', username=username, password=password)
stdin, stdout, stderr = client.exec_command('ls -l')

This is to set the policy to use when connecting to a server that doesn't have a host key in either the system or local HostKeys objects. The default policy is to reject all unknown servers (using RejectPolicy). You may substitute AutoAddPolicy or write your own policy class.

More details at paramiko api doc. Hope this helps.

@F1Rumors 2016-06-15 18:18:40

It works, and I do that too, but worth mentioning you are auto-trusting the target machine and technically exposed to man in the middle attacks. Just to mention the caveat!

@nidHi 2016-09-16 04:46:38

Does AutoAddPolicy() add the host_key to the known_hosts. So the next time this is run can I remove the set_missing_host_key_policy() and just use load_system_host_keys()?

@Martin Prikryl 2017-03-29 12:45:03

@nidHi Only if you tell it, where to load the keys from in advance, by calling the SSHClient.load_host_keys. See my answer

Related Questions

Sponsored Content

2 Answered Questions

Using ssh_config file with Paramiko

1 Answered Questions

[SOLVED] authentication exception in paramiko

  • 2017-06-12 12:39:54
  • mydigi
  • 2275 View
  • 1 Score
  • 1 Answer
  • Tags:   python paramiko

1 Answered Questions

2 Answered Questions

[SOLVED] How to access a remote host with Paramiko?

1 Answered Questions

15 Answered Questions

2 Answered Questions

Can I ssh to a server and then do sshfs on the server with paramiko?

1 Answered Questions

[SOLVED] python issues with MySQL in eclipse

Sponsored Content