How to Connect AWS EC2 Linux and Azure Virtual Machine Instances 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 the AWS EC2 Linux Instance?

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 how to connect

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.

edit inbound rules

How to Connect Your Azure Virtual Machine

Ensure Network Security Group (NSG) Rules Are Configured

Step 1: Locate the Public IP Address

  1. Sign in to Azure Portal: Go to Azure Portal.
  2. Navigate to Virtual Machines and select your VM.
  3. In the Overview section, you’ll find the Public IP address under the “Essentials” section. Note this address.

Step 2: Ensure Network Security Group (NSG) Rules Are Configured

  1. Go to Networking: In your VM’s settings, navigate to Networking.
  2. Inbound Port Rules: Ensure the following rules exist:
    • Port 80 (HTTP) for accessing your Magento storefront.
    • Port 443 (HTTPS) if you’ve set up SSL/TLS.
    • Port 22 (SSH) for remote access.
image

Accessing VM Through Public IP

Open your browser and visit: https://<public-ip-address>

Accessing SSH Terminal For VM Instance

Step 1: If you are on a Mac or Linux machine, open a Bash prompt and set read-only permission on the .pem file using chmod 400 ~/<your_folder_path>/myKey.pem. If you are on a Windows machine, open a PowerShell prompt.

Step 2: Go to your Virtual Machine on Azure Portal, click Connect > SSH

AD 4nXfvQ3wTVr40kKd9Wo5Dn3dz3vETqHsiMgbGnx2cB 32YvAnxmrYKQLRpKWfs7VaCmXWzVMn85dVNRTXjh6f5tc3rEs N9Hj8b09aSCWmKlLF0Ylyu2tQh0Zd7dVq5eqTQCwfOmv A?key=g3rNWJCz60zkbk2GNO1BGigI

Step 3: Copy and run the SSH Command:
ssh -i ~/<your_folder_path>/myKey.pem azureuser@10.111.12.123

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.