LLI Wiki — My Digital Knowledge Base

SSH: tunnels, port forwarding, ProxyJump and mosh

Cheat sheet for SSH tunnels (VNC, RDP, SQL Server), ProxyJump/ProxyCommand jump hosts, plink and mosh.

A cheat sheet of SSH tunnelling patterns: local port forwarding, forwarding to SQL Server and RDP through a gateway, jump hosts (ProxyJump / ProxyCommand), PuTTY/plink equivalents and mosh.

Local port forwarding

Forward local port 8080 to port 80 of a host reachable from the SSH server:

ssh -L 8080:192.0.2.10:80 user@gateway.example.com

With PuTTY (Windows): Connection > SSH > Tunnels, set Source port 8080, Destination 192.0.2.10:80, choose "Local", then Add.

List the SSH tunnels currently open:

sudo lsof -i -n | egrep '\<ssh\>'

Look up the port used by a service:

cat /etc/services | grep smtp

VNC through a tunnel

ssh -C -L 5908:localhost:5951 user@gateway.example.com
vncviewer -encoding hextile -bgr233 127.0.0.1:8

-C enables compression, hextile is the best encoding for a WAN link and bgr233 selects 8-bit colour.

Forward a local port to the SQL Server port of a server only reachable through a gateway. Here gateway01 is the SSH host, sqlserver01:1433 the target and 14331 the local port:

plink -v -x -a -T -C -ssh -pw "<PASSWORD>" -L 14331:sqlserver01:1433 app_user@gateway01

Repeat with a different local port for each target server (for example 14332 for sqlserver02). Connect the client to localhost,14331. Prefer key-based authentication (-i key.ppk) over -pw.

PuTTY: Tunnels tab, local port forwards

Remote Desktop through a tunnel

ssh -L 9999:192.0.2.12:3389 user@gateway.example.com

Then connect to the forwarded port:

mstsc /v:localhost:9999

On Linux: rdesktop localhost:9999. Several forwards can be combined in one command:

ssh -C -L 3389:192.0.2.13:3389 -L 5903:localhost:5951 user@gateway.example.com

PuTTY: Tunnels tab with L5904, L5905 and L3390 forwards

MobaXterm: SSH tunnel editor, local port forwarding

Jump hosts

ProxyJump

ssh -J app_user@jumphost01 app_user@sqlserver01

In ~/.ssh/config:

### First jumphost. Directly reachable
Host alphajump
  HostName jumphost1.example.org

### Second jumphost. Only reachable via jumphost1.example.org
Host betajump
  HostName jumphost2.example.org
  ProxyJump alphajump

### Host only reachable via alphajump and betajump
Host behindalphabeta
  HostName behindalphabeta.example.org
  ProxyJump betajump

ProxyCommand (older method)

ssh -o ProxyCommand="ssh -W %h:%p bastion-host" remote-host
ssh -o StrictHostKeyChecking=no -o ProxyCommand="ssh -W %h:%p bastion-host" remote-host

In ~/.ssh/config:

Host jumpy
  ProxyCommand ssh -W destination-host bastion-host

Host vps1
  HostName vps1.example.org
  IdentityFile ~/.ssh/vps1.pem
  User ec2-user

Host vps2
  HostName vps2.example.org
  IdentityFile ~/.ssh/vps2_key
  Port 22
  User admin
  ProxyCommand ssh -q -W %h:%p vps1

PuTTY can use plink as a proxy command (Connection > Proxy > Local). The template is:

plink.exe %user@%proxyhost -ssh -i c:\keys\my_key.ppk -nc %host:%port

If the bastion listens on a non-default port, add -P %proxyport. See also how to set up ProxyJump with PuTTY.

mosh

mosh 192.0.2.14 --ssh="ssh -p 2222" -- sh -c 'tmux a -t pairing || tmux new -s pairing'

This opens (or re-attaches) a tmux session named pairing over mosh, with SSH on a custom port.

References

Notes

Screenshots

PuTTY: session window, host name and saved sessions

PuTTY: Data page, login details

PuTTY: Proxy page with a local plink command

Mastodon LLIMastodon SocialFosstodon