Raspberry Pi Port Forwarding Setup – Beginner Guide
⚠ Security Notice: Port forwarding opens direct connections from the internet to your home network – every open port is a potential attack target. If you only want to reach your Raspberry Pi remotely, Tailscale VPN is much safer: no open ports in your router, end-to-end encrypted, and free.
With port forwarding (also called port mapping), you redirect incoming internet connections directly to your Raspberry Pi. This lets you access the Pi via SSH from anywhere, host a self-managed website, or expose other services to the internet.
Prerequisites
- DynDNS address set up (→ Part 5: DynDNS)
- Raspberry Pi with a static IP address in your home network
- Access to your router’s admin interface
Step 1: Reserve a Static IP for the Pi
To make sure the router always knows where to forward packets, the Pi should always get the same local IP address. There are two ways to do this:
Option A: DHCP Reservation in the Router (Recommended)
The easiest method: assign a fixed IP to the Pi’s MAC address in the router settings. Get the Pi’s MAC address with:
ip link show eth0
On a FritzBox: Home Network → Network → click “Edit” next to the Pi → check “Always assign this network device the same IPv4 address”
Option B: Static IP on the Pi (Raspberry Pi OS)
Since the October 2023 release, Raspberry Pi OS manages networking with NetworkManager. The commands below use example values, so adjust them to your network:
- 192.168.1.42: the fixed address for the Pi. Pick a free address in your network (same first three numbers as your router) that sits outside the range your router hands out via DHCP.
- 192.168.1.1: your router’s IP. You’ll find it after “default via” in the output of
ip route. - Wired connection 1: the name of the network connection.
nmcli con showlists the actual name.
Then set a static IP with:
sudo nmcli con mod "Wired connection 1" ipv4.addresses 192.168.1.42/24
sudo nmcli con mod "Wired connection 1" ipv4.gateway 192.168.1.1
sudo nmcli con mod "Wired connection 1" ipv4.dns "1.1.1.1 8.8.8.8"
sudo nmcli con mod "Wired connection 1" ipv4.method manual
sudo nmcli con up "Wired connection 1"
If the Pi ends up with a different IP than before, any open SSH session will drop. Just reconnect using the new address.
Step 2: Configure Port Forwarding in Your Router
The external port (the one reachable from the internet) doesn’t have to match the internal port on the Pi. That’s exactly what you want for SSH: the Pi keeps listening on port 22, but the router exposes a different port to the outside, such as 2222.
FritzBox
- Open the router interface: fritz.box in your browser. As of FRITZ!OS 8.50, AVM recommends
http://fritzbox.localinstead, butfritz.boxstill works on your home network. - Internet → Permit Access → Port Sharing
- Click Add Device for Sharing
- Select the Raspberry Pi from the list
- New Sharing → Port Sharing → “Other application”, protocol TCP. Enter the port on the Pi in “Port to device” and “through port” (SSH: 22), and the port you want to use from the internet in “Port requested externally (IPv4)” (SSH: e.g. 2222)
- Save
Other Routers
On other routers, port forwarding is usually found under Advanced → NAT → Port Forwarding or similar. The key settings are:
- External Port: The port to be reachable from the internet
- Internal IP: IP address of the Raspberry Pi
- Internal Port: The port the service listens on (SSH: 22, HTTPS: 443)
- Protocol: TCP (for SSH, HTTP, HTTPS)
Common Ports
| Service | Port on the Pi | External Port | Protocol |
|---|---|---|---|
| SSH | 22 | e.g. 2222 (not 22) | TCP |
| HTTP (web server, Let’s Encrypt validation) | 80 | 80 | TCP |
| HTTPS (web server) | 443 | 443 | TCP |
| Nextcloud | 443 (plus 80 for the certificate) | 443 and 80 | TCP |
Don’t forward VNC (port 5900) or Pi-hole (DNS port 53 and the web interface). Neither is meant to sit open on the internet: an exposed VNC login quickly attracts password-guessing attacks, and an open DNS port gets abused to attack other people. To reach either one on the go, use Tailscale (see below).
Security: Protect Your Pi from Attacks
Opening ports means you need to harden your Pi. Minimum requirements:
- Don’t expose port 22: In the router, forward a different external port (e.g. 2222) to port 22 on the Pi. That keeps most automated bot scans away, and SSH on the Pi stays on port 22
- SSH keys instead of passwords: Completely disable password login for SSH
- Fail2Ban: Automatically block IPs after multiple failed login attempts
- UFW Firewall: Keep only the ports you actually need open
Our full guide to securing the Pi – including SSH keys, UFW, Fail2Ban, and Tailscale as a port forwarding alternative – is here: Raspberry Pi Security Guide.
Alternative: Tailscale Instead of Port Forwarding
Port forwarding is the classic approach – but not the safest. Tailscale is a free WireGuard VPN that lets your own devices reach the Pi from anywhere without opening a single router port:
curl -fsSL https://tailscale.com/install.sh | sh
sudo tailscale up
sudo tailscale up prints a login URL. Open it in your browser and sign in to Tailscale – the account is free for personal use. Then install Tailscale on the device you want to connect from (phone or laptop) and sign in with the same account.
After setup, your Pi gets a fixed IP in the 100.x.x.x range (tailscale ip -4 shows it) – no DynDNS, no port forwarding, no attack surface from open ports. Ideal for SSH access, Nextcloud, and other private services.
Test the Connection
After setup, test from a different network (e.g. mobile data). The router translates external port 2222 to port 22 on the Pi:
ssh -p 2222 user@yourdomain.duckdns.org
Port Forwarding FAQ
Port forwarding opens a direct path through your router to one specific service on the Pi. It’s easy to set up, but every open port is a potential attack target. A VPN such as Tailscale or WireGuard creates an encrypted tunnel without any open ports. For SSH access to your own Pi, Tailscale is both safer and simpler.
SSH runs on port 22 on the Pi, and you can leave it there. In the router, expose a different external port (e.g. 2222) and forward it to port 22 on the Pi. You then connect with ssh -p 2222 user@your-domain. Always use SSH keys instead of a password and enable Fail2Ban.
With DS-Lite (common on cable and some fiber connections) and carrier-grade NAT (common on LTE/5G), your router doesn’t get its own public IPv4 address – many customers share one. An IPv4 port forward on your router never receives any traffic. Your options: Tailscale, which works without any port forwarding, an IPv6 port forward (only works if the network you’re connecting from also has IPv6), or asking your provider for a real public IPv4 address.
It works the same way as SSH: forward port 443 (HTTPS) to port 443 on the Pi’s local IP, plus port 80 – Let’s Encrypt uses it to confirm you own the domain when Certbot issues or renews the SSL certificate. You’ll also need a domain name, for example from DuckDNS. Nextcloud is then available at https://yourdomain.duckdns.org.
Conclusion
Port forwarding is a proven way to make your Raspberry Pi reachable from outside – but every open port is a potential attack target. For SSH access and private services, Tailscale is the safer and easier option: no open router ports, end-to-end encryption, and it’s free. For public websites (ports 80/443 with HTTPS), port forwarding is still the right choice – combined with Let’s Encrypt, Fail2Ban, and SSH keys.
