How to connect to AWS ec2 Linux instance using SSH?

Table of Contents

After launching your Linux instance on AWS, a common practice is to enable SSH connections for provisioning, configuring, or deploying your application. I have done these practices daily because all tools that I am using such as Ansible, Capistrano, Fabric, Github, and BitBucket, need an SSH connection.

How to connect to the AWS ec2 Linux instance using SSH?

Firstly, we must have an SSH Client installed on your development machine

For Windows users, you should install Git Bash (Not putty as recommended in the AWS document)

For Ubuntu users, please run this command Sudo apt-get install OpenSSH-client

For Mac users, please run ssh -V to make sure it has been installed

I suppose that you have known to create an AWS EC2 instance; please log in to your AWS console and select the instance you have created.

aws_ec2_instance

Click on the connect button that I noted on the above screen, you will see a screen for guiding how to connect to your Linux instance.

guide for connecting to your aws instance using ssh

I hope you can connect to your Linux Instance now with this command

ssh -i "yourkeyfile.pem" ubuntu@ec2-35-160-228-141.us-west-2.compute.amazonaws.com

but this is not the reason that I write this blog, you must configure to enable to connect your instance with this command

ssh ubuntu@ec2-35-160-228-141.us-west-2.compute.amazonaws.com
or
ssh yourdomain.com

To allow this to happen, we must follow these steps

  • Create public and private keys using ssh-keygen using your ssh client ( I use git bash as mentioned above), output will look like this
Generating public/private rsa key pair.
Enter file in which to save the key (~/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in ~/.ssh/id_rsa.
Your public key has been saved in ~/.ssh/id_rsa.pub.
The key fingerprint is:
0a:78:46:af:23:99:ac:b2:1e:ec:ef:c9:c9:b3:22:48 vagrant@vagrant-ubuntu-trusty-64
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|    .            |
|   o .           |
|  . + . S        |
|.E = o .         |
|oo= o .          |
|=o+oo.           |
|*++Oo            |
+-----------------+
  • Copying the public key that you have just created to the remote server
cat ~/.ssh/id_rsa.pub | ssh -i "yourkeyfile.pem" ubuntu@awspublicdns.com 'cat >> .ssh/authorized_keys'

After that, you may access your Linux instance with this simple command, you do not have to specify the critical file anymore

ssh ubuntu@ec2-35-160-228-141.us-west-2.compute.amazonaws.com

But it will be hard for you to remember the public DNS, you can config to change in the ssh config ~/.ssh/config

I am using Windows, I appended the file with these lines

Host yourdomain.com
	Hostname ec2-35-160-228-141.us-west-2.compute.amazonaws.com
	User ubuntu

After that, you can access your instance using this command

ssh yourdomain.com
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-36-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    https://www.ubuntu.com/business/services/cloud

58 packages can be updated.
20 updates are security updates.


Last login: Sun Oct 23 14:55:15 2016 from 203.205.35.160
ubuntu@ip-172-31-27-94:~$

Important Note

To secure your instance, Your default security group does not allow incoming SSH traffic by default and enables your IP only when needing access

security_group_inbound_rule

This blog is for me to remember the daily processes I go through. I hope it can help you guys save time by googling less. If you have any questions, please feel free to contact us.