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