Skip to content

How do I troubleshoot "Connection refused" or "Connection timed out" errors when I use SSH to connect to my Amazon EC2 instance?

8 minute read
3

I receive "Connection refused" or "Connection timed out" errors when I use SSH to connect to my Amazon Elastic Compute Cloud (Amazon EC2) instance.

Resolution

Troubleshoot the "Connection refused" error

If a host remotely refuses your connection, then you receive the following error message:

"ssh: connect to host ec2-X-X-X-X.compute-1.amazonaws.com port 22: Connection refused"

To troubleshoot this issue, make sure that there isn't a firewall on the EC2 instance that blocks the SSH connection. Also, make sure that the SSH daemon (sshd) is running and listens on port 22.

Note: To check the preceding configurations, you must have operating system (OS) access to the instance.

Troubleshoot the "Connection timed out" error

If the server doesn't respond to the client, then the client program times out and you receive the following error message:

"ssh: connect to host ec2-X-X-X-X.compute-1.amazonaws.com port 22: Connection timed out"

To troubleshoot this issue, check the following settings:

For more troubleshooting steps, see Error connecting to your instance: Connection timed out.

Troubleshoot issues with instances that pass their status checks

Run the AWSSupport-TroubleshootSSH runbook to automatically resolve issues

To automatically identify and resolve SSH issues, run the AWSSupport-TroubleshootSSH runbook. The runbook installs the EC2Rescue tool on your instance to identify and fix issues that block a remote SSH connection to a Linux host.

Manually troubleshoot OS-level issues

Use the EC2 Serial Console or Session Manager, a capability of AWS Systems Manager, to connect to your instance. Then, troubleshoot SSH configuration issues.

To use the EC2 Serial Console, you must use a supported instance type and configure access to the serial console. Also, make sure that each instance that uses the serial console has at least one password-based Linux user with sudo access. To reset the password for an AWS account with sudo access, run ssm-user. You can also use the EC2 Serial Console to troubleshoot OS-level issues such as boot issues and network configuration issues.

To use Session Manager, your instance must be a managed node and must meet the Session Manager prerequisites. The instance's AWS Systems Manager Agent (SSM Agent) ping status must be Online. Also, the attached AWS Identity and Access Management (IAM) role must have AmazonSSMManagedInstanceCore permissions.

To update your configuration so that it doesn't block SSH access, complete the following steps:

  1. Use the EC2 Serial Console to connect to the instance as a password-configured Linux user. Or, use Session Manager to start a session.

  2. If you configured iptables rules, then run the following command to add a rule in iptables that accepts all SSH connections on port 22:

    sudo iptables -I INPUT 1 -p tcp --dport 22 -j ACCEPT
  3. If you use an OS-based firewall, then deactivate it. It's a best practice to use security groups instead of a firewall. To deactivate your OS-based firewall, run the following commands based on your OS.
    Important: The following commands clear all main iptables rules and delete existing rules. The commands also add a rule that allows inbound SSH connections and change the default policy of the main chain to ACCEPT. With this configuration, you don't affect instance network connectivity when you clear the iptables rule.
    Distributions that use Uncomplicated Firewall (UFW) such as Ubuntu and Debian:

    sudo iptables -F
    sudo iptables -P INPUT ACCEPT
    sudo ufw disable

    Distributions that use firewalld such as Red Hat Enterprise Linux (RHEL) and CentOS:

    sudo iptables -F
    sudo iptables -P INPUT ACCEPT
    sudo systemctl disable firewalld

    Note: After you regain access to your instance, check your firewall configuration.

  4. To verify that SSH is running and that TCP port 22 is in the listening state, run the following commands:

    sudo systemctl restart sshd
    sudo ss -tpln | grep -iE '22|ssh'
    LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1901,fd=3))LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1901,fd=4))

    Note: If your system doesn't have the ss command, then replace ss with the legacy netstat command. Make sure to use the same syntax as the preceding command.

  5. To make sure that the TCP Wrapper doesn't block an SSH connection, run the following command:

    if [[ $( cat /etc/hosts.[ad]* | grep -vE '^#' | awk 'NF' | wc -l) -ne 0 ]];\
    then sudo sed -i '1i sshd2 sshd : ALL: allow' /etc/hosts.allow; fi
  6. Use SSH to connect to the instance.

  7. Disconnect from the serial console session. Or, end the session in Session Manager.

Use a user data script to troubleshoot your instance

If you can't use the runbook, serial console, or Session Manager to connect to your instance, then use a user data script to troubleshoot issues. In the user data script, deactivate the OS-level firewall and the TCP Wrapper. Then, restart the sshd service.

Configure your instance for a stop and start

