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

AWS EC2 Ubuntu 16.*实例SSH连接每隔10分钟卡顿求助

Fixing SSH Timeouts on Ubuntu 16.x AWS EC2 Instances

Hey there, let's tackle that annoying SSH timeout issue you're dealing with—where your connection freezes every 10 minutes and you have to reconnect. I've run into similar headaches before, so here are some proven fixes you can implement right away:

1. Tweak Your Local SSH Client Settings

The simplest fix starts on your local machine. We'll configure your SSH client to send regular keepalive packets to the EC2 instance, preventing the connection from going idle.

  • Open (or create) your local SSH config file:
    nano ~/.ssh/config
    
  • Add these lines tailored to your instance:
    Host my_IP
      User ubuntu
      IdentityFile /full/path/to/my_app.pem
      ServerAliveInterval 30
      ServerAliveCountMax 3
    
    • ServerAliveInterval 30: Sends a keepalive packet every 30 seconds to the server
    • ServerAliveCountMax 3: Allows 3 failed keepalive attempts before disconnecting (adjust if needed)
  • Save the file (Ctrl+O, press Enter, then Ctrl+X) and reconnect to your instance. This should keep the connection alive through idle periods.

2. Adjust the SSH Server Configuration on EC2

For extra reliability, you can also configure the EC2 instance's SSH server to send keepalive packets back to your client.

  • Connect to your instance (one last time manually if needed) and edit the SSH server config:
    sudo nano /etc/ssh/sshd_config
    
  • Find and uncomment/modify these lines (add them if they don't exist):
    ClientAliveInterval 30
    ClientAliveCountMax 3
    TCPKeepAlive yes
    
  • Save the file and restart the SSH service to apply changes:
    sudo systemctl restart sshd
    
    This ensures the server actively maintains the connection, even if your client's keepalives don't make it through.

3. Understand AWS VPC Idle Timeouts

AWS VPC has a default TCP idle timeout of 350 seconds (around 5.8 minutes), which is likely contributing to your 10-minute disconnects. The keepalive settings above will send traffic before this timeout kicks in, so they should resolve the issue. If you're using a NAT gateway or load balancer, they share the same timeout—our keepalive packets will keep the connection active through these too.

4. Alternative: Use AWS Session Manager (No SSH Keys Needed)

If you want to avoid SSH entirely, AWS Systems Manager Session Manager is a more reliable alternative. It uses HTTPS to connect to your instance, so timeouts are far less common. Here's how to set it up:

  • Attach an IAM role to your EC2 instance with the AmazonSSMManagedInstanceCore policy (this grants the instance permission to communicate with Session Manager)
  • Check if the SSM Agent is installed (most Ubuntu 16.x AWS AMIs have it pre-installed). If not, install it:
    sudo apt-get update && sudo apt-get install -y amazon-ssm-agent
    sudo systemctl start amazon-ssm-agent
    
  • Head to the AWS Console > Systems Manager > Session Manager, select your instance, and start a session. You'll get a terminal directly in the browser, no SSH keys or public IP required.

Start with the first two fixes—they're quick and should resolve the timeout issue for most cases. If you still run into problems, give Session Manager a shot.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:32:47