3.16 SSH over Linux&Mac
Now that we have created our first EC2 instance, we are going to connect to it from a Linux or macOS system. Our AWS machine is a Linux server with a public IP and a security group allowing inbound traffic on port 22. That security group is what makes SSH possible, since SSH is the most important tool we will use on EC2 servers: it lets us connect remotely and administer the machine through the command line. In this lesson we connect from a native terminal on Linux or macOS.
Connecting over SSH
- Open the EC2 console and copy the public IP address of your instance.
- Open your terminal and try a first plain connection:
ssh ec2-user@<public-ip>. - The first attempt fails with a permission denied error — that is normal, the server does not trust unknown clients.
- Use the private key downloaded when the instance was created with the
-iflag.
The full command becomes: ssh -i my-first-instance.pem ec2-user@<public-ip>. The terminal asks you to confirm the host fingerprint on the first connection, then logs you in. You are now inside your EC2 machine and can run any Linux command. For example whoami returns the current user (ec2-user), and a ping google.com confirms that the server has outbound network access.
To leave the SSH session, simply type exit or press Ctrl + D. You have learned how to connect to an AWS EC2 instance from a macOS or Linux terminal using only the standard ssh client and the .pem key delivered by AWS. In the next lessons we will see how to do the same thing from a Windows machine.
Summary
This lesson demonstrates how to establish SSH connections to AWS EC2 instances from Linux and Mac systems using the terminal. Students learn the SSH command syntax with their private key (.pem file) and the EC2 user account, how to troubleshoot permission denied errors, and how to execute basic commands once authenticated. The session covers safe disconnection methods and the essential role SSH plays in remote server administration across Unix-like systems.
Key points
- SSH (Secure Shell) on port 22 is the essential tool for secure remote access and command-line administration of AWS EC2 instances from Linux and Mac terminals
- Connect to EC2 instances using the syntax: ssh -i <private-key-file> ec2-user@<instance-ip-address>, where the -i flag specifies your downloaded .pem private key
- Permission denied errors occur when SSH cannot verify your identity; resolve this by providing the correct path to your .pem private key file using the -i flag
- Once authenticated and connected, you can execute system commands like whoami to verify your user identity and gather instance information
- Safely disconnect from an SSH session by typing exit or pressing Ctrl+D
FAQ
Why does SSH initially refuse permission to connect to my AWS instance?
SSH denies permission because it cannot authenticate your identity without the private key. You must use the -i flag to specify your .pem private key file that was downloaded when you created the EC2 instance.
What is the correct command to SSH into an AWS EC2 instance from Linux or Mac?
Use: ssh -i /path/to/your-key.pem ec2-user@<your-instance-ip>, where you replace the key path with your .pem file location and the IP with your instance's public IP address.
How do I exit an SSH session safely?
Type exit or press Ctrl+D on your keyboard to cleanly disconnect from the SSH session and return to your local terminal.