MacOS Proxy – App Store and Safari Work but Terminal Network Traffic Does Not

configurationmacosPROXYwifi

Set HTTP, HTTPS, FTP, SOCKS, Streaming, Gopher to 10.11.0.1 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?

Best Answer

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="http://10.11.0.1:8080"
    export http_proxy=$HTTP_PROXY
    export HTTPS_PROXY="http://10.11.0.1:8080"
    export http_proxy=$HTTP_PROXY
    export FTP_PROXY="http://10.11.0.1:8080"
    export ftp_proxy=$FTP_PROXY
    export NO_PROXY="localhost,127.0.0.1"
    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.