Mac OS X does a good job of juggling proxy configurations for graphical applications while moving between wired and wireless network connections. However, this functionality doesn’t extend to command-line work in Terminal or iTerm and can be a pain when using git or package managers like npm, apm, pip, or homebrew while switching between environments. This post describes a method for programmatically setting the command-line network proxy environment variables based on the configured proxy in the Network System Preferences pane.
You can set up a proxy server on a Mac. Introduction If you ever use your Mac on a large network such as at work, at a university or at a library, you will probably need to set up a proxy. Networks use proxies to control and limit access from unauthorized users to unauthorized content. In addition, proxies make network speeds faster because a. Steps To Manually Change Your Proxy Server Settings in Mac. Open the System Preferences application in your Dock, or else go to the Apple menu and click System Preferences. Now go to Network Panel icon. The Internet panel shows all the available interfaces. Click the interface you want to configure (usually Ethernet or Wi-Fi). Now Click the “Advanced” button at the bottom right.
Mac OS X Proxy Behavior
Mac OS X maintains individual network proxy settings for each network adapter. For example, a Thunderbolt ethernet adapter has its own proxy configuration associated with it that is separate from a wireless adapter. The operating system uses the proxy configuration for the currently-connected adapter, updating the system proxy as adapter connection states change. If more than one adapter is connected, the operating system uses the proxy configuration for the connected adapter highest on the adapter list in the Network System Preferences pane. The adapter order can be changed by clicking on the gear icon at the bottom of the list and clicking the “Set Service Order…” menu item.
Access System Proxy settings in Terminal
The
scutil
command-line utility allows for management for a variety of system configuration parameters and can be used to access system proxy configuration with the --proxy
flag.Here is the output of
scutil --proxy
without a configured proxy:and here is the output of
scutil --proxy
with example.proxy
set as the system proxy for the HTTP and HTTPS protocols:Parse scutil
output
Setting Proxy For Mac Windows 10
We can use
awk
to parse the output of scutil
and extract the proxy configuration. The following snippet does the trick:This script looks for
HTTPEnable
, HTTPProxy
, and HTTPPort
in the output of scutil
. If the proxy is enabled, the script prints out the proxy URL and sets it as the http_proxy
environment variable. If the proxy is not enabled, the script sets http_proxy
to an empty string. The final line sets the HTTP_PROXY
environment variable as well since some command-line applications use that instead.Setting Proxy For Terminal Mac
Placing this snippet in your
.bash_profile
ensures that your proxy will stay configured automatically while switching between wired and wireless networks.