Note: When you stop and start an instance, the instance's public IP address changes. It's a best practice to use an Elastic IP address to route external traffic to your instance instead of a public IP address. If you use Amazon Route 53, then you might need to update the Route 53 DNS records when the public IP address changes. A stop and start is different from an instance reboot. For more information, see How EC2 instance stop and start works.

Before you stop and start your instance, take the following actions:

Configure user data for the instance

Complete the following steps:

  1. Stop the instance.
  2. Choose the instance, and then choose Actions.
  3. Select Instance settings.
  4. Choose Edit user data, and then enter the following user data script:
    Content-Type: multipart/mixed; boundary="//"
    MIME-Version: 1.0
    
    --//
    Content-Type: text/cloud-config; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="cloud-config.txt"
    
    #cloud-config
    cloud_final_modules:
    - [scripts-user, always]
    
    --//
    Content-Type: text/x-shellscript; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="userdata.txt"
    
    #!/bin/bash
    iptables -P INPUT ACCEPT
    iptables -F
    systemctl restart sshd.service || service sshd restart
    if [[ $( cat /etc/hosts.[ad]* | grep -vE '^#' | awk 'NF' | wc -l) -ne 0 ]];\
    then sudo sed -i '1i sshd2 sshd : ALL: allow' /etc/hosts.allow; fi
    --//
    Note: The preceding user data script runs at each instance reboot. This method removes all main iptables rules.
  5. Choose Save.
  6. Use SSH to connect to the instance.
  7. Remove the user data script, and then confirm that your firewall configuration is accurate.

Related information

How do I troubleshoot Amazon EC2 instance connection timeout errors from the internet?

How do I resolve SSH connection issues to my Amazon EC2 Linux instance?

7 Comments

Check if kes are in SHA-1 or some warrants - if yes then try "update? change keys to RSA . The another advice is if you user PUTTY for SSH connection, followed steps are recommended:

  • backup all keys (and on your decision PUTTY confit)
  • install (update )putty to the latest stable version
  • try to connect, in case of no change disable/enable compression and check connection

Trying to lower key file access privileges may also help mostly if not only you are only user of one stem account.

In my opinion upgrading keys to the latest standard (avoiding MD5 fingerprinting) or changing them to latest version will help. If this is new instance and you can't connect - verify Security Groups for PORT 22 - if port is open limit it to accept only your IP and try to connect. Soft restart of instance may be some solution in scenarios of resources problem.

Not elegant way that helps (USE IT ONLY AT INSTANCES WITHOUT CRITICAL/LIVE SERVICES to avoid data loss, USE OF IT IS JUST EXAMPLE based on own experience where asap action was needed and this fast way helped , in my case it was data breach - it was on ARM IoT device without chance to physical access over the same day) sio "hard reset" allowed our learning emploee diagnoze data breach and secure device with redirect pockets for analyse. On AWS use this only as I advised - DO NOT USE or USE at very last choice ;)

replied 3 years ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

AWS
EXPERT

replied 3 years ago

I was experiencing this same error message "Connection Timed Out", when I tried to SSH using the SSH Client, at that moment my security group was only permitting inbound traffic via SSH from my local IP, but when I reconfigured it to allow SSH traffic from anywhere (0.0.0.0), the error message cleared and I was able to successfully SSH into the Amazon Linux 2 instance.

I had to use this type of security group configuration, because this was a testing environment, so in a production environment, how does one resolve this error message when using a local IP from my computer?

Thanks

replied 3 years ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

AWS
EXPERT

replied 3 years ago

Here is the official resolution video from AWS. Please check it out.

How do I fix Connection refused or Connection timed out errors when connecting to an EC2 instance? https://www.youtube.com/watch?v=b1K0CNYxtC0

replied 2 years ago

For Latest Ubuntu

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type:
    text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
sed -i "s|PasswordAuthentication no|PasswordAuthentication yes|" /etc/ssh/sshd_config

if [ -d "/etc/ssh/sshd_config.d" ]; then
    for file in /etc/ssh/sshd_config.d/*; do
        if [ -f "$file" ]; then
            sed -i "s|PasswordAuthentication no|PasswordAuthentication yes|" "$file"
        fi
    done
fi

echo -e 'ubuntu:YourPasswordHere' | chpasswd
systemctl restart sshd

iptables -P INPUT ACCEPT
iptables -F
if [[ $( cat /etc/hosts.[ad]* | grep -vE '^#' | awk 'NF' | wc -l) -ne 0 ]];\
then sudo sed -i '1i sshd2 sshd : ALL: allow' /etc/hosts.allow; fi

--//
AWS

replied a year ago

This article was reviewed and updated on 2026-06-17.

EXPERT

replied 3 months ago