SSH: Connection timed out- Unable to SSH into EC2 instance - amazon-web-services

I had created an Amazon EC2 instance and was able to SSH into it previously. Now if I try to SSH into it, I get the error as below:
ssh: connect to host [ip address] port 22: Connection timed out
I've not changed anything in the security group and NACL to which the instance and the subnet are attached respectively.
Creating another instance from the AMI of this instance doesn't seem to work either.
I tried fixing the problem by detaching the EBS volume from this instance and mounting it to a fresh instance for checking /etc/ssh/sshd_config, but there wasn't any problem there.
I've also tried commenting out stuff from the fstab as per the solution https://stackoverflow.com/a/14050894
I'm facing the same issue in around 3 instances, kindly help.
Below is the output of ssh -v
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n 7 Dec 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to ip [ip] port 22.
debug1: connect to address ip port 22: Connection timed out
ssh: connect to host ip port 22: Connection timed out

After much struggle, I've resolved this issue by mounting the volume of the instance I was unable to SSH into with another EC2 instance created using the same key, and thereafter replacing the .ssh/authorized_keys file in the attached volume with .ssh/authorized_keys in the newly created instance. Finally, reattaching this volume to the original instance.
There wasn't any difference between the two authorized_keys files, so I can't say why did replacing the file work.
Creating a new instance with the AMI of the problematic instance using a new key should've worked which didn't for some reason unidentified as of yet.

Related

No response for ssh when Amazon EC2 running with `2/2 checks passed`

I have ec2 running.
It runs continuously for 6 months and has no problem.
However today I run some Mysql sequence.
It suddenly has no response for ssh and http
I have checked status in amazon console 2/2 checks passed
but when I ry to connect ssh it stacks.
ssh -v ubuntu#54.71.***.*** -i ~/.ssh/id_rsa.myserver
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /Users/whitebear/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug1: Connecting to 54.71.***.*** [54.71.***.***] port 22.
I think it looks like somehow server is freeze.
However in amazon aws console 2/2 checks passed it can be happen??
Any suggestion appreciated . thank you very much
When an SSH connection times-out, it is normally an indication that network traffic is not getting to the Amazon EC2 instance.
Things to check:
The instance is running Linux
The instance is launched in a public subnet, which is defined as having a Route Table entry to points to an Internet Gateway
The instance has a public IP address, which you are using for the connection
The Network Access Control Lists (NACLs) are set to their default "Allow All" values
A Security Group associated with the instance that permits inbound access on port 22 (SSH) either from your IP address, or from the Internet (0.0.0.0/0)
Your corporate network permits an outbound SSH connection (try alternate networks, eg home vs work vs tethered to your phone)
See also: Troubleshooting connecting to your instance - Amazon Elastic Compute Cloud

Lost access to EC2 instance

I reformatted my macbook and completely forgot to copy my ~/.ssh directory.
I tried ssh'ing into my EC2 instance
$ ssh ec2-user#xx.xxx.xxx.xx -i xxx.pem -v
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug1: Connecting to xx.xxx.xxx.xx [xx.xxx.xxx.xx] port 22.
debug1: connect to address xx.xxx.xxx.xx port 22: Operation timed out
ssh: connect to host xx.xxx.xxx.xx port 22: Operation timed out
But I presume it's not working because my PEM file is linked to the id_rsa file on my old laptop?
I had the PEM file in my google drive.
I've tried
sudo chmod 400 xxx.pem
Update my security group to allow SSH access
to my new laptop
Any ideas how I can regain access to my EC2 insance?
I would generate a new key pair as explain here, but it requires me to have access to the instance
Update: check #jordanm comment. You have no connection to the instance, maybe there is no need to create a new one.
As far as I know there is only way to create a new one. You can keep data if you make an image of your existing instance. You may have changed external IP if your instance has no elastic IP attached.
Login to AWS Console
Create a new key pair.
Select your EC2 instance in the list, in the top-left corner select Actions -> Image and templates -> Create image. Fill the form to create an image.
Launch a new instance using the image you've created and your new key.
(optional) If everything is in place consider terminating the original instance using console.

Permission denied (publickey) error while copying to EC2

