iTranslated by AI
SSH Tunneling: Port Forwarding and Proxying
Note: Operations have only been verified with OpenSSH. They may not work or may behave differently with other SSH implementations.
Contents
- Introduction
- Local Port Forwarding (local -> remote)
ssh -L - Remote Port Forwarding (remote -> local)
ssh -R - Proxy
ssh -D - Epilogue
Introduction
SSH is widely known as a tool for opening remote shells.
However, SSH also has tunneling features beyond remote shells. For example, you can achieve the following using only SSH:
- Accessing remote services as if they were running locally
- Temporarily exposing local development services to the remote
- Accessing external sites via a server
For these use cases, there is no need to open a shell, so it is common to use the -N flag to perform only communication forwarding.
From the man page:
-N Do not execute a remote command. This is useful for just forwarding ports. Refer to the description of SessionType in ssh_config(5) for details.
Local Port Forwarding
When you access a local port, it is forwarded to the remote side.
Syntax
-L [bind_address:]port:host:hostport
-L [bind_address:]port:remote_socket
-L local_socket:host:hostport
-L local_socket:remote_socket
Example: Checking a dev server running on a remote host in your browser
$ pnpm vite dev
VITE v7.1.9 ready in 1025 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
Forwarding local :3000 -> remote localhost:5173.
ssh -L 3000:localhost:5173 user@remote.example -N
# While running this, open `http://localhost:3000` in your browser to access the dev server.
Remote Port Forwarding
When you access a remote port, it is forwarded to the local side.
Syntax
-R [bind_address:]port:host:hostport
-R [bind_address:]port:local_socket
-R remote_socket:host:hostport
-R remote_socket:local_socket
-R [bind_address:]port
Example 1: Using a service running locally from a VPS
docker run --rm -p 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD=password postgres
# Forward remote localhost:5000 -> local localhost:5432
ssh -R 5000:localhost:5432 user@remote.example -N
psql postgres://postgres:password@localhost:5000
# -> Connects to the postgres instance running locally
Example 2: Exposing a local server via VPS
As a prerequisite, edit sshd_config so that -R can listen on arbitrary addresses.
$ cat /etc/ssh/sshd_config | grep "GatewayPorts"
GatewayPorts clientspecified
Open up a server. I am using GNU netcat, but options may differ for other versions of netcat.
# Caution: This will be exposed to the internet!
echo 'Hello World!' | nc -l -p 3002
Forward remote 0.0.0.0:3001 -> local localhost:3002. In other words, localhost:3002 will be exposed globally.
ssh -R 0.0.0.0:3001:localhost:3002 user@public.remote.example -N
:::
To allow connection via both IPv4 and IPv6, use an empty specification or '*' instead of 0.0.0.0. Thanks to Koichi Iwamoto for this tip!
ssh -R ':3001:localhost:3002' user@public.remote.example -N
ssh -R '*:3001:localhost:3002' user@public.remote.example -N
:::
You can now access it from anywhere.
$ echo 'Hello local!' | nc public.remote.example 3001
Hello World!
Proxy
SSH can be used as a simple SOCKS5 proxy with the -D flag.
SOCKS5 is a general-purpose proxy protocol for relaying communication.
Syntax
-D [bind_address:]port
Example: Proxying curl
ssh -D 1080 user@remote.example -N
# Comparison: Without proxy
$ curl https://ifconfig.me
(The external IP address of this device)
# Using proxy
$ curl --socks5 localhost:1080 https://ifconfig.me
(The external IP address of the remote server)
How to use SOCKS5 with an actual web browser instead of curl is a bit more complex, but I will skip it as it is not the main point of this article. Feel free to ask an AI about it.
Epilogue
If there are any inaccuracies or unclear points, please feel free to leave a comment!
Discussion
どこからでもという事ならばbind addressは
0.0.0.0ではなく*や空の指定にした方がいいかと。違いはIPv6でもListenするかどうかです。
ssh(1)のman pageにもとありますし、IPv4に依存した
0.0.0.0よりは*や空の指定の方が開発側の意図した使い方だと思います。ご指摘ありがとうございます!その通りですね。修正しました。