How to SSH into a Linux EC2 Instance?
How to SSH into a Linux EC2 Instance
Amazon Web Services (AWS) provides a robust cloud platform with various services, among which Amazon Elastic Compute Cloud (EC2) is one of the most widely used. EC2 instances are virtual servers that can be configured to meet specific computing needs. Securely accessing these instances is crucial, and this is typically done using Secure Shell (SSH). This guide will walk you through the process of SSH-ing into a Linux EC2 instance step by step.
Prerequisites:
Before you can SSH into your EC2 instance, you need to have the following:
1. AWS Account: Ensure you have an active AWS account.
2. EC2 Instance: A running EC2 instance. If you don’t have one, you can launch an instance via the AWS Management Console.
3. Key Pair: A key pair (.pem file) generated and downloaded when you created your EC2 instance.
4. SSH Key Pair: When you launch an EC2 instance, you need to specify an SSH key pair. If you don’t have a key pair, you can create one in the AWS Management Console. Ensure you download the private key (.pem file) and keep it secure.
Steps to SSH into Your EC2 Instance
Step 1: Locate Your EC2 Instance
- Log in to the AWS Management Console
- Navigate to the EC2 Dashboard by selecting “EC2” from the Services menu.
- Under “Instances,” locate the instance you want to connect to. Note the Public DNS (or IPv4 address) and Instance ID of your instance.
Step 2: Set Permissions on Your Private Key File
-
Open your terminal.
-
Navigate to the directory where your key pair file is located.
Eg: Your file is located in the ‘test’ directory.
For security reasons, the .pem
file must have restricted permissions. Use the following command to set the correct permissions on your private key file:
icacls.exe your_key_name.pem /reset
icacls.exe your_key_name.pem /grant:r "$($env:username):(r)"
icacls.exe your_key_name.pem /inheritance:r
Step 3: Connect to Your Instance Using SSH
In the terminal window use the SSH command to connect to your EC2 instance. The basic syntax for the SSH command is:
ssh -i /path/to/your-key-pair.pem ec2-user@your-instance-public-dns
Replace /path/to/your-key-pair.pem
with the path to your .pem
file and your-instance-public-dns
with the Public DNS of your instance.
For example, if your .pem
file is located at ~/test/test-2.pem
and your instance’s Public DNS is ec2-13-235-94-125.ap-south-1.compute.amazonaws.com
the command will be:
ssh -i ~/test/test-2.pem ec2-13-235-94-125.ap-south-1.compute.amazonaws.com
Troubleshooting Connection Issues
If you encounter issues connecting to your EC2 instance, consider the following troubleshooting steps:
1. Security Group Settings: Ensure your instance’s security group allows inbound SSH traffic on port 22. This can be checked and modified in the EC2 Dashboard under “Security Groups.”
2. Public IP Address: Ensure you are using the correct Public DNS or IP address. Instances without an Elastic IP might have different IP addresses after being stopped and started.
3. Instance State: Ensure your instance is in the running state.
4. Network ACLs: Check your network ACLs to ensure they are not blocking your IP address.
Step 4: Additional Configuration (Optional)
Static IP Address
To avoid changes in Public DNS/IP address every time the instance is restarted, consider associating an Elastic IP address with your instance. Elastic IP addresses are static and can be attached to any instance in your account.
-
In the EC2 Dashboard, navigate to “Elastic IPs” under the “Network & Security” section.
-
Allocate a new Elastic IP and associate it with your instance.
SSH Configuration File
To simplify the SSH command, you can create an SSH configuration file (~/.ssh/config). Add the following configuration to the file:
Cd ~
Cd .ssh
Config File Code:
Host my-ec2-instance
HostName ec2-198-51-100-1.compute-1.amazonaws.com
User ec2-user
IdentityFile ~/Downloads/my-key-pair.pem
With this configuration, you can connect to your instance using a simpler command:
ssh my-ec2-instance
Step 5: Disconnecting
When you are finished with your SSH session, simply type exit in the terminal to disconnect from the EC2 instance.
exit
Conclusion:
SSH-ing into a Linux EC2 instance is a fundamental skill for anyone working with AWS. By following these steps, you should be able to securely connect to your instance and manage it as needed. Remember to keep your private key secure and to follow best practices for securing your instances, such as regularly updating software and using security groups effectively. By mastering these basics, you can take full advantage of the flexibility and power that AWS EC2 provides.