Recently, I found that the speed of installing some software using brew is too slow, and many installations fail due to timeout issues. Then I searched for many methods provided by netizens and found that most of them are no longer usable or have some minor errors. Therefore, I am rewriting an article on how to use the macOS terminal to bypass the Great Firewall.
Configure Proxy#
Process omitted
Assume that the final obtained port is HTTP 127.0.0.1:1087
Configure Proxy in Terminal#
Enter the following two commands in the command line
#http
export all_proxy=http://127.0.0.1:1087
#socks
export all_proxy=socks5://127.0.0.1:1080
The macOS version of SS monitors the local HTTP port by default as 1087, while the Windows version is 1080. If you have changed the default port, use the port you specified.
This completes the process of bypassing the Great Firewall in the terminal. Of course, it is inconvenient to execute the command every time we want to bypass the firewall. So let's write the command into .bash_profile for future operations.
Add Proxy Configuration to .bash_profile#
vim ~/.bash_profile
If it hasn't been configured before, a new file will be created.
Go to .bash_profile and add the following code at the end
function proxy_on(){
export all_proxy=http://127.0.0.1:1087
echo -e "Proxy enabled"
}
function proxy_off(){
unset all_proxy
echo -e "Proxy disabled"
}
#Global proxy for Chrome
function proxy_chrome(){
open -a /Applications/Google\ Chrome.app/ --args --proxy-server=socks5://127.0.0.1:1080
echo -e "Global proxy enabled for Google Chrome"
}
#Check IP directly
function myip(){
curl -L ip.tool.lu
}
Enter the following command to make the configuration file take effect
source ~/.bash_profile
Before using the proxy, check the current IP address
curl -L ip.tool.lu
Current IP: 103.202.xxx.xx
From: Beijing
After that, enable the proxy and check again
proxy_on
Proxy enabled
curl ip.cn
Current IP: 103.88.xxx.xx
From: CatNetworks
When you don't need the proxy, execute proxy_off
to disable it
proxy_off
Proxy disabled
If you configure the environment variables in ~/.bash_profile
, but they don't take effect after restarting the terminal, you need to execute $source ~/.bash_profile
again.
I found that zsh loads the ~/.zshrc
file, and the .zshrc
file does not define any environment variables.
Solution:
At the end of the ~/.zshrc
file, add the following line:
source ~/.bash_profile
If it doesn't exist, create a file starting with .
in the home folder by pressing command
+shift
+.
.
Related Recommendations#
DDDD
Base64 aHR0cHM6Ly9iaXR6YXBwLnVrL2FmZj1VVU83YlRpbQ==