Access to local network from external network without open ports | Ngrok

Gonzalo Galante
3 min readAug 22, 2022

--

In my next article I will write about how to build a server on a raspberry, but first I wanted to make a special mention of how we can access the functions that this server will have from outside our local network.

For this, the ports of our local network are often opened, which is relatively simple (unless the company that provides you internet does not give you access, as in my case).

The bad thing about this solution is that we are vulnerable to an external attack.

As an alternative, I bring a solution that is not perfect and is not for those looking to put a server into production but for personal use, for my part I use it to access certain functions of several microservices that I have mounted in Flask inside a Raspberry.

Ngrok

This great tool enables us to create a bridge between an external network and our local network in just a few steps.

To implement it, all we have to do is go to the link that I leave below.

https://ngrok.com/

You must create an account and go to the “downloads” section, select depending on the OS you have, in my case Linux.

Once downloaded in the folder where you have the file, use the following command

sudo tar xvzf ~/Downloads/ngrok-v3-stable-linux-amd64.tgz -C /usr/local/bin

Finished the installation on the web, in the section “Your Authtoken” will give them a command which is used so that when executing the authentication token is established.

And that’s all the configuration needed to start using it.

As an example, assuming that we have a microservice with Django running on port 5000, in a separate terminal we run the following command.

Ngrok http 8000

What this will do is leave us in the terminal running ngrok with a link similar to this “https://2dv4-181-245-0-131.sa.ngrok.io/” where if we click it will send us to a website like this.

In which when clicking on “Visit Site” it will direct us to the endpoint of our microservice hosted on port 8000.

Then through the different http requests we can use our functionalities with the difference that instead of using localhost:8000/funcion we will use the link from ngrok/function.

Run in background

If we do not want to leave the Ngrok service running in the terminal and therefore without being able to use it, we can use this command.

ngrok http 5000 --log=stdout > ngrok.log &

The port is 5000 but you can put whatever you want and what it does is the service runs in the background and the logs are stored in a file called ngrok.log to which With a “cat ngrok.log” we can see the logs, among which are the link to access, in addition to all the requests that are received.

--

--