Connect to ec2-server which has bastion host

Step by step guide to connecting to ec2-instance which is using bastion host as a public sever(contains public IP) and another ec2-instance as a private server(contains private IP)

 Why use Bastion Host?

It is more secure to hide your server behind a bastion host as you can not create a direct connection to the server. You must create an ssh connection with a private key file(.pem file) to the bastion host server after that you can start RDP session with servers connected to this bastion host.

Prerequisites

For Mac and Linux Users

1. Terminal with ssh command enabled

2. Microsoft Remote Desktop app if connecting to the window server

For Windows users

Gitbash with ssh command enabled if not installed, install from below link

https://gitforwindows.org/

Steps to connect to Server

Create an ssh connection with Bastion Host

Open your terminal and paste below command to create an ssh connection with Bastion Host. You have to replace variables with ports and IPs of the Bastion host server and actual server.

ssh -L port1:ec2-instance-private-IP:port2 -i ~/path-to-pem-file/file-name.pem bastion-username@bastion-host-IP

The description for variables used in the above command is as below

port1

You can assign port1 any value that you want. We will use it while creating an RDP connection with our server. For example, we can assign 33381 value to it.

instance-private-IP

This is the IP address of your ec2-instance which is behind the bastion host. You can replace it with your server Private IP. For example, we can assign 0.0.0.0 value to it.

port2

You can assign port2 any value that you want. For example, we can assign 3388 value to it.

path-to-pem-file

Replace this path with the actual path where your private key file exists. If it exists inside a folder name as doc the path will be - ~/doc/file-name.pem

bastion-username

Replace this with your server username. For example, ec2-user

bastion-host-IP

Replace this with the public IP of Bastion host. For example, we can assign 1.1.1.1 value to it.

Final command based on above-assigned values will be

ssh -L 33381:0.0.0.0:3388 -i ~/doc/file-name.pem [email protected]

A warning may be prompted inside the terminal

WARNING: UNPROTECTED PRIVATE KEY FILE!

To fix this on MAC or Linux run below command inside your terminal

sudo chmod 600 /path/to/my/key.pem

After this, run ssh command again and connection will be made.

Create RDP connection with the server

After successfully connected via ssh we can initiate a session with our server.

For MAC Users

Open Microsoft Remote Desktop app and enter credentials inside it

PC name - 127.0.0.1:port1(for example we have assigned 33381 value to it)

Username - Username of your server(For example, Admin)

Password - Username of your server(For example, AdminPass)

For Windows Users

Open RDP connector by running 'mstsc' inside run prompt(windows+R) and enter credentials inside it

Hostname/PC name - 127.0.0.1:port1(for example we have assigned 33381 value to it)

Username - Username of your server(For example, Admin)

Password - Username of your server(For example, AdminPass)

And you are good to go! Thanks for reading.

0 Comments
Write new article
Never leave your website again in search of code snippets by installing our chrome extension.