Setting up Port Forwarding and DNS

Aziz Fikri Mahmudi
6 min readJun 14, 2023

--

Back to the “Setup a private server without having a static public IP” series! Make sure to read the previous story by clicking the link below,

Okay! Let’s dive into today’s topic!

Image by fullvector on Freepik

Setting up Port Forwarding and DNS

Today I will cover 2 important parts of this entire process,

  1. Make sure that you can forward your port via your router
  2. Buy a domain and set it on your favorite DNS provider

Before going to set your router to forward your port, we must know first what is “Port Forwarding”.

Port Forwarding

Port Forwarding is a technique used in computer networking to forward traffic from a port in one device into a port in another device. By doing this, we can expose our private computer to receive traffics from the public internet via our public IP address.

Port Forwarding is one of the important aspects to make this entire project work. Because of that, we need to check whether our router can handle port forwarding or not. You can just simply go to the router setting to make sure that the router can handle port forwarding like the image below,

Port Forwarding setting

If you see the above image, I can find the “Port Forwarding” setting in the Internet tab within the Security section. Of course, every device has its own firmware and its own location on where they put the “Port Forwarding” setting. If you don’t find the “Port Forwarding” setting in the same place that I found it, just try to google it and make sure that your router can handle “Port Forwarding”.

Setup Our First Port Forwarding

To make sure that Port Forwarding is working on our device, we should test it first. If the port forwarding is not working, you can try to solve the problem by asking to Chat-GPT or reading articles on Google. Good Luck!

We will use Linux in this story

Check your device's local IP.

To check your device’s local IP you can simply run hostname -I and you’ll find a bunch of IPs like this,

~ >>> hostname -I
192.168.1.30 172.17.0.1 172.18.0.1 2001:448a:404c:1bc7:aad9:c73b:5630:ee0a 2001:448a:404c:1bc7:5364:cf8:12e4:5c65 2001:448a:404c:1bc7:e63d:c131:3e5f:730d

The first one on the list is your private IP address, you can use it when you try to forward your local port.

Forward a port from the device

After we know what our device’s local IP is, we can set up a Port Forwarding right away.

Go to the control panel of your router, it is usually hosted at this address 192.168.1.1

After logging in, you can just go to the Port Forwarding setting and choose “Create New Item” with this configuration,

Usually, each device has its own fields, but what you need to know is how we configure what port that will be opened on our router and who will receive the traffic. If you take a look at the above image, we only need to set the,

  1. LAN Host → which stands for Local Area Network Host. We will fill our device IP to that field
  2. WAN Port → which stands for Wide Area Network Port. We will fill it with 80 right now, but it is not limited to 80
  3. LAN Host Port → which stands for Local Area Network Host Port. We will fill it with 80 right now.

After filling in all necessary fields shown by the setting, we can give it a name and hit “Apply”.

Yay! We now have a public port ready for connection!

Wait! We still have to configure something else!

As you guys know, we only forward traffic from port 80 on our router to port 80 on our (to-be) server. We haven’t set up a running application at port 80 on our server.

To keep things simple, let’s just listen to any incoming TCP connection on port 80 by running this command,

~ >>> sudo netcat -l -p 80

After running that, try to open your public IP Address from your phone.

Oh, I haven’t told you how to check your public IP Address huh? Okay, let me tell you.

You guys can check your public IP Address by opening this URL in your browser,

https://api4.my-ip.io/ip.txt

Now you know what your public IP Address is, let’s open it from our mobile phone! Make sure to not use the same network as your server is, because it’ll never work!

Hmm… The site keeps loading and there’s no end to it…

Wait! Don’t be so pessimistic, try to take a look at your server’s terminal

If you see at your terminal, there’s some random text shown. The text below is what you send from your mobile phone’s browser to the server and that is a sign that it’s worked!

~ >>> sudo netcat -l -p 80
GET / HTTP/1.1
Host: 36.73.50.81
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9

Register our IP to our domain

After successfully setting your port forwarding, now is the time to buy your own domain and set it up on your private DNS Provider!

ps: This project will use Cloudflare since I’m used to it. :)

Register a new domain name by following the steps provided by Cloudflare

Followed by registering your site to the Cloudflare

After successfully registering your site, you can go to the DNS menu and add your new record like this,

use @ to point it to your root subdomain and put your public IP Address in the IPv4 address field. I preferred to leave the proxy on because it’ll hide my true IP from people who try accessing my website.

After clicking save, let’s try that thing once again!

  1. Listen to TCP on port 443 (if the proxy is on, Cloudflare will redirect us to https instead of http)
  2. Opening our URL in the browser

Once you open the URL, let’s try to check it again on our server.

~ >>> sudo netcat -l -p 443                                                                                                                                                                                                             [130]
��؉�v ���4��-+�5O<��G�BI>i�� ?�fזè�0����,㋧�0����Ύ�~��+�,�/�0��(��/5

afikrim.net�


#
3&$ �48�i�Ͷ`45������68mt^Z��C-+�

If you get something like this, then it means that you have successfully set up a working server!

Yay! Finally a working server!

Don’t let the hype make you forget a thing. Yes, your IP is not static, it can be changed at any moment. To make your private server work peacefully, make sure to read the next story that I’ll post again!

Let’s end it here! Thanks for taking the time to read this story, have a good day!

--

--

Aziz Fikri Mahmudi

Passionate Backend Engineer | Backend Engineer at Amartha