In this post, we are going to learn “How to connect to EC2 instance using SSH” and what are the prerequisite for it.
Primary Usernames for EC2 Instances:
Depending on the type of EC2 instance you have setup, below are the default / primary usernames:
“ec2-user” (Amazon Linux, Red Hat Linux, SUSE Linux)
“root” (Red Hat Linux, SUSE Linux)
“ubuntu” (Ubuntu Linux distribution)
“fedora” (Fedora Linux distribution)
Identify your Ec2 Instance Name
To SSH into your EC2 instance, you’ll need to grab its the EC2 Public DNS URL. It will look something like below:
To find this Public DNS URL, do the following:
- Login to AWS Console – http://aws.amazon.com/Sign
- Click on Services on the nav menu, and then select EC2. Click on the Running Instances link
- Select the EC2 Instance and make note of the Public DNS URL. That will be the URL you will use to SSH into the server.
Or Select the EC2 Instance and make note of the Public IP which will be the IP you will use to SSH into servers.
SSH Key (.pem file) provided by Amazon. This SSH key is provided by Amazon when you launch the instance.
IP address: IP address assigned to your ec2 instance.
Username: The username depends on the Linux distro you just launched.Usually, these are the usernames for the most common distributions:
Only for Windows Users: Putty SSH Client installed on your PC. You can download the latest version here: https://www.putty.org/
Connect to EC2 instance using SSH Using Linux
Open your terminal and change directory with command cd, where you downloaded your pem file. In this demonstration, pem file is stored in the downloads folder.
- Type the SSH command with this structure:
ssh -i file.pem username@ip-address
This is the explanation of the previous command:
- ssh: Command to use SSH protocol
- -i: Flag that specifies an alternate identification file to use for public key authentication.
- username: Username that uses your instance
- ip-address: IP address given to your instance
- After pressing enter, a question will prompt to add the host to your known_hosts file. Type yes. This will help to recognize the host each time you’re trying to connect to your instance.
- And that’s it! Now you’re logged in on your AWS instance
Connect to EC2 instance using SSH Using Windows
- Open your PuttyGen(it is included with your Putty Client installation) on your PC.
- Make sure the checkbox “RSA” is selected.
- Click load and go to the folder where you have stored your pem file, select it and choose open.
- You will see this message.
- After the Key is loaded, click on save private key. Then a message will prompt, select yes.
- Now type a name for your key. For this example, the key will be named “instance” (it is not necessary to add the .ppk to the name file, Putty will do it)
- Now close PuttyGen program and open Putty. Go to SSH section and double-click it.
8.Go to Auth section and select the ppk file that we just created.
- Go back at the top in the Session section. Fill the field Hostname (or IP address) with the IP address given to your AWS instance and click open.
- A warning will prompt. The server host is not registered on our PC, so we must add it. Click yes.
- A window will prompt asking for the username, type your distro username, in this demonstration, username is ubuntu.
- And there you go! Now you’re connected to your AWS instance using Putty on your Windows OS.
As you can see, steps are quite simple, but sometimes we can mess up the SSH conversion on Windows or how to get your instance information, in case that happens or you forget any step, feel free to come back and take a look on this tutorial.
1) There’s a connection timeout error:
This is a security group issue. Any timeout (not just for SSH) is related to security groups or a firewall. Ensure your security group looks like this and correctly assigned to your EC2 instance.
2) There’s still a connection timeout issue
If your security group is properly configured as above, and you still have connection timeout issues, then that means a corporate firewall or a personal firewall is blocking the connection. Please use EC2 Instance Connect as described in the next post.
3) SSH does not work on Windows
- If it says: ssh command not found, that means you have to use Putty
- Follow again the video. If things don’t work, please use EC2 Instance Connect as described in the next lecture
4) There’s a connection refused
This means the instance is reachable, but no SSH utility is running on the instance
- Try to restart the instance
- If it doesn’t work, terminate the instance and create a new one. Make sure you’re using Amazon Linux 2
5) Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
This means either two things:
- You are using the wrong security key or not using a security key. Please look at your EC2 instance configuration to make sure you have assigned the correct key to it.
- You are using the wrong user. Make sure you have started an Amazon Linux 2 EC2 instance, and make sure you’re using the user ec2-user. This is something you specify when doing ec2-user@<public-ip>(ex: firstname.lastname@example.org) in your SSH command or your Putty configuration
6) Nothing is working – “aaaahhhhhh”
Don’t panic. Use EC2 Instance Connect from the next post.
7) I was able to connect yesterday, but today I can’t connect.
This is probably because you have stopped your EC2 instance and then started it again today. When you do so, the public IP of your EC2 instance will change. Therefore, in your command, or Putty configuration, please make sure to edit and save the new public IP.
That’s all for how to connect to EC2 instance using SSH in windows and linux..