This page looks best with JavaScript enabled

Port Forwarding and How to Do It With SSH

 ·   ·  ☕ 2 min read

Introduction

Started with my Pentium 4 processor desktop with merely 256 MB of RAM. And today I’m port forwarding sitting on my laptop in a cloud enabled web. Today we’ll see how I have manage to make a tunnel between my laptop and EC2 instance that I own.

In this short post, I will show how we can forward port from a remote host to a local computer. We will forward a port from AWS EC2 to local computer with openssl.

So let’s cut the shit and proceed ahead.

How I do port forwarding with ssh?

This is how I open a tunnel from my local computer port 5901 to my EC2 instance in AWS with public ip of xxx.xxx.xxx.xxx. This command is run from the local computer.

ssh -L 5901:localhost:5901 -i /path/to/key.pem ec2-user@xxx.xxx.xxx.xxx

I find it typically easier to use our very own ssh command which we already have in Linux and if you are using Windows, you will get it with installation of Git Bash.

So let’s break down the command:

  1. ssh - Our friendly ssh command.
  2. -L 5901:localhost:5901 - This tells the SSH to forward remote port 5901 to local port (thus localhost) 5901.
  3. -i /path/to/key.pem - This is to connect to remote host with a identity file. It is more secure than basic auth.
  4. ec2-user@xxx.xxx.xxx.xxx - This is my username and host IP.

So basically you can say that there is a tunnel between xxx.xxx.xxx.xxx:5901 and localhost:5901.

Anything running on xxx.xxx.xxx.xxx:5901 could also be viewed on your local computer. This too without opening any port on the remote host.

And on top of that. This connection is end to end encrypted SSL.

Conclusion

There are multiple many ways to forward port. What method do you use when you have to forward a port?

Share on

Santosh Kumar
WRITTEN BY
Santosh Kumar
Fullstack Developer at Method Studios