In macOS, you can manage network settings through the command line using the networksetup command, including enabling and disabling the web proxy. Below are the steps to follow:
Enable Web Proxy
- Identify the Network Service Name
First, identify the exact name of the network service you want to configure (e.g., Wi-Fi, Ethernet). You can list all available network services using the following command:
bashnetworksetup -listallnetworkservices
- Enable Web Proxy
Assuming your network service is named 'Wi-Fi', use the following command to enable the HTTP proxy:
bashsudo networksetup -setwebproxy Wi-Fi 127.0.0.1 8080
In the above command, 127.0.0.1 is the IP address of the proxy server, and 8080 is the port number. Replace these with the actual IP address and port of your proxy server.
- Enable Secure Web Proxy (HTTPS Proxy)
If you also need to configure the proxy for HTTPS, use the following command:
bashsudo networksetup -setsecurewebproxy Wi-Fi 127.0.0.1 8080
Disable Web Proxy
- Disable Web Proxy
To disable the HTTP proxy, run:
bashsudo networksetup -setwebproxystate Wi-Fi off
- Disable Secure Web Proxy
Similarly, to disable the HTTPS proxy:
bashsudo networksetup -setsecurewebproxystate Wi-Fi off
Example: Integrating Commands into a Script
You can integrate these commands into a shell script to quickly enable or disable the proxy as needed. For example:
bash#!/bin/bash # Enable proxy function enable_proxy() { sudo networksetup -setwebproxy Wi-Fi 127.0.0.1 8080 sudo networksetup -setsecurewebproxy Wi-Fi 127.0.0.1 8080 echo "HTTP and HTTPS proxies are enabled." } # Disable proxy function disable_proxy() { sudo networksetup -setwebproxystate Wi-Fi off sudo networksetup -setsecurewebproxystate Wi-Fi off echo "HTTP and HTTPS proxies are disabled." } # Choose operation based on input parameter case "$1" in on) enable_proxy ;; off) disable_proxy ;; *) echo "Usage: $0 on|off" exit 1 ;; esac
Thus, you can run ./proxy_script.sh on to enable the proxy or ./proxy_script.sh off to disable it.
This approach allows you to flexibly manage the network proxy settings in macOS, accommodating various networking configuration needs.