Skip to content

Instantly share code, notes, and snippets.

@thousandlemons
Last active July 31, 2019 18:55
Show Gist options
  • Save thousandlemons/f79341c7f180014169f27bf0c929a4e7 to your computer and use it in GitHub Desktop.
Save thousandlemons/f79341c7f180014169f27bf0c929a4e7 to your computer and use it in GitHub Desktop.
macOS SSH Proxy on/off

How to use SSH Tunneling with Socks proxy on Mac

Disable networksetup sudo password

To prevent networksetup from asking for password in the terminal, add the following line to the bottom of /etc/sudoers, where <username> is the actual username that you are using on your mac.

<username> ALL=(root) NOPASSWD: /usr/sbin/networksetup

Or, if you wish to enter your password each time you need to turn on/off the proxy, just skip this step.

Turn on the proxy

# config system socks proxy 
# (same as System Preferences -> Network -> Advanced -> Proxies -> Socks)
sudo networksetup -setsocksfirewallproxy Wi-Fi 127.0.0.1 8910
sudo networksetup -setsocksfirewallproxystate Wi-Fi on

# create ssh tunneling
# -N: no interactive ssh bash window
# -D <port>: bind local port
ssh -ND 8910 <username>@<server-ip-address>

Please note that

  1. In the two scripts above, all occurrences of 8910 can be replaced by any non-privileged port number as you wish.
  2. <username>@<server-ip-address> is just like how you normally use ssh to remote access your server.

Turn off the proxy

First, press control-C to terminate the ssh session.

Then, run the following,

# config system socks proxy 
# (same as System Preferences -> Network -> Advanced -> Proxies -> Socks)
sudo networksetup -setsocksfirewallproxy Wi-Fi "" ""
sudo networksetup -setsocksfirewallproxystate Wi-Fi off

Using connections other than Wi-Fi

Nomally, the scripts above should work if you connect to the Internet using Wi-Fi. However, if you are using any other connection, like "Ethernet", "Thunderbolt Bridge" or "Bluetooth PAN", please add similar lines in the scripts by replacing "Wi-Fi" by the actual connection names you use.

To get a list of your connections, use the following command:

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