SSH tunneling validation guide

SSH tunneling validation guide

SSH forwarding is an administrative feature that can cross network trust boundaries. Use it only for endpoints and services named in authorization. Bind to loopback by default, use the narrowest destination, preserve host-key checking, and keep an operator-visible session unless the runbook requires background operation.

Forwarding model

Mode Listener Connection made by Typical authorized use
Local -L SSH client SSH server reach one internal service through a bastion
Remote -R SSH server SSH client expose one local lab service to the SSH server
Dynamic -D SSH client SSH server SOCKS access through a managed bastion

Friend annotation — where you type vs who listens

Usual class topology: you already SSH to B. You type both -L and -R on A (your laptop). The flag does not mean "run -R on the target."

You said Precise meaning
-L opens my 8080 for whatever is on their 80 Yes. Listener on A:8080. Browser hits 127.0.0.1:8080 on A → B dials C:80 (or 127.0.0.1:80 on B).
-R opens 8080 on the target back to my 8080 Yes, with port names clear: listener on B (often 127.0.0.1:18080). Something on B hits that port → A serves your 127.0.0.1:8080.
# both from YOUR laptop (A)
ssh -N -L 127.0.0.1:8080:127.0.0.1:80 user@B   # pull their :80 to your :8080
ssh -N -R 127.0.0.1:18080:127.0.0.1:8080 user@B # push your :8080 onto B as :18080

Only if B opens SSH back to you (you are the SSH server) do the roles flip. That is a different topology — not the default demo.

Mermaid — -L local forward

sequenceDiagram
  participant You as A your laptop
  participant B as B bastion
  participant C as C service :80

  Note over You: Listener on A :8080
  You->>You: curl 127.0.0.1:8080
  You->>B: SSH channel (-L)
  B->>C: B dials C:80
  C-->>B: response
  B-->>You: back through SSH

Mermaid — -R remote forward

sequenceDiagram
  participant You as A your laptop
  participant B as B bastion
  participant Svc as A service :8080

  Note over B: Listener on B :18080
  Note over Svc: Your lab HTTP on A
  B->>B: curl 127.0.0.1:18080
  B->>You: SSH channel (-R)
  You->>Svc: A dials 127.0.0.1:8080
  Svc-->>You: response
  You-->>B: back through SSH

Mermaid — side-by-side mental model

flowchart LR
  subgraph L["-L pull to me"]
    A1["A listens :8080"] --> SSH1["SSH"] --> B1["B dials C:80"]
  end
  subgraph R["-R push to them"]
    B2["B listens :18080"] --> SSH2["SSH"] --> A2["A dials my :8080"]
  end

FAQ / common mistakes (from class Q&A)

Use this after the friend annotation. Kill these myths before anyone types ssh.

Myth / mix-up Truth
"-L opens a port on the bastion" False. -L opens the listener on A (your laptop). B only dials the destination after you hit your local port.
"-L 8080:127.0.0.1:80 means C:8080 shows up as my port 80" False. Left side = your listen port (8080). Right side = destination host:port as B sees it (127.0.0.1:80 on B, or C_IP:80).
"I used 127.0.0.1 but never wrote C's IP — so the tunnel is broken" Not broken. Destination is resolved from B. 127.0.0.1 means B's loopback. If the service is on host C, put C's IP/hostname there instead.
"The service on C sees my laptop's IP" No. C (or the service on B) sees the connection coming from the bastion (B).
"-R means I must run SSH on the target" Usually false. With A→B SSH, you still type -R on A. Listener appears on B. Only if B SSHes back to you do roles flip.
"-D needs a target host:port like -L" False. -D only sets a local SOCKS port. Each app picks destinations through B. The app must speak SOCKS (or use a SOCKS-aware wrapper).

Port order cheat (say it aloud)

-L [bind:]LOCAL_PORT:DEST_HOST:DEST_PORT
         ^ you listen here     ^ B dials this

Example friends often mistype mentally:

# YOUR listen :8080  →  B dials its own :80
ssh -N -L 127.0.0.1:8080:127.0.0.1:80 user@B

# YOUR listen :8080  →  B dials C:80  (put C's address)
ssh -N -L 127.0.0.1:8080:10.10.10.50:80 user@B

Bind defaults (class safety)

Mode Safer class bind Why
-L / -D Prefix 127.0.0.1: Listener stays on your laptop loopback
-R Prefix 127.0.0.1: on the remote side Avoids a wide-open listen on B; do not enable GatewayPorts for class

One-sentence each (checkpoint)

Local forwarding

The SSH client listens on loopback and asks the SSH server to connect to one destination:

ssh -N -o ExitOnForwardFailure=yes \
  -L 127.0.0.1:15432:db.internal.example:5432 \
  operator@approved-bastion.example

Validate with one benign database health query or TCP connection. The destination name is resolved and reached from the SSH server's network position.

For a second SSH hop, prefer the standard jump-host feature:

ssh -J operator@approved-bastion.example admin@approved-private-host.example

Remote forwarding

Keep the remote listener on the SSH server's loopback interface unless a reviewed requirement says otherwise:

ssh -N -o ExitOnForwardFailure=yes \
  -R 127.0.0.1:18080:127.0.0.1:8080 \
  operator@approved-gateway.example

A process on the gateway can now reach the approved local lab service at 127.0.0.1:18080. Do not set GatewayPorts yes for a single test. If another approved host must connect, use a dedicated bastion configuration with GatewayPorts clientspecified, host firewall restrictions, and an exact source allowlist. Back up the configuration, validate it with sshd -t, and restore it during cleanup.

Dynamic forwarding

Use a loopback SOCKS listener and direct only approved applications through it:

ssh -N -o ExitOnForwardFailure=yes \
  -D 127.0.0.1:1080 operator@approved-bastion.example

Confirm whether the client resolves DNS locally or through the proxy. Validate one approved host and port; do not use a broad subnet scan as a tunnel test.

Server controls

A managed bastion can restrict forwarding in sshd_config or authorized_keys:

AllowTcpForwarding local
PermitOpen db.internal.example:5432

PermitOpen accepts host and port destinations, not CIDR ranges. Pair it with account restrictions, MFA or short-lived certificates, firewall policy, and session logging.

Verification and cleanup

# Client-side listeners
lsof -nP -iTCP -sTCP:LISTEN

# Stop the foreground SSH session with Ctrl-C, then confirm the listener closed.

Record the SSH client and server, source and destination, listener address, ports, start and end time, one benign validation request, and relevant authentication, process, and network events. Remove temporary server configuration and firewall rules and confirm the original state.

References