I'm trying to copy file from my local to Aws EC1 or EC2.
my detailed setup.
Created two EC2 instances.
Got .pem file (Private key) from AWS consle.
Generated private key from .pem file (step2).
Able to connect EC1 and EC2 using private key generated in step 3.
Copied .pem file in EC1 and EC2 servers, by doing that am able to copy files in between EC1 and 2 aws servers.
Now I want to copy the file from my local machine to AWS EC1 and EC2 serves.
But when am trying to do below command (using public DNS):
ssh ubuntu#ec2-18-222-170-204.us-east-2.compute.amazonaws.com
getting below error
Permission denied (publickey)
And when I do it using Private DNS:
ssh ubuntu#ip-172-31-43-162.us-east-2.compute.internal
getting below error:
ssh: Could not resolve hostname ip-172-31-43-162.us-east-2.compute.internal: Name or service not known
what should I do to fix this or make it work?
Output
OpenSSH_7.2p2 Ubuntu-4ubuntu2.8, OpenSSL 1.0.2g 1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "ip-172-31-43-162.us-east-2.compute.internal" port 22
ssh: Could not resolve hostname ip-172-31-43-162.us-east-2.compute.internal: Name or service not known
First thing, ssh ubuntu#ip-172-31-43-162.us-east-2.compute.internal this will only work within VPC, as its private IP and your local system will never able to resolve this DNS until you connect with some VPN from that VPC.
It's not a security group issue, your instance is reachable.
You should post debugs log for ssh ubuntu#ec2-18-222-170-204.us-east-2.compute.amazonaws.com, you can get debug log using ssh -v ubuntu#ec2-18-222-170-204.us-east-2.compute.amazonaws.com
But I am sure it's not picking the pem file from right location. Two possible reason
set chmod 400 your.pem and then try to connect
specify the key in your ssh command
ssh -i path_to_key/private.pem ubuntu#ec2-18-222-170-204.us-east-2.compute.amazonaws.com
Might be possible user ubuntu not exist in that case you can try with root and AWS will return the user which can ssh to that server.
You can also get ssh command for AWS ec2 console. -> Select Instance and click connect, copy the command and try to connect.

I can SSH to one instance via bastion, but not to a second instance

I supposed to add the ssh key and then ssh to the AWS instance using a jumphost in 2 operations
So, first adding the key
ssh-add ~/.ssh/<key-file>.pem
Then ssh to jumphost
ssh -A ec2-user#jumphost
And then from jumphost to instance
ssh ec2-user#<private IP>
This works for one instance but does not work for another instance - cannot ssh to this instance from a jumphost.
What instance setting can prevent me to do ssh?
The output of ssh -v ec2-user#
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to <private ip>[<private ip>] port 22
There are different causes of this issue.
Check the security group of the ec2 instance you are connecting from the jump host / bastion if port 22 is open. If it's not open, add the rule. You can verify this if you telnet the destination server on port 22 form the jump host / bastion. command: telnet destinationip 22
if the above doesn't work, check if the ec2 instance you are connecting is in a running state with 2/2 checks passed. This will make sure that both network and operating system are functional.
if the above doesn't work, check if your Network Access List (NACLs) are allowing the inbound and outbound traffic. The default setting allows the traffic.
Make sure the the firewall is setup correctly! That is mandatory in AWS console when you setup your new instance. My guess is a wrong default was selected.
The reason was pointed by the user LogicIO. Somehow, one instance was moved to the different VPC and therefore the jumphost (bastion) and the instance appeared on the different VPC's, so connection stopped working. I needed a different jumphost.

Cannot reach amazon EC2 instance from local using SSH

Cannot ssh into Amazon EC2 instance, this seems to be very common problem but I have tried everything suggested in all available documents, anyone else have any idea what is missing from below?
Created new EC2 instance and downloaded the .pem file
Created a new inbound rule in EC2 instance Security group allow my local IP
Created a new inbound rule in EC2 instance Network ACL to allow my local IP
Created a new outbound rule in EC2 instance Network ACL to reach my local IP
Made sure the VPC route is attached to internet gateway
Made sure EC2 instance is attached to correct security group
After all this when I try to ssh from my local machine I'm getting connection timed out ,is there anything else I have to do
I have also disabled firewall and tested just incase
ssh -vvv -i key.pem ec2-user#********
OpenSSH_7.1p2, OpenSSL 1.0.1g 7 Apr 2014
debug1: Reading configuration data /etc/ssh_config
debug2: ssh_connect: needpriv 0
debug1: Connecting to ec2-instance [********] port 22.
debug1: connect to address ****** port 22: Connection timed out
ssh: connect to host ******* port 22: Connection timed out
A timeout is normally an indication that there is no network connectivity. This is almost always related to the Security Group. It the error returns immediately, it means that the instance received, but rejected, the connection. If it takes a long time to respond, it typically means that the request is not reaching the instance.
Some suggestions:
Confirm that the instance has been assigned a Public IP address
Confirm that the security group is permitting Inbound Port 22 (SSH) for your IP address. In fact, for testing purposes, temporarily allow it from 0.0.0.0/0 just to eliminate one potential cause of the problem.
Remove your NACL entries. The default NACL entries permit ALL inbound and outbound access. Your changes might have negatively impacted this.
Confirm that the it is a Linux instance (Windows does not support SSH by default)
Confirm the instance is in the Public Subnet (the one with your route to the Internet Gateway)
It is possible that your network is rejecting the connection (eg due to firewall rules). Try using a different computer and/or a different network (eg home vs office) to eliminate this as a cause.