23 Nov 2026 · 5 min read · Tutorial
How to forward a remote web port over SSH, with and without a shell
SSH local forwarding opens a port on your machine and sends everything that connects to it, over the encrypted connection, to a host and port that only the r…
SSH local forwarding opens a port on your machine and sends everything that connects to it, over the encrypted connection, to a host and port that only the remote machine can reach. It is the standard way to open a web UI that sits behind a jump host or inside a private network, and it needs no VPN, firewall change, or route.
This guide covers the command, the two ways to run it. In the foreground with a shell, or detached and non-interactive with -N and the caveats that decide whether the page actually loads.
What the command means
ssh -N -L [bind_address:]local_port:target_host:target_port user@remote_hostpart | meaning |
|---|---|
| the port opened on your machine, e.g. |
| what to connect to from the remote host, e.g. |
| the SSH server you log in to, e.g. |
| local forward: listen locally, connect remotely |
| do not open a session or run a command - forwarding only |
The direction is the part most people get wrong. A "reverse web port" usually means the opposite: a port published on the server that points back to your machine. That is -R, and it is covered near the end.
flag | where the port listens | who connects to the target |
|---|---|---|
| your machine | the SSH server |
| the SSH server | your machine |
With -L, the target name is resolved and dialled by the remote host, not by your laptop. notes.internal works when it only exists in the remote network, and localhost in a target means the remote machine's loopback, not yours.
-N is not a non-interactive shell mode; it is the absence of a session. No shell is started at all, which is why the flag is used for tunnels that should not depend on a login shell, a terminal, or a shell profile.
Before you start
An SSH client and a login that works without an interactive prompt, preferably a key. For anything automated, use
-o BatchMode=yesso a password prompt fails instead of hanging.The SSH server must allow forwarding (
AllowTcpForwarding yes, the default). On hardened servers, forwarding is often disabled or restricted withPermitOpen; checksshd_configon the remote side if the tunnel connects but the port never appears.The target must be reachable from the remote host. Verify before forwarding:
ssh user@remote-ip 'curl -sS -o /dev/null -w "%{http_code}\n" http://10.0.0.5:80/'The local port must be free:
ss -ltn | grep ':8080' || echo freeStep 1 : Open the tunnel in the foreground
ssh -N -L 8080:10.0.0.5:80 user@remote-ipThe command stays in the foreground with no output. Verify it from another terminal:
curl -sS -D- http://127.0.0.1:8080/ | head -20Then open http://127.0.0.1:8080 in a browser. Stop the tunnel with Ctrl-C; the local port closes with it.
If you also want a shell on the remote machine, drop -N:
ssh -L 8080:10.0.0.5:80 user@remote-ipYou get a prompt and the tunnel underneath it. The tunnel lives exactly as long as the session: exit or Ctrl-D closes both. This is convenient and unsuitable for anything that must stay up.
Step 2 : Make the Host header match the site
If the page returns a 404, a default page, or an unrelated site, the tunnel is working and the virtual host is not. A web server that hosts several names picks the site from the Host header, and a tunnel to 127.0.0.1:8080 sends Host: 127.0.0.1:8080.
Point the real name at loopback:
# /etc/hosts
127.0.0.1 notes.example.netThen open http://notes.example.net:8080. For a one-off check without editing anything:
curl --resolve notes.example.net:8080:127.0.0.1 http://notes.example.net:8080/Chrome accepts --host-resolver-rules="MAP notes.example.net 127.0.0.1" for the same effect. Keep the port in the URL: the hosts file maps names, not ports.
The same fix covers absolute redirects. An app that answers 302 http://notes.example.net/login, or sets a cookie for that domain, sends the browser out of the tunnel and to the real host - which may be exactly what you want, or a redirect into a timeout.
Step 3 : Add or remove forwards on a running tunnel
One ssh can carry several forwards:
ssh -N -L 8080:10.0.0.5:80 -L 9090:10.0.0.9:9090 user@remote-ipTo change a running tunnel, use connection multiplexing. Start it as a master:
ssh -M -S /tmp/web-tunnel -f -N -L 8080:10.0.0.5:80 user@remote-ipThen from any terminal:
ssh -S /tmp/web-tunnel -O check user@remote-ip
ssh -S /tmp/web-tunnel -O forward -L 9090:10.0.0.9:9090 user@remote-ip
ssh -S /tmp/web-tunnel -O cancel -L 9090:10.0.0.9:9090 user@remote-ip
ssh -S /tmp/web-tunnel -O exit user@remote-ipInside a session that has a shell, the ssh escape ~C opens the same command line and accepts a new -L. This does not work with -N: the client does not read stdin, so ~C, ~# and ~. are ignored. That is the main practical cost of -N, and the reason multiplexing exists for detached tunnels.
Step 4 : Run the tunnel non-interactively
For anything that should outlive the terminal, detach it. -f backgrounds ssh after authentication, and -N is required, because ssh refuses to daemonise without a command to execute (Cannot fork into background without a command to execute).
ssh -f -N -L 8080:10.0.0.5:80 \
-o ExitOnForwardFailure=yes \
-o BatchMode=yes \
-o ServerAliveInterval=30 -o ServerAliveCountMax=3 \
user@remote-ipExitOnForwardFailure=yesturns a failed bind into a fatal error (exit 255). Without it, ssh prints the bind error and keeps running with the forward missing; with-fthe command can report success while the port never appears.BatchMode=yesfails instead of prompting for a password.The keepalives drop a connection that has silently died. Without them, a suspended laptop or expired NAT mapping leaves a local port that accepts connections which go nowhere.
Verify the tunnel by testing the port, not by trusting the exit code:
curl -sS --max-time 3 http://127.0.0.1:8080/ >/dev/null && echo up || echo downStop it by PID (pgrep -f 'ssh -f -N -L 8080') or with the multiplexing -O exit from Step 3.
Step 5 : Keep it running
Describe the tunnel once in ~/.ssh/config:
Host webtunnel
HostName remote-ip
User user
LocalForward 8080 10.0.0.5:80
ExitOnForwardFailure yes
ServerAliveInterval 30
SessionType noneSessionType none is the config-file spelling of -N, so ssh -f webtunnel starts the tunnel with no shell.
For a service that should start at login and restart on failure, use a systemd user unit:
[Unit]
Description=web tunnel to 10.0.0.5:80
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/bin/ssh -N -L 8080:10.0.0.5:80 \
-o BatchMode=yes -o ExitOnForwardFailure=yes \
-o ServerAliveInterval=30 -o ServerAliveCountMax=3 \
webtunnel
Restart=always
RestartSec=5
[Install]
WantedBy=default.targetEnable and start it with systemctl --user enable --now web-tunnel.service. Restart=always covers reconnects; autossh is the alternative when systemd is not available and does the same job with a wrapper.
Step 6 : Reverse direction, if that is what you need
To publish a web app that runs on your machine through a remote server:
ssh -N -R 8080:127.0.0.1:8000 user@public-hostThe listener now lives on public-host and forwards back to your 127.0.0.1:8000. Two settings decide whether it works:
GatewayPortson the server. With the defaultno, sshd binds the listener to the server's loopback even if you request0.0.0.0, so the tunnel comes up and nothing from outside reaches it.GatewayPorts clientspecifiedmakes the server honour your bind address.-R 0:127.0.0.1:8000lets the server choose the port; in the foreground ssh printsAllocated port N for remote forward to 127.0.0.1:8000. Local forwarding has no equivalent:-L 0:...is rejected as a bad forwarding specification.
For anything long-lived, put a reverse proxy with TLS and authentication in front of the published port rather than exposing the app directly.
Caveats
A tunnel does not add authentication. It protects the transport, not the endpoint. An unauthenticated admin page stays unauthenticated, and plain HTTP stays plain HTTP. If the target needs to be safe, fix the target.
Loopback is shared.
-Lbinds127.0.0.1and::1, which every local user on a multi-user machine can reach. For a sensitive service, forward to a Unix socket instead:-L /run/user/1000/web.sock:10.0.0.5:80creates a mode-0600 socket, reachable withcurl --unix-socket.Binding beyond loopback is a decision.
-gor-L '*:8080:...'listens on all interfaces and exposes the forward to the network. Do that deliberately, on a trusted network.TLS needs the right name. Forward to the HTTPS port and browse with the real hostname, or the certificate will not validate. With a hosts entry the name and SNI are correct, so it works; the port change only breaks apps that build absolute URLs.
Absolute URLs leave the tunnel. Redirects, cookies scoped to the real domain, and OAuth
redirect_urichecks are evaluated against the public name. The hosts entry is the usual fix; a tunnel on a different port can still fail the callback with a mismatch.One port per forward. Several services on one host mean several
-Loptions, or-D 1080with a browser SOCKS proxy. For a whole network of web UIs, a VPN is less work than a dozen forwards.Keepalives are not optional for long-lived tunnels. Without
ServerAliveInterval, a dead connection looks fine until someone tries to use it.Check both ends when the port is silent. If
curlreturns a connection reset or closes with no HTTP response, the remote target is down or blocked, not the tunnel; if the local port is not listening, the bind or the server's forwarding policy failed.The remote sshd may forbid forwarding.
AllowTcpForwarding nomakes the connection succeed and the forward fail, andPermitOpenrestricts targets. For a forwarding-only account, a dedicated user withPermitOpenis a smaller blast radius than a shell account.Verify with the port, not the exit code. The failure mode to design against is a process that reports success and forwards nothing.
Troubleshooting
symptom | likely cause | fix |
|---|---|---|
| tunnel not running, wrong local port, or bind failed | check |
tunnel exits 255 immediately | local port already in use (with | free the port; check auth; check |
404, default page, or wrong site | Host header does not match a vhost |
|
browser jumps to the real hostname | absolute redirect or cookie domain | map the name to |
certificate warning | name/SNI does not match the certificate | browse with the real name; or forward to the HTTP port behind the proxy |
port accepts but every request hangs | target unreachable from the server, or a dead connection | test from the server with |
works for a while, then stops | NAT expiry, suspend, or network change |
|
Quick reference
option | purpose |
|---|---|
| local forward (listener on your machine) |
| reverse forward (listener on the server) |
| no session, forwarding only |
| background after authentication (requires |
| allow non-local hosts to use the local forward |
| run as a multiplexing master with a control socket |
| control a running master |
| fail hard when a forward cannot be set up |
| never prompt for credentials |
| detect dead connections |
| config-file form of |
| pipe the target to stdin/stdout instead of listening |