Cannot connect to EC2 - ssh: connect to host port 22: Connection refused

  1. In AWS, navigate to Services > EC2.
  2. Under Resources, select Running Instances.
  3. Highlight your instance and click Connect.
  4. In Terminal, cd into the directory containing your key and copy the command in step 3 under "To access your instance."
  5. In Terminal, run: ssh -vvv -i [MyEC2Key].pem [email protected](xx.xx.xx.xx = your EC2 Public IP) OR run the command in the example under step 4.

My debugging steps to EC2 connection time out

  1. Double check the security group access for port 22

  2. Make sure you have your current IP on there and update to be sure it hasn't changed

  3. Make sure the key pair you're attempting to use corresponds to the one attached to your EC2

  4. Make sure your key pair on your local machine is chmod'ed correctly. I believe it's chmod 600 keypair.pem check this

  5. Make sure you're in either your .ssh folder on your host OR correctly referencing it: HOME/.ssh/key.pem

  6. Last weird totally wishy washy checks:

    • reboot instance
    • assign elastic IP and access that
    • switch from using the IP to Public DNS
    • add a : at the end of user@ip:

Totally mystical debugging sets for 6 though. That's part of the "my code doesn't work - don't know why. My code does work - don't know why." Category

Note: If you access your EC2 while you are connected to a VPN, do know that your IP changes! So enable incoming traffic from your VPN's IP on your EC2 security group.


Just check if your public ip that you get when you are on VPN is configured as a source address in the SG inbound entry that opens up port 22.

You can check your ip using https://www.google.co.in/search?q=whats+my+ip, when connected to your VPN.