I'm setting up my first Amazon EC2. I already created the instance with Ubuntu using a tutorial on youtube. I configured the instance correctly, the Security Groups, the Elastic IPs, the SSH ports are on 22 and open to the world. Unfortunately, I still have the error Network error: connection timed out when connecting by Putty. This is also configured with the EC2 keypar. I have already followed several tips here from the forum and from Amazon itself but the error persists. Unfortunately as I am in the free plan of them I have no support to help me. Is there another factor that could cause this error?

To try to connect I am doing the following procedure.

I downloaded the key pair, I used puttygen to create ppk, I open the putty and I put the elastic ip address in Host, SSH access -> Auth and I find ppk and I'm going to open it. After a while he gives this error.

  • Are you using the domain name or the Elastic IP address to access this instance? Oct 31, 2017 at 2:15
  • And can you allow ICMP on the Security-group And try to ping it? Oct 31, 2017 at 2:17
  • Yes, ping works fine using my elastic IP.
    – Anderson
    Oct 31, 2017 at 2:39
  • How about: telnet Your_Elastic_IP 22 Oct 31, 2017 at 3:12
  • Connecting to ... Could not open host connection on port 22: Connection failed
    – Anderson
    Oct 31, 2017 at 3:21

2 Answers 2


This must be a firewall issue.

  1. Check if your host firewall is preventing Putty from communicating through the firewall:

    • Go to Control Panel\System and Security\Windows Firewall\Allowed Programs and allow or add putty.
  2. Check if your router/Service Provider is blocking traffic (outgoing) to port 22


In creating the instance, we use the command below in the User Data field in the advanced options to change SSH port from TCP 22 to TCP 2222.

#!/bin/bash -ex
perl -pi -e 's/^#?Port 22$/Port 2222/' /etc/ssh/sshd_config
service sshd restart || service ssh restart

I do not understand much of Perl and Linux, but I'm sure someone here who understands will explain to us what all these commands do. Just know that User Data scripts run when the instance is created/launched.

These commands solved the problem of time out when connecting to the instance via ssh.

  • This Perl script (similar to sed -i) is changing the configuration for ssh which is in /etc/ssh/sshd_config. It is changing the port ssh is using. From port 22 to 2222. The script looks for a line that starts with # (if it exists) along with "Port 22" (at the end of the line) and replaces it with "Port 2222". Then it restarts the sshd service (or ssh if sshd doesn't succeed) Nov 3, 2017 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.