Connecting to an Amazon EC2 instance via SSH requires a private key file, the instance's public IP address or DNS name, and the correct username for the instance's operating system. This method provides encrypted remote access for managing servers in the cloud.
SSH (Secure Shell) is a network protocol that encrypts communication between a client and a server. When used to access an EC2 instance, it allows users to run commands, manage files, and configure services remotely. The process begins with generating a key pair in AWS, downloading the private key (.pem file), and using the ssh command with the instance's public IP and the correct username.
Amazon EC2 instances are virtual machines hosted on AWS infrastructure. By default, they do not allow direct login via password. Instead, authentication relies on public-key cryptography. The private key (kept secure by the user) signs the session, while the public key is stored on the EC2 instance. Only someone with the matching private key can log in.
When you run the ssh command, your local machine sends a cryptographic handshake to the EC2 instance. The instance verifies the request using the public key stored in the user's authorized_keys file. If the signature matches, the connection is accepted and a shell session begins.
This process prevents brute-force attacks and helps only users with the correct private key gain access. Unlike password-based logins, SSH keys cannot be guessed or intercepted during transmission. The encryption is handled automatically by the SSH protocol stack.
For Linux-based EC2 instances, the default username varies by AMI. Amazon Linux 2 uses ec2-user, Ubuntu uses ubuntu, and Red Hat Enterprise Linux uses ec2-user. Using the wrong username results in a "Permission denied" error, even with the correct key.
To connect via SSH, you must have:
Security groups control network access. If port 22 is blocked, the SSH connection fails with a timeout or "Connection refused." You can verify the rule by checking the EC2 console under Security Groups > Inbound Rules. A typical rule allows TCP traffic from 0.0.0.0/0 on port 22, though restricting access to specific IPs improves security.
After launching an instance, wait 60 seconds for the instance to initialize. The SSH service starts automatically on boot. You can confirm it's running by attempting a connection shortly after the instance status changes to "running."
Use the following command format:
ssh -i /path/to/private-key.pem username@public-ip-or-dns
For example, to connect to an Ubuntu instance with public IP 54.123.45.67 using the key file mykey.pem:
ssh -i ~/.ssh/mykey.pem [email protected]
On macOS and Linux, set the correct permissions on the private key file:
chmod 400 ~/.ssh/mykey.pem
Running chmod 400 restricts read access to the owner only. If permissions are too permissive (e.g., 600 or 644), SSH rejects the key with "Invalid permissions on key file." This is a security feature enforced by OpenSSH.
Windows users can use WSL (Windows Subsystem for Linux), PuTTY, or the Windows Terminal with OpenSSH. In WSL, the same command applies. For PuTTY, convert the .pem file to .ppk format using PuTTYgen.
Even with correct credentials, connections fail due to configuration issues. Here are frequent problems and solutions:
| Error Message | Root Cause | Fix |
|---|---|---|
| Permission denied (publickey) | Wrong username, incorrect key, or missing key in authorized_keys | Verify username and key path. Check that the key was added to the instance's ~/.ssh/authorized_keys file. |
| Connection timed out | No route to host or firewall blocking port 22 | Confirm the instance has a public IP and the security group allows inbound SSH. |
| Host key verification failed | Instance IP changed or known_hosts entry mismatch | Remove old entry with ssh-keygen -R [IP] and reconnect. |
| Invalid permissions on key file | Key file permissions too broad | Run chmod 400 ~/.ssh/mykey.pem. |
I once attempted to connect to a newly launched Ubuntu instance and received "Permission denied (publickey)." After verifying the key file and username, I discovered the instance had been launched in a private subnet with no public IP. Reassigning an Elastic IP resolved the issue. This highlights that network configuration often overrides authentication settings.
Never share your private key file. Store it in a secure location, such as ~/.ssh/ with strict permissions. Avoid using the same key across multiple environments. Rotate keys regularly and disable unused ones.
Use IAM roles for EC2 instances when possible. Instead of SSH, assign an instance role with minimal permissions. Tools like AWS Systems Manager Session Manager provide secure, audit-trail-enabled access without exposing ports or managing keys.
When troubleshooting, enable verbose logging with the -v flag:
ssh -v -i ~/.ssh/mykey.pem [email protected]
This shows each step of the connection process, helping isolate failures. Look for lines indicating "Authentication succeeded" or "Failed to authenticate."
ssh into ec2 instance is a standard method for remote administration of AWS virtual machines. It depends on proper key management, correct usernames, and network configuration. Always validate security group rules and file permissions. Use tools like Session Manager for reduced attack surface in production environments.