Expose a local service on the Internet from Ubuntu: Complete guide to port forwarding
Published on 28 May 2025
- The problem: NAT and private addresses
- Step 1: Identify your public IP address
- Step 2: Determine the local IP address of your Ubuntu machine.
- Step 3: Configure port forwarding on your box
- Step 4: Setting up the service on Ubuntu
- Step 5: setting up the Ubuntu firewall
- Step 6: Test and validation
- Best practices and security
- Alternatives to port forwarding
- Troubleshooting
- Conclusion
-
Assumed prerequisites: basic knowledge of Ubuntu, the terminal, basic networking concepts
-
Technical concepts: NAT, port forwarding, firewall, reverse proxy
-
Commands system: use of the terminal, network configuration
-
Advanced sections: monitoring, security, cloud alternatives
The problem: NAT and private addresses
By default, your internet router uses NAT (Network Address Translation) to share a single public IP address among all devices in your home network. Your local machines use private IP addresses (192.168.x.x, 10.x.x.x, etc.) that are not routable on the Internet.
When you launch a service on your Ubuntu machine (for example a web server on port 8080), it is only accessible from your local network. To make it accessible from the Internet, we must configure a port forward.
Step 1: Identify your public IP address
First of all, let’s retrieve your public IP address from your Internet connection:
# Plusieurs méthodes au choix
curl ifconfig.me
curl ipecho.net/plain
wget -qO- http://ipecho.net/plain
# Ou encore
curl -s checkip.amazonaws.com
This IP address is the one that internet users will use to access your service.
Step 2: Determine the local IP address of your Ubuntu machine.
Your Ubuntu machine has a private IP address on your local network. To find it:
# Afficher toutes les interfaces réseau
ip addr show
# Ou plus spécifiquement pour l'interface principale
ip route get 1.1.1.1 | awk '{print $7; exit}'
# Alternative avec hostname
hostname -I | awk '{print $1}'
Write down this address (for example`192.168.1.100`), we will need them for the configuration.
Step 3: Configure port forwarding on your box
The configuration varies depending on the model of your box, but the principle remains the same:
Access to the administration interface
Open your browser and go to the web interface of your box :
-
Freebox:`192.168.1.1` ou
mafreebox.freebox.fr -
Livebox Orange(Empty)
192.168.1.1 -
SFR Box(empty)
192.168.1.1 -
Bbox Bouygues:`192.168.1.254`
Redirect configuration
Look for the section dedicated to port forwarding (names vary):
-
Port forwarding
-
NAT/PAT
-
Port Forwarding
-
Game servers
Create a new rule with these parameters:
| Field | Value |
|---|---|
Name/Description |
Ubuntu Web Service" (or any other explicit name) |
External port (or public port) |
8080 |
Internal IP address |
Your Ubuntu machine’s IP address (e.g., 192.168.1.100). |
Internal port (or private port) |
8080 |
Protocol |
TCP |
State |
Enabled |
Step 4: Setting up the service on Ubuntu
Make sure your service listens on all interfaces, not just localhost:
# ✅ Correct : écoute sur toutes les interfaces
python3 -m http.server 8080 --bind 0.0.0.0
# ❌ Incorrect : écoute seulement en local
python3 -m http.server 8080 --bind 127.0.0.1
To verify that your service is listening correctly:
# Vérifier les ports en écoute
sudo netstat -tlnp | grep :8080
# ou avec ss (plus moderne)
sudo ss -tlnp | grep :8080
You should see something like`0.0.0.0:8080`and not`127.0.0.1:8080`.
Step 5: setting up the Ubuntu firewall
Ubuntu uses UFW (Uncomplicated Firewall) by default. Check its status and allow the port if necessary:
# Vérifier le statut du pare-feu
sudo ufw status
# Si le pare-feu est actif, autoriser le port 8080
sudo ufw allow 8080/tcp
# Ou plus spécifiquement pour un service web
sudo ufw allow 'Apache' # si vous utilisez Apache
sudo ufw allow 'Nginx Full' # si vous utilisez Nginx
Step 6: Test and validation
Test from the local network
First, test access from another computer on your local network:
# Remplacez par l'IP locale de votre machine Ubuntu
curl http://192.168.1.100:8080
Test from outside
Now, test from the Internet using your public IP:
# Remplacez par votre IP publique
curl http://VOTRE_IP_PUBLIQUE:8080
You can also use online tools like:
Best practices and security
Basic security
# Configurer fail2ban pour protéger contre les attaques par force brute
sudo apt update && sudo apt install fail2ban
# Limiter l'accès avec UFW (exemple : autoriser seulement certaines IP)
sudo ufw allow from 203.0.113.0/24 to any port 8080
Connection monitoring
# Surveiller les connexions en temps réel
sudo netstat -an | grep :8080
# Logs des connexions (selon votre application)
sudo tail -f /var/log/nginx/access.log # pour Nginx
sudo journalctl -f -u your-service # pour un service systemd
Using a reverse proxy
For better security and flexibility, consider using a reverse proxy like Nginx:
server {
listen 80;
server_name votre-domaine.com;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Alternatives to port forwarding
Cloud solutions
-
ngrokSecure temporary tunnel
-
Cloudflare Tunnelfree and secure solution
-
serveo.netsimple SSH tunnel
# Exemple avec ngrok
ngrok http 8080
# Exemple avec serveo
ssh -R 80:localhost:8080 serveo.net
VPS and reverse proxy
For professional use, consider using a VPS with a reverse proxy pointing to your local infrastructure via VPN.
Troubleshooting
Common problems
| Problem | Solution |
|---|---|
Service inaccessible from the outside |
Check the port forwarding configuration and that the service is listening on 0.0.0.0 |
Connection refused |
The service is not started or is listening only on 127.0.0.1 |
Connection timeout |
Firewall problem (router box or Ubuntu) or misconfigured port forwarding |
Public IP changes regularly |
Set up a dynamic DNS (DynDNS, No-IP, etc.) |
Network debugging
# Tester la connectivité locale
telnet localhost 8080
# Tester depuis une autre machine du réseau
telnet 192.168.1.100 8080
# Vérifier les routes réseau
ip route show
# Analyser le trafic réseau
sudo tcpdump -i any port 8080
Conclusion
Exposing a local service on the Internet requires careful configuration of port forwarding, security, and monitoring. Although this approach is perfect for development or personal projects, consider more robust alternatives (VPS, CDN, cloud services) for production use.
The important thing is to always keep security in mind: use strong passwords, regularly update your system, and monitor access to your exposed services.
Feel free to share your experiences and questions in the comments!