0

I can't connect to my AWS EC2 Ubuntu server using SSH.

Every time I want to connect using SSH, it gives:

$ ssh -i key.pem ubuntu@[IP Address]
Connection closed by [IP Address] port 22

With -vvv option:

$ ssh -i key.pem -vvv ubuntu@[IP Address]
OpenSSH_8.1p1, OpenSSL 1.1.1d  10 Sep 2019
debug1: Reading configuration data /etc/ssh/ssh_config
debug2: resolve_canonicalize: hostname [IP Address] is address
debug2: ssh_connect_direct
debug1: Connecting to [IP Address] [[IP Address]] port 22.
debug1: Connection established.
debug1: identity file key.pem type -1
debug1: identity file key.pem-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.8
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.8 pat OpenSSH_7.0*,OpenSSH_7.1*,OpenSSH_7.2*,OpenSSH_7.3*,OpenSSH_7.4*,OpenSSH_7.5*,OpenSSH_7.6*,OpenSSH_7.7* compat 0x04000002
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to [IP Address]:22 as 'ubuntu'
debug3: send packet: type 20
debug1: SSH2_MSG_KEXINIT sent
Connection closed by [IP Address] port 22

I try using AWS CLI with the ec2-instance-connect command:

$ aws ec2-instance-connect send-ssh-public-key --instance-id [instance id] --instance-os-user ubuntu --availability-zone ap-southeast-1b --ssh-public-key file://key.pub

and it returns with successful

{
    "RequestId": "[Request ID]",
    "Success": true
}

But the problem is, when I try to log in again with new SSH key, it still gives the same error.

Anyone have the same problem before? How do you solve this issue?

kenlukas
  • 3,101
  • 2
  • 16
  • 26
  • My suggestion would be to check logs of sshd, is your instance pushing logs to cloudwatch? If not you might be able to use AWS System Manager Run Command. – Alex Moore Apr 07 '20 at 06:53
  • @AlexMoore My instance not pushing logs to cloudwatch and i dont think they have AWS system manager is installed by default in my ubuntu instance – Ikmal Zul Apr 07 '20 at 07:11
  • What about using instance-connect given that seems to be working? – Alex Moore Apr 07 '20 at 07:14
  • As you can see above, the aws cli return success, so i assume that i can connect after executing the script? No? – Ikmal Zul Apr 07 '20 at 07:23
  • Upon further checking, i need to install ec2-instance-connect first i guess. https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-connect-set-up.html#ec2-instance-connect-install – Ikmal Zul Apr 07 '20 at 07:23

0 Answers0