UPNP logo

Open router’s ports dynamically with UPNP

VirgilPosted by

I have a development server at home who is not available from outside of my network. I wanted to show something to a friend without modifying my router’s configuration.
The easiest way to do that is to open the port with an UPNP command.

I’m running on Ubuntu Focal but it should work on Debian too. We will install miniupnpc to open the port.

sudo apt update
sudo apt install miniupnpc -y

My local IP is 192.168.42.9 and I will use the port 4443 remotely to stay away from HTTPS scanners.

upnpc -a 192.168.42.9 443 4443 tcp

upnpc : miniupnpc library test client, version 2.1.
 (c) 2005-2019 Thomas Bernard.
Go to http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
for more information.
List of UPNP devices found on the network :
 desc: http://192.168.42.1:5000/Public_UPNP_gatedesc.xml
 st: urn:schemas-upnp-org:device:InternetGatewayDevice:1

Found valid IGD : http://192.168.42.1:5000/Public_UPNP_C3
Local LAN ip address : 192.168.42.9
ExternalIPAddress = XX.XX.XX.XX
InternalIP:Port = 192.168.42.9:443
external XX.XX.XX.XX:4443 TCP is redirected to internal 192.168.42.9:443 (duration=0)

The port is now open. If you want to get the redirection on startup, you can add a Systemd service to create it during the boot process.
To keep the port open you should add a cron in case of a router reboot.
In a file, in /etc/cron.d/, don’t forget to adapt it to your local IP and the wanted ports.

*/5 * * * * root upnpc -a 192.168.42.9 443 4443 tcp

Remove the redirection when you don’t need it anymore.

upnpc -d 4443 tcp

Leave a Reply

Your email address will not be published. Required fields are marked *