Skip to content

Instantly share code, notes, and snippets.

@SCP002
Last active March 9, 2026 05:08
Show Gist options
  • Select an option

  • Save SCP002/58cf405c7eaab0830de5daa4f4495988 to your computer and use it in GitHub Desktop.

Select an option

Save SCP002/58cf405c7eaab0830de5daa4f4495988 to your computer and use it in GitHub Desktop.
SSH: Tunnel all traffic using dynamic port

On client, run:

ssh -f -D SOCKS_PORT -N -p SSH_PORT USER@REMOTE_ADDR

replace:

SOCKS_PORT - with port to open local SOCKS server at (for example 1080).

SSH_PORT - with SSH port server is listening to (default is 22).

USER - with your login.

REMOTE_ADDR - with address of remote machine.

and enter your password.

You can now close the terminal.

To stop it, kill the SSH process or run without -f flag and press Ctrl+C.

To transfer traffic via another server, use:

ssh -D SOCKS_PORT -J USER_1@VIA_SERVER_IP:VIA_SERVER_PORT -N -p OUT_SERVER_PORT USER_2@OUT_SERVER_IP

replace:

SOCKS_PORT - with port to open local SOCKS server at (for example 1080).

USER_1 - with login to middle server.

VIA_SERVER_IP - with IP of middle server.

VIA_SERVER_PORT - with SSH port middle server is listening to.

OUT_SERVER_PORT - with SSH port output server is listening to.

USER_2 - with login to output server.

OUT_SERVER_IP - with IP of output server.

Then, on client, either:

A) Open a browser, find proxy settings and specify "SOCKS": address 127.0.0.1, port SOCKS_PORT (the one you specified).

B) Set system proxy to these values, but it will not work on Windows since Windows proxy settings does not support SOCKS. On Windows, better use third-party software for it, for example https://github.com/InterceptSuite/ProxyBridge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment