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

|▌冷眼眸甩不掉的悲伤 提交于 2020-05-25 06:53:33

问题


I am currently overseas and I am trying to connect to my EC2 instance through ssh but I am getting the error ssh: connect to host ec2-34-207-64-42.compute-1.amazonaws.com port 22: Connection refused

I turned on my vpn to New York but still nothing changes. What reasons could there be for not being able to connect to this instance?

The instance is still running and serving the website but I am not able to connect through ssh. Is this a problem with the wifi where I am staying or with the instance itself?


回答1:


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.




回答2:


  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 ec2-user@xx.xx.xx.xx(xx.xx.xx.xx = your EC2 Public IP) OR run the command in the example under step 4.



回答3:


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.




回答4:


Had the same problem after creating some instances on a new VPC. (If internet SSH worked before this solution may not work for you)

When creating a new VPC, make sure you create an internet gateway (VPC -> Internet Gateways)

And also make sure that your VPC's routing table (VPC -> Route Tables) has an entry which redirects all IPs (or just your IP) to the internet gateway you just created.



来源:https://stackoverflow.com/questions/44802363/cannot-connect-to-ec2-ssh-connect-to-host-port-22-connection-refused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!