Skip to content
← all writing

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…

How to forward a remote web port over SSH, with and without a shell cover

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_host

part

meaning

local_port

the port opened on your machine, e.g. 8080

target_host:target_port

what to connect to from the remote host, e.g. 10.0.0.5:80

remote_host

the SSH server you log in to, e.g. user@jump.example.com

-L

local forward: listen locally, connect remotely

-N

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

-L 8080:host:80

your machine

the SSH server

-R 8080:host:80

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=yes so 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 with PermitOpen; check sshd_config on 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 free

Step 1 : Open the tunnel in the foreground

ssh -N -L 8080:10.0.0.5:80 user@remote-ip

The command stays in the foreground with no output. Verify it from another terminal:

curl -sS -D- http://127.0.0.1:8080/ | head -20

Then 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-ip

You 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.net

Then 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-ip

To 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-ip

Then 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-ip

Inside 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-ip
  • ExitOnForwardFailure=yes turns a failed bind into a fatal error (exit 255). Without it, ssh prints the bind error and keeps running with the forward missing; with -f the command can report success while the port never appears.

  • BatchMode=yes fails 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 down

Stop 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 none

SessionType 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.target

Enable 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-host

The listener now lives on public-host and forwards back to your 127.0.0.1:8000. Two settings decide whether it works:

  • GatewayPorts on the server. With the default no, sshd binds the listener to the server's loopback even if you request 0.0.0.0, so the tunnel comes up and nothing from outside reaches it. GatewayPorts clientspecified makes the server honour your bind address.

  • -R 0:127.0.0.1:8000 lets the server choose the port; in the foreground ssh prints Allocated 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

  1. 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.

  2. Loopback is shared. -L binds 127.0.0.1 and ::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:80 creates a mode-0600 socket, reachable with curl --unix-socket.

  3. Binding beyond loopback is a decision. -g or -L '*:8080:...' listens on all interfaces and exposes the forward to the network. Do that deliberately, on a trusted network.

  4. 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.

  5. Absolute URLs leave the tunnel. Redirects, cookies scoped to the real domain, and OAuth redirect_uri checks 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.

  6. One port per forward. Several services on one host mean several -L options, or -D 1080 with a browser SOCKS proxy. For a whole network of web UIs, a VPN is less work than a dozen forwards.

  7. Keepalives are not optional for long-lived tunnels. Without ServerAliveInterval, a dead connection looks fine until someone tries to use it.

  8. Check both ends when the port is silent. If curl returns 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.

  9. The remote sshd may forbid forwarding. AllowTcpForwarding no makes the connection succeed and the forward fail, and PermitOpen restricts targets. For a forwarding-only account, a dedicated user with PermitOpen is a smaller blast radius than a shell account.

  10. 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

curl: (7) Connection refused on the local port

tunnel not running, wrong local port, or bind failed

check ss -ltn, rerun with ExitOnForwardFailure=yes

tunnel exits 255 immediately

local port already in use (with ExitOnForwardFailure), bad key, or server refuses forwarding

free the port; check auth; check AllowTcpForwarding

404, default page, or wrong site

Host header does not match a vhost

/etc/hosts entry or curl --resolve

browser jumps to the real hostname

absolute redirect or cookie domain

map the name to 127.0.0.1 and keep the port

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 curl; add keepalives

works for a while, then stops

NAT expiry, suspend, or network change

ServerAliveInterval and a restart policy

Quick reference

option

purpose

-L [addr:]port:host:port

local forward (listener on your machine)

-R [addr:]port:host:port

reverse forward (listener on the server)

-N

no session, forwarding only

-f

background after authentication (requires -N or a command)

-g

allow non-local hosts to use the local forward

-M -S path

run as a multiplexing master with a control socket

-O check/forward/cancel/exit

control a running master

-o ExitOnForwardFailure=yes

fail hard when a forward cannot be set up

-o BatchMode=yes

never prompt for credentials

-o ServerAliveInterval=30 -o ServerAliveCountMax=3

detect dead connections

SessionType none

config-file form of -N

-W host:port

pipe the target to stdin/stdout instead of listening