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?

up vote 16 down vote accepted

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.

  • 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

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

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 one generated with ssh -D or shadowsocks).

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

💡 interface name is not case sensitive

networksetup -setsocksfirewallproxy wi-fi localhost 1080

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.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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