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

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.

Tags:

Shell

Proxy

Mac