简体   繁体   中英

SSH connect to EC2 instance

I am trying to SSH to an EC instance.

These are the steps I followed and below is the error I got.

  1. In the Amazon Console, I created a Key Pair and downloaded it

  2. Changed the permissions of that pem file to 400(as written here )

  3. Went to the running instances in the console and got my Public DNS

  4. Added an inbound rule(SSH),0.0.0.0/0 to the group with description 'SecurityGroup for ElasticBeanstalk environment.' in the Security Groups tab in console

  5. Executed this in console ssh -i <>my_key_filename>.pem ec2-user@<Public DNS>

And this is the output for that:

OpenSSH_6.1p1 Debian-4, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to ec2-54-254-148-94.ap-southeast-1.compute.amazonaws.com [54.254.148.94] port 22.
debug1: Connection established.
debug1: identity file mykey.pem type -1
debug1: identity file mykey.pem-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH_5*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.1p1 Debian-4
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA a1:2b:92:f6:cf:e3:ed:8a:60:0e:34:c0:27:24:6f:f7
The authenticity of host 'ec2-54-254-148-94.ap-southeast-1.compute.amazonaws.com (54.254.148.94)' can't be established.
RSA key fingerprint is a1:2b:92:f6:cf:e3:ed:8a:60:0e:34:c0:27:24:6f:f7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'ec2-54-254-148-94.ap-southeast-1.compute.amazonaws.com,54.254.148.94' (RSA) to the list of known hosts.
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: aws_key.pem
debug1: Authentications that can continue: publickey
debug1: Offering DSA public key: id_dsa
debug1: Authentications that can continue: publickey
debug1: Offering RSA public key: mailid@gmail.com
debug1: Authentications that can continue: publickey
debug1: Trying private key: mykey.pem
debug1: read PEM private key done: type RSA
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
Permission denied (publickey).

Also, the instance I am trying to connect to is an Amazon Linux instance.

From your comments below your question : it looks like the order of your steps where incorrect.

  • First you need to create the keypair
  • then you need to tell ElasticBeanstak to use that key pair when it will start your instances

Public keys are injected into the instances at first boot. It is not possible for AWS to change the keypair after the instance has been started - AWS has no technical way to connect to your instance. (you can do it manually by upload files in ~/.ssh directory)

To learn more about how to use keypair with ElasticBeanstalk, have a look at this screenshot 在此处输入图片说明

Or just create an .ebextensions directory with an application.config file containing

- namespace: aws:autoscaling:launchconfiguration   
  option_name: EC2Keyname   
  value: "keyname"   

More details about customising ElasticBeanstalk environment : http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#customize-containers-format-options

Possible values are listed here http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-launchconfig.html

More details about keypair can be found here : http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html

--Seb

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