Set HTTP, HTTPS, FTP, SOCKS, Streaming, Gopher to and port 8080, with no username and password. And it works for Safari and App Store. And set the same entries to Firefox and it also works with no issue.

How do I set the proxy for particular networks so that it works fine on every app?

  • 1
    Which network traffic? (ssh, ping, scp, etc)? – Allan Aug 14 '18 at 16:40

How do I set the proxy for particular networks so that it works fine on every apps?

You have configured several proxies in System Preferences, and that's the right way to do it (that's why the App Store app and Safari work fine). Unfortunately, Terminal is an exception and needs its own proxy settings.

Why is that? Terminal is merely a container that runs a shell. A shell a program that, simply put, displays a prompt and waits patiently for you to execute commands like ls and rm.

Shells predate macOS and have their own rules for configuring proxies. Shells know nothing about proxy settings in System Preferences, and they need to be configured separately.

macOS's default shell is Bash (you can find out which shell you are running by executing echo $0 at the prompt) so I'll explain how to set the proxy in Bash.

The simple solution

  1. Launch Terminal and type this command to change directory to our home directory:

    cd ~
  2. Type these commands to open .bash_profile with TextEdit:

    touch .bash_profile
    open -a TextEdit .bash_profile
  3. Set the proxy environment variables by typing the following text in TextEdit and replace <your http proxy>, <your https proxy> and <your ftp proxy> with your proxy server (if TextEdit already contains text, add the text below at the end of the file):

    export HTTP_PROXY="<your http proxy>"
    export http_proxy=$HTTP_PROXY
    export HTTPS_PROXY="<your https proxy>"
    export http_proxy=$HTTP_PROXY
    export FTP_PROXY="<your ftp proxy>"
    export ftp_proxy=$FTP_PROXY

    If you have configured in System Preferences a list of hosts and domains that should be contacted directly bypassing the proxy, add them like this:

    export NO_PROXY="<comma-separated list of hosts>"
    export no_proxy=$NO_PROXY

    For example:

    export HTTP_PROXY=""
    export http_proxy=$HTTP_PROXY
    export HTTPS_PROXY=""
    export http_proxy=$HTTP_PROXY
    export FTP_PROXY=""
    export ftp_proxy=$FTP_PROXY
    export NO_PROXY="localhost,"
    export no_proxy=$NO_PROXY
  4. Save the file with S and close TextEdit.

  5. Close Terminal and reopen it. Now the proxy settings should work correctly.

A more elaborate solution

The method above has a drawback: you need to keep two configurations, the one in System Preferences and the one in .bash_profile.

Luckily, as explained on Derek Morgan's blog, you can have Bash import the System Preferences proxy settings using the scutil command. Simply follow the steps detailed in the previous section, but in step 3 set the http/https/ftp proxy environment variables as follows:

export HTTP_PROXY=$(scutil --proxy | awk '\
/HTTPEnable/ { enabled = $3; } \
/HTTPProxy/ { server = $3; } \
/HTTPPort/ { port = $3; } \
END { if (enabled == "1") { print "http://" server ":" port; } }')
export http_proxy=$HTTP_PROXY

export HTTPS_PROXY=$(scutil --proxy | awk '\
/HTTPSEnable/ { enabled = $3; } \
/HTTPSProxy/ { server = $3; } \
/HTTPPort/ { port = $3; } \
END { if (enabled == "1") { print "http://" server ":" port; } }')
export https_proxy=$HTTP_PROXY

export FTP_PROXY=$(scutil --proxy | awk '\
/FTPEnable/ { enabled = $3; } \
/FTPProxy/ { server = $3; } \
/FTPPort/ { port = $3; } \
END { if (enabled == "1") { print "http://" server ":" port; } }')
export ftp_proxy=$FTP_PROXY

In the definitions above, I set http:// as the scheme for all proxy servers, you might need to modify them to socks5:// for SOCKS or https:// for HTTPS as necessary.

  • One more thing I forgot to mention. Chrome and a Torrent Client does not work. What do I do about that? – gaidink Aug 16 '18 at 7:35
  • @gaidink Chrome automatically uses any proxies configured in System Preferences. You can check the current configuration in Chrome>Preferences, scroll down and click Advanced, scroll down to the System section, where you can open the proxy settings configuration. About Torrent Client, I don't know the software, is it an app or a command line tool? Check the documentation in the Help menu (in case it is an app) and the online documentation on the site you downloaded it from. – jaume Aug 16 '18 at 11:05

I going on the assumption that you mean applications/protocols like ssh can't connect because of the proxy.

To connect, you have to "tunnel" through the HTTP proxy

ssh USERNAME@REMOTEHOST -o "ProxyCommand=nc -X connect -x PROXY:PORT %h %p"

Replace USERNAME, REMOTEHOST, PROXY and PORT with their respective values.

If this is something you have to do more than once, adding a static entry to your ~/.ssh/config file is helpful. For example:

Host remotehost.foo.bar
    ProxyCommand          nc -X connect -x proxyhost:proxyport %h %p
    ServerAliveInterval   10

The ServerAliveInternal is there because most proxies will drop the connection if there's no activity for a certain amount of time.

Then, to connect, all you have to do is:

ssh remotehost.foo.bar

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .