AWS EC2 Linux实例连接失败:无可用认证方法(server sent:publickey)求助
Hey there, let's work through this SSH connection problem you're hitting with your EC2 Linux instance. First off, let's clear up your quick question: a terminated instance has no impact at all on your running instance's connectivity—so that's definitely not the root cause here.
The error message Disconnected : No supported authentication methods available (server sent :publickey) tells us the server only accepts public key authentication, but your client isn't successfully passing that verification. Even though your key pair works for other instances, let's walk through the most common fixes step by step:
Fix your .pem key file permissions
Linux systems are super strict about key file access—if your .pem file is readable by anyone besides you, SSH will reject it immediately. Run this command on your local machine to set the correct permissions:chmod 600 /path/to/your/key-pair-file.pemVerify your instance's security group allows SSH access
Double-check your instance's security group inbound rules to make sure TCP port 22 (SSH) is open to your current IP address (you can temporarily use0.0.0.0/0for testing, but remember to lock this down to your specific IP later). You can edit this in the EC2 Console under your instance's "Security" tab.Confirm the instance is using the correct key pair
It's easy to accidentally pick the wrong key pair when launching an instance. Head to the EC2 Console, pull up your instance's details, and check the "Key pair name" field—this must match the name of the .pem file you're using. If it doesn't, you'll either need to launch a new instance with the correct key pair or use EC2 Instance Connect to set up alternative access (if your instance supports it).Double-check your SSH command syntax
Make sure you're using the right command structure—small typos can break everything:ssh -i /path/to/your/key-pair-file.pem ec2-user@<your-instance-public-ip-or-dns>Note: For non-Amazon Linux distros (like Ubuntu), the default user might be
ubuntuinstead ofec2-user—but since you mentioned usingec2-user, this should be correct for your setup.Check the SSH server configuration on the instance
If you can connect via EC2 Systems Manager Session Manager (a great backup option when SSH fails), log into the instance and verify the SSH daemon allows public key auth:- Open the SSH config file:
sudo nano /etc/ssh/sshd_config - Ensure the line
PubkeyAuthentication yesis uncommented and set toyes. - Restart the SSH service to apply changes:
sudo systemctl restart sshd
- Open the SSH config file:
If none of these steps fix the issue, you could try re-downloading your key pair file (to rule out corruption) or checking if your local SSH client has any odd config overrides in ~/.ssh/config.
内容的提问来源于stack exchange,提问作者Cristopher

