24

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?

26

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.

| improve this answer | |
  • thanks. networksetup is just what I want. – flanker Aug 2 '11 at 3:26
  • 1
    Is there a way to set the Automatic Proxy Configuration I didn't see it in the options – Jeef Sep 23 '15 at 23:48
  • @Jeef: See superuser.com/questions/1035461/… – Mikey T.K. Mar 1 '16 at 17:15
  • can you give example of the -setwebproxy option -- i want to connect without username and password and not sure what to enter for those fields – Dinesh Feb 28 '17 at 22:23
  • @swisscheese is there a method of setting proxy as an option to safari? sort of like how on chrome for windows you can do start chrome --proxy-server=calamari652.gc-va.ncbi.nlm.nih.gov:3128 – Akin Hwan Oct 16 '19 at 16:18
8

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"
| improve this answer | |
6

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

| improve this answer | |
1

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).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.