Linux SOCKS Proxy for SSH

How To Setup

  • In one SSH session run:
    • ssh -N -D 7080 username@server.foo.com
      • If you SSH to localhost (IP Address 127.0.0.1) on port 7080, this command forwards that port/request to server.foo.com
      • -N – No Command will be sent once the tunnel is up
        • You do not want an open session where commands can run
      • -D – Specifies the port for your localhost to listen on
    • Enter Password at prompt
    • if successful, there will be no prompt
      • Sort of like it is hanging to log in

How to Use

Use SSH -o Option

  • open up a new terminal session
  • Run the following command:
ssh -o ProxyCommand='nc --proxy-type socks4 --proxy 127.0.0.1:7080 %h %p' username@remoteserver.foo.com
  • This will uses socks version 4
  • --proxy your connection to 127.0.0.1 on port 7080
  • Substitute %h %p with username@remoteserver.foo.com

Create a SSH Config File

  • open a terminal session
  • run nano ~/.ssh/config
  • paste in the following
    • it will automatically forward connections to the above proxy server for:
    • any hostname ending with .foo.com or any IP address starting with 192.168.
      • but the SSH command will have to have the hostname not an IP address
    • But will proxy not to jumpserver.foo.com or jumpserver2.foo.com
Host *.foo.com !jumpserver.foo.com !jumpserver2.foo.com
                ProxyCommand nc --proxy-type socks4 --proxy 127.0.0.1:7080 %h %p

Host 192.168.*
                ProxyCommand nc -X 4 -x localhost:7180 %h %p
  • press CTRL + X to save
  • press Y
  • Now you can test a connection

About Daniel Fredrick

Technology enthusiast, Programmer, Network Engineer CCIE# 17094

View all posts by Daniel Fredrick →

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.