Create your own tunnelling like ngrok does
Tags: ssh, tunnelling, ngrok, forward
Published: 2020-02-19 18:36:48

---
title: Create your own tunnelling like ngrok does
published: true
description: A clear explanation of how ngrok works and how you can tunnelling your own server through public IP or subdomain.
tags: ssh,tunnelling,ngrok,forward
cover_image: https://blog.deeditt.com/bl-content/uploads/pages/9e06118b54d6d5899a9ec1da2119266d/aaron-burden-gmy25xvSkq8-unsplash.jpg
---

If you're here is because you don't want to use [ngrok](https://ngrok.com/) or you are looking for an alternative to it, which is pretty valid because if somebody did that, why you don't?

Well, this story begins with me trying to make my local development environment public and here is where [ngrok](https://ngrok.com/) came to the rescue, this tool works really well, but with some limitations, like having a random subdomain every time you restart your application or using more than one connection, are the things you will need to deal with unless you don't pay for it.

So basically everything works behind ssh tunnelling connections, and if you want to create your own [ngrok](https://ngrok.com/) clone, mainly you will need a VPS server running Linux preferentially.

Now try to connect your local computer to the VPS server through SSH (111.111.111.111 in this case).

```sh
$ ssh root@111.111.111.111
```

There are some different ways to get this done, every connection string depends on the service provider you're using, could need to type your password or could be using a .pem key but in the end, will have the same result.

With your VPS session opened, just type the next command in your terminal, if you have issues with this command try to open the [sshd_config](https://www.ssh.com/ssh/sshd_config) file and write at the very end "[GatewayPorts yes](https://www.ssh.com/ssh/tunneling/example)".

$ echo "GatewayPorts yes" >> /etc/ssh/sshd_config
By default, OpenSSH only allows connecting to remote forwarded ports from the server host. However, the GatewayPorts option in the server configuration file sshd_config can be used to control this.

Restart the SSH service and exit from your session

```sh
$ service ssh restart
$ exit
```

Now you should be able to run a command similar to this one, and guess what!!

```sh
$ ssh -R 8080:localhost:3000 root@111.111.111.111
```

Voilá, this will open the port 8080 on the VPS server and will forward it to the port 3000 on our local machine, basically means you can type on your browser http://111.111.111.111:8080 and effectively will redirect the traffic to your local computer.

I know you want more, say.. subdomains and https connections? yeah, why not, let's finish this.

The next part of this mini how-to is creating a subdomain and link it with your public VPS IP, but I'm will leave this to you, again .. this depends on what service provider you're using.

At this point, you have your VPS server running and ready to forward from source to destination ports, let's assume you have your subdomain created as well, now the next step will be setting up your web server with a reverse proxy tweak. In my case I decided to use Nginx, it's my favourite and it's really simple to set up, actually, my current configuration is similar to this:

```bash
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	server_name _;
	location / {
		proxy_pass http://127.0.0.1:8080;
	}
}
```

And again, works! my VPS server receives traffic from my subdomain and redirects to 127.0.0.1:8080 which is the same server but through SSH tunnelling.

If we test again typing in the browser address bar http://yoursubdomain.domain.com should work and redirect again all petitions to your local computer.

This still needs to work with SSL and to complete this step I recommend you to install [certbot](https://certbot.eff.org/) and follow the instructions, it's pretty straight forward so I don't have a doubt you will get it.

This is my first article as a non-native English-speaker, so be kind and patient.

Happy Coding! :D