More generally, if you get this error, you should try SSHing with the exact parameters that paramiko is trying to use:
- hostname
- user
- authentication method
I found that having too many SSH keys caused some (but not all) of my fabric SSH connections to fail, because all the keys were being offered to the remote host. In the past, malformed keys have also raised this error message for me (you can detect them by removing the keys from ~/.ssh/
, one at a time.)
Unfortunately, Fabric doesn't respect your .ssh/config settings. If you want to debug this, you can run the following:
#!/usr/bin/env python
import paramiko
paramiko.util.log_to_file("/tmp/paramiko.log")
ssh = paramiko.SSHClient()
# Run this if you get host key errors: see later
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("example.com", username="myuser", password="mypassword")
and check the output in /tmp/paramiko.log
- you might see something like:
INF [20120904-16:58:52.155] thr=1 paramiko.transport: Disconnect (code 2): Too many authentication failures for myuser
You can set no_keys on the Fabric env environment:
env.no_keys = True
But then you will need to tell Fabric to use specific keys for specific hosts. As suggested above, you can do that in your fabfile with:
from fabric.api import env
env.key_filename = "/path/to/.ssh/ssk_non_public_key"
More generally here's a function to parse your .ssh config and pull out selective keys - in this keys, the SSH key to use. For this to work automatically, you'll need to add IdentityFile to ~/.ssh/config
:
Host example.com
IdentityFile /home/jp/.ssh/id_rsa_example
Another cause of failure might be that paramiko does not recognize all host key types. This is somewhat more problematic: paramiko is quietly ignoring the host key in ~/.ssh/known_hosts
, because it's not a format of host key that it understands. Try ssh-ing with -v and see what line SSH says it finds a host key match for:
debug1: Host '1.2.3.4' is known and matches the RSA host key.
debug1: Found key in /home/jp/.ssh/known_hosts:105
You can try deleting this line, then doing ssh again and accepting the (new?) host key, and see if paramiko is happy then. If that's the problem, though, and that doesn't solve it, then there's no clear solution that I can see.
ssh -o"PreferredAuthentications=password"
and see if it denies you access. – Schulz