SSH Port Forwarding Explained: Access Your Homelab from Anywhere Safely
Your homelab runs services on your home network: a dashboard, a file server, a media app, a test site. From your couch they load fine. From a hotel or a coffee shop they are unreachable, because your router hides them behind NAT. SSH port forwarding homelab setups solve this without opening your home network to the whole internet. A single SSH connection carries traffic for another service through an encrypted tunnel, so you reach it as if you were at home.
This guide covers the three kinds of forwarding, the exact commands, and the security rules that keep a handy tunnel from becoming an open door. If you have not set up SSH yet, start with our walkthrough on setting up SSH keys on a new VPS.
SSH Port Forwarding Homelab Basics: How a Tunnel Works
SSH port forwarding sends a connection meant for one port through your SSH session to a different port somewhere else. The SSH server acts as a relay: traffic between your laptop and the server is encrypted, and the final hop travels on the network where the service lives.
Your laptop asks the server for a port, the server passes the request through the tunnel to the service, and the answer travels back the same way.
Three flags control the three kinds of ssh port forwarding homelab owners use most. The -L flag forwards a local port to a remote service. The -R flag forwards a remote port back to a local service. The -D flag opens a SOCKS proxy that forwards traffic on demand.
SSH Local Port Forwarding (-L): Reach Services Behind Your Router
Local forwarding is the one you will use most. It takes a port on your own machine and connects it to a service that only the SSH server can reach, for example the web dashboard on your home server.
The syntax reads left to right:
ssh -L [local-port]:[target-host]:[target-port] [user]@[ssh-server]
Suppose your home server runs a dashboard on port 80 at 192.168.1.50, and you SSH into your VPS at 203.0.113.10. This command maps your laptop’s port 8080 to that dashboard:
ssh -L 8080:192.168.1.50:80 [email protected]
While the session is open, visit http://localhost:8080 and the dashboard loads. Close the SSH session and the tunnel closes with it.
Add -N when you only want the tunnel and no shell, and -f to send SSH to the background:
ssh -N -f -L 8080:192.168.1.50:80 [email protected]
Use the same pattern for a database on port 5432, a monitoring page on port 3000, or a NAS interface on port 5000: pick a free local port, point it at the service, and connect through localhost.
SSH Remote Port Forwarding (-R): Expose a Homelab Service to the Internet
Remote forwarding works in the opposite direction. It takes a port on the SSH server and sends traffic back through the tunnel to a service on your local machine, so you can share a homelab service or reach your home server through your VPS.
The syntax mirrors local forwarding, with the direction flipped:
ssh -R [remote-port]:[target-host]:[target-port] [user]@[ssh-server]
Say you run a small web app on your laptop on port 3000 and want it reachable through your VPS:
ssh -R 8080:localhost:3000 [email protected]
Anyone who visits http://203.0.113.10:8080 now reaches the app on your laptop, routed through the tunnel. By default the remote port only listens on the server’s loopback address. To listen on all interfaces you need GatewayPorts yes in sshd_config, and you should only turn that on when the service has its own authentication, because it becomes reachable from the whole internet.
Remote forwarding is a quick way to show a friend a project or test a webhook against a local server. It is not a replacement for a proper deployment: the tunnel dies when your SSH session drops.
SSH Dynamic Port Forwarding (-D): Turn SSH into a SOCKS Proxy
Dynamic forwarding does not map one port to one service. It opens a SOCKS proxy on your machine, and each connection through the proxy decides its own destination. This is useful when you need to reach several services at once, or when you want your browsing to exit from the server’s network.
ssh -D 1080 [email protected]
Point your browser or app at a SOCKS5 proxy on localhost:1080 and traffic flows through the tunnel. A request to an internal dashboard resolves on the server side, so hostnames that only exist on the server’s network start working. Firefox lets you set a SOCKS proxy per browser; Chrome and most desktop apps follow the system proxy settings.
The proxy only covers traffic from apps you point at it. It does not protect your whole machine, and it does not hide your traffic from the SSH server itself.
Local vs Remote vs Dynamic Forwarding at a Glance
| Type | Flag | Direction | Best for | Main risk |
|---|---|---|---|---|
| Local | -L | Your port to a remote service | Opening a private dashboard or database from your laptop | Low; the port only listens on your machine |
| Remote | -R | Server port back to your machine | Sharing a local app through your VPS | High with GatewayPorts; the service faces the internet |
| Dynamic | -D | SOCKS proxy, any destination | Reaching several internal services at once | Medium; apps must be set to use the proxy |
When in doubt, start with local forwarding. In most ssh port forwarding homelab setups it exposes the least and covers most needs.
Making Tunnels Persistent with the SSH Config File
Typing long forwarding commands every day gets old fast. The SSH config file on your laptop, ~/.ssh/config, stores the tunnel as a named host so one short command opens it:
Host homelab-tunnel
HostName 203.0.113.10
User admin
IdentityFile ~/.ssh/id_ed25519
LocalForward 8080 192.168.1.50:80
LocalForward 5433 192.168.1.50:5432
ServerAliveInterval 60
Now ssh homelab-tunnel opens both tunnels at once, and ServerAliveInterval sends a keepalive every minute so idle tunnels do not die silently behind NAT. Keep the config file readable only by you:
chmod 600 ~/.ssh/config
For tunnels that must survive reboots, a tool like autossh watches the connection and restarts it when it drops. A plain SSH command is enough for one-off work.
Security Rules Before You Forward Anything
A tunnel is only as safe as the SSH server behind it, and every ssh port forwarding homelab guide should start with that warning. Password login should already be off. If it is not, follow our guide on disabling password login and using SSH keys only, then work through the rest of the ten SSH hardening changes for Ubuntu 24.04. Key-only login plus fail2ban removes most of the automated login attempts a public SSH port attracts.
A few rules apply to the tunnels themselves:
- Bind forwarded ports to 127.0.0.1 unless you have a reason not to. A port listening only on loopback cannot be reached from other machines on your network.
- Never forward a database or admin panel with GatewayPorts on unless the service has strong authentication of its own.
- Forward the smallest set of ports you need, and close the tunnel when you finish. A tunnel left running for weeks is an open path you forgot about.
- Use a separate, limited user for tunnels where you can. That user needs no shell and no sudo, only the right to forward ports.
- Check what is listening.
ss -tlnpon the server shows every listening port, including ones your tunnels opened.
When Port Forwarding Is the Wrong Tool
SSH tunnels are quick but manual: one command, one session, one thing to remember. When remote access becomes a daily need, a VPN fits better. WireGuard gives every device on your home network a private address you can reach directly, with no tunnel per service. Our guide on setting up WireGuard to access your homelab remotely walks through the full install, and the Tailscale vs WireGuard comparison for remote homelab access helps you pick between a self-hosted VPN and a managed mesh.
A reverse proxy fits when the goal is sharing web services rather than reaching private ones. If you want stable public URLs with HTTPS for your homelab apps, compare the three common choices in Cloudflare Tunnel vs Nginx Proxy Manager vs Traefik. A Cloudflare Tunnel can publish a service without opening any inbound port at all, which removes the risk remote forwarding carries.
FAQ
Is SSH port forwarding safe?
Yes, when the SSH server is hardened: key-only login, an updated server, forwarded ports bound to localhost, and no GatewayPorts unless the exposed service has its own login.
Why does my tunnel close after a few minutes?
Idle connections get dropped by NAT devices and firewalls. Add ServerAliveInterval 60 to your SSH config or pass -o ServerAliveInterval=60 on the command line, and the keepalive packets hold the session open.
Can I forward more than one port in a single command?
Yes. Repeat the flag for each port, for example ssh -L 8080:192.168.1.50:80 -L 5433:192.168.1.50:5432 [email protected]. The SSH config file is cleaner once you pass two or three forwards.