简体   繁体   中英

Fabric using SSH key to connect the ec2 instance

I'm learning the fabric to automatically connect the ec2 instance which is already created. I set a ssh_config in the ssh folder

Home myhostname
        Hostname 52.62.207.113
        User ubuntu
        UserKnownHostsFile /dev/null
        StrictHostKeyChecking no
        PasswordAuthentication no
        IdentityFile ~/.ssh/mykey-pem

And I wrote a python file to test

from fabric import Connection
c = Connection('52.62.207.113')
result = c.run('uname -s')

The terminal response

paramiko.ssh_exception.SSHException: No authentication methods available. 

I'm not sure what happens. I try to manually

ssh -i mykey.pem ubuntu@52.62.207.113

It is successfully connecting the EC2 instance

Home myhostname
        Hostname 52.62.207.113
...
c = Connection('52.62.207.113')

I'm not a fabric user, but I guess you're expecting fabric to make use of the entry from your ssh_config file here? I can see two likely problems:

  1. You have Home myhostname . The correct keyword here is Host , not Home :

     Host myhostname Hostname 52.62.207.113 
  2. If you want fabric to use the Host section for myhostname , you probably have to tell it to connect to myhostname :

     c = Connection('myhostname') 

    You're telling it to connect to an IP address, and it probably wouldn't relate that to the host section

The actual error that you're getting, "No authentication methods available", is probably because fabric didn't apply the Host section from ssh_config , and it doesn't know of any key files that it should use for the session.

I think you missed PreferredAuthentications options.
And you typed your key file name incorrectly.

Change the config file as shown below and try connecting again.

Home myhostname
    Hostname 52.62.207.113
    User ubuntu
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/mykey.pem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM