For some time, Linux and its derivatives or distributions have been the most widely used platform in the cloud. Since many of these Linux instances are headerless, meaning they don’t have a graphical user interface (GUI), you need to access them through the command line.
In this post, we will show you how to connect to your Linux EC2 instance via SSH. We are using a Mac, so some of our screenshots will be taken from the Mac UI.
Source: https://thecloudmarket.com/stats#/by_platform_definition
SSH logins are typically done using public key authentication, which requires users to have a private key. While you can generate a public / private key pair using a third-party tool and then import the public key into Amazon EC2, we’re going to go the normal route of generating keys from Amazon EC2 itself. Now let me show you how this is done.
Generating keys in Amazon EC2
Sign in to the AWS Management Console.
Expand the All Services drop-down list and click EC2 under Calculations.
Scroll down the left sidebar until you reach the Network & Security section, and then click the Key Pairs menu.
Click the Generate Key Pair button.
Give the key pair a name and click the Create button.
Once a key pair is generated, its name will appear in the list of key pairs, and the private key will be automatically downloaded by your browser. Keep this private key file in a safe place and remember where you kept it as you will need it every time you connect to your Linux EC2 instance via SSH.
Before you can use this private key file, you need to change its permissions. Launch the Mac terminal from the Dock, or launch Mission Control (F4 key) and then go to Other Terminal.
Change to the directory containing your private key file and type:
chmod 400 nameofyourpravatekey.pem
You can now start using your private key to authenticate to your Amazon EC2 Linux instance over SSH.
I will not go into the details of creating a Linux EC2 instance as it should be owned by a separate post, but once you start such an instance, you will need to select the key pair you created so that Amazon EC2 knows which private key file is expected when connecting via SSH.
Once you start the instance, take note of its public DNS hostname or public IP address, as you will also need one of these when connecting via SSH.
To SSH into your EC2 instance from your Mac, simply go back to your terminal (or start it again if you’ve closed it already), navigate to the directory containing your private key file, and then type:
ssh – i ./nameofyourprivatekey.pem ec2-user @ ipaddress or hostname
For example (this is one line):
ssh -i ./myfirstkeypair.pem ec2-user@ec2-54-88-51-10.compute-1.amazonaws.com
After that, you should get something like the message below. Just enter yes.
Unable to authenticate host ec2-54-88-51-10.compute-1.amazonaws.com (54.88.51.10). ECDSA key fingerprint – SHA256: dDmWU5MGZhIxAVEajRTRLSQvQ1OIbVNy3Et1FInpKVg. Are you sure you want to continue connecting (yes / no)? yes
You should end up with something like this:
Warning: “ec2-54-88-51-10.compute-1.amazonaws.com, 54.88.51.10” (ECDSA) has been added to the list known hosts. -bash: warning: setlocale: LC_CTYPE: unable to change locale (UTF-8): no such file or directory [ec2-user @ ip-172-30-0 -93 ~] $
Once you see ec2-user @ the IP address of your ec2 instance as an invitation, you can start celebrating. You have successfully connected to your Linux EC2 instance via SSH.
Note. One common mistake is choosing the wrong key pair when starting an instance, so make sure that the private key you use when logging in over SSH and the key pair you selected earlier match. If they don’t match, you get something like:
Permission denied (publickey, gssapi-keyex, gssapi-with-mic)
We’re done here. See you next time. Enjoy!