How to change proxy setting using Command line in Mac OS?

I am using Mac OS and sometimes some work need to be done using a proxy so I need to change my proxy setting frequently. But changing proxy setting in System Preferences is very messy.

Is there a way that I can change my proxy setting using command line? Use a shell command or some tools?


EDIT: I am assuming you actually means Mac OS X, and not Mac OS..

You can use the commandline tool 'networksetup'

There is a manpage for it - networksetup(8)

man networksetup

You will find many options for proxy settings in there:

              [-setftpproxy networkservice domain portnumber authenticated username password]
              [-setftpproxystate networkservice on | off] [-getwebproxy networkservice]
              [-setwebproxy networkservice domain portnumber authenticated username  password]
              [-setwebproxystate networkservice on | off] [-getsecurewebproxy networkservice]
              [-setsecurewebproxy networkservice domain portnumber authenticated username password]
              [-setsecurewebproxystate networkservice on | off] [-getstreamingproxy networkservice]
              [-setstreamingproxy networkservice domain portnumber authenticated username password]
              [-setstreamingproxystate networkservice on | off] [-getgopherproxy networkservice]
              [-setgopherproxy networkservice domain portnumber authenticated username password]
              [-setgopherproxystate networkservice on | off] [-getsocksfirewallproxy networkservice]
              [-setsocksfirewallproxy networkservice domain portnumber authenticated username password]
              [-setsocksfirewallproxystate networkservice on | off] [-getproxybypassdomains networkservice]
              [-setproxybypassdomains networkservice domain1 [domain2] [...]] [-getpassiveftp networkservice]

Alternatively, if you just need to set the proxy for something like wget, or curl, then you can use the fairly universal 'http_proxy' and 'ftp_proxy' env variables.

Eg in bash:

export http_proxy=http://my.proxy.com:1234

Where '1234' is the port number.

cheers

sc.


Setting up a proxy with networksetup: (check you available adapters with networksetup -listallnetworkservices before this)

networksetup -setwebproxy "Wi-fi" 127.0.0.1 8080

If required, you can setup authentication with the following syntax: [-setwebproxy networkservice domain portnumber authenticated username password]

Turning the proxy on or off:

networksetup -setwebproxystate "Wi-fi" off

View the proxy status:

networksetup -getwebproxy "Wi-Fi"

Expanding on swisscheese's answer for the specific case of the SOCKS5 proxy (i.e. the type generated with ssh -D, shadowsocks, v2ray, etc).

Given that you configured the proxy server with e.g.

networksetup -setsocksfirewallproxy wi-fi localhost 1080

đź’ˇ interface name is not case sensitive

you can later turn it off with

networksetup -setsocksfirewallproxystate wi-fi off

and then back on with

networksetup -setsocksfirewallproxystate wi-fi on

I.e. the first command only needs to be run once, and then you can just activate/deactivate the proxy as needed.

GUI for setting proxies

Here is a bitbar plugin which wraps the commands above and makes them available as dropdown options in the macos menu bar. The plugin also supports http and PAC proxies. Bitbar is an open-source app that allows putting the output of any command to the menu bar (as well as launch any command via a bash/python/ruby/node/go script). Disclaimer: I'm not affiliated with the bitbar author.

enter image description here

Another GUI solution specifically for touch-bar users:

  • https://github.com/Toxblh/MTMR

With MTMR you can use the shellScriptTitledButton in the JSON config file to run a specific command from the mac touch-bar.


Create a proxyless network environment: in the Network pane of System Preferences, click on the “Profile” drop down and select “Manage Profiles…”. Duplicate your standard settings (by clicking on the cog wheel button at the bottom of the list). Give the duplicated profile a unique name (“Proxyless”, say). Disable all proxies in the settings for that profile.

Assign a keyboard shortcut to your new profile: in the Keyboard pane of System Preferences, create a new Application shortcut for “All Applications”. Name it like your profile (i.e. “Proxyless”), add the shortcut.

The shortcut will be activated once your switch applications at least once (it won’t be active while you are still in System Preferences).