You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过SSH连接AWS服务器,请求排查与解决方法

Troubleshooting Your AWS EC2 SSH Connection Failure

Let's work through this issue step by step—first, let's start with the clear clues from your debug output before diving into router firewall assumptions.

1. Fix the Key File Issue (Top Priority from Logs)

Your debug output explicitly flags a problem with your keypair file:

debug1: key_load_public: No such file or directory
debug1: identity file <keypair.pem> type -1

This means SSH can't locate your keypair, or the path you're using is incorrect. Here's how to resolve this:

  • Verify the file path: If <keypair.pem> isn't in your current working directory, use the full absolute path (e.g., /home/your-username/aws-keys/my-keypair.pem) instead of a relative path.
  • Set strict file permissions: SSH requires keypair files to have restricted access (no read/write permissions for other users). Run this command to fix permissions:
    chmod 600 <keypair.pem>
    
    If permissions are too open, SSH will refuse to use the key even if it finds it.

2. Validate EC2 Instance & AWS-Side Configurations

Even with a correct key, AWS-side settings could block your connection:

  • Check Security Group Rules: Ensure your EC2 instance's security group has an inbound rule allowing SSH (port 22) from your local public IP address (use 0.0.0.0/0 temporarily for testing, but restrict it back to your IP later for security).
  • Confirm Instance Status: Make sure the EC2 instance is in the running state and has passed both system and instance status checks (verify this in the AWS Console).
  • Double-Check the Username: ec2-user works for most Amazon Linux AMIs, but other distributions use different defaults:
    • Ubuntu AMIs: Use ubuntu
    • CentOS AMIs: Use centos
    • Debian AMIs: Use admin

3. Troubleshoot Router/Local Network Issues (Your Initial Guess)

If the above steps don't fix the problem, let's test your local network and router:

  • Test Basic Connectivity:
    • Ping the EC2 instance's public IP: ping <ip> (note: some EC2 instances block ICMP, so this might fail even if SSH works—don't rely solely on this).
    • Test port 22 with telnet: telnet <ip> 22. A timeout or "connection refused" message means the port is likely blocked.
  • Check Local Firewall: On your Ubuntu machine, verify if ufw (Uncomplicated Firewall) is blocking outbound SSH:
    ufw status
    
    If there's a rule denying outbound port 22, allow it with ufw allow out 22.
  • Test with a Different Network: Try connecting via a mobile hotspot or another network. If the connection works, your router is likely blocking outbound port 22. Check your router's admin panel for firewall rules that restrict SSH traffic (look for "outbound rules" or "port filtering").
  • Check for AWS IP Blocking: Multiple failed connection attempts might trigger a temporary IP block from AWS. Test from a different IP, or request a block removal via AWS Support if needed.

Final Tip

If you still can't connect, share the full -vvv debug output—the truncated section might show handshake failures or other critical details that can narrow down the issue.

内容的提问来源于stack exchange,提问作者user8397275

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:55:53