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

企业AWS EC2多管理员共用.pem登录SSH/Putty时挂起求助

Fixing AWS EC2 SSH Hang Without Restarting the Instance

Hey there, I feel your pain—constantly restarting EC2 instances just to regain access is a huge hassle, especially with a team relying on the server. Let’s break down actionable steps to get back in safely without rebooting, plus fix the root causes long-term:

1. Bypass SSH entirely with AWS Systems Manager Session Manager

This is my go-to fix for unresponsive SSH connections because it doesn’t rely on port 22 at all. Here’s how to use it:

  • First, make sure your EC2 instance has an IAM role attached with the AmazonSSMManagedInstanceCore policy (this gives SSM permission to access the instance).
  • Head to the AWS Console → Systems Manager → Session Manager.
  • Select your instance and click "Start session"—you’ll get a fully functional terminal directly in the browser, no SSH keys or port forwarding needed.

Once you’re in via Session Manager, you can troubleshoot the SSH issue directly (like checking sshd logs or resource usage).

2. Diagnose the root cause without logging in

Before jumping into fixes, figure out why SSH is hanging:

  • Check EC2 instance metrics: Go to the EC2 Console → Your Instance → Monitoring tab. Look for spiked CPU, 100% memory usage, or disk space exhaustion—any of these can crash the sshd process or make the instance unresponsive.
  • Retrieve system logs: In the EC2 Console, right-click your instance → Monitor and troubleshoot → Get system log. Look for lines like Out of memory or sshd failed to start—this will point you to the problem.

3. Fix SSH remotely with AWS Run Command

If the instance is still responsive but SSH is dead, use Run Command to restart the sshd service without rebooting:

  • Go to AWS Console → Systems Manager → Run Command.
  • Select the AWS-RunShellScript document.
  • In the "Commands" field, enter:
    # For systemd-based systems (Amazon Linux 2, Ubuntu 16.04+)
    systemctl restart sshd
    
    # For older sysvinit systems (Amazon Linux 1)
    service ssh restart
    
  • Target your instance and run the command—this will restart the SSH daemon, which often resolves hang issues caused by crashed processes.

4. Long-term fixes (stop this from happening again)

Your setup of sharing a single .pem file is a security risk and can contribute to login issues. Here’s how to fix that:

  • Issue individual SSH keys: Create a separate EC2 key pair for each admin, add their public keys to the instance’s ~/.ssh/authorized_keys file, and let them use their own .pem files. No more shared credentials!
  • Move to Session Manager permanently: Ditch SSH keys entirely. Use IAM permissions to control which admins can access which instances via Session Manager—it’s more secure, easier to manage, and avoids SSH-related headaches.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:23:22