Set proxy for terminal on mac

在 MacOS 终端中使用代理

根据使用的 shell,修改不同的配置文件:

  • bash 修改 ~/.bash_profile
  • zsh 修改 ~/.zshrc

在对应的文件中添加两个函数。 以本地 http 代理,端口 8001 为例。

# proxy
function proxy_off()
{
	unset http_proxy
	unset https_proxy
	unset ftp_proxy
	unset rsync_proxy
	echo -e "已关闭代理"
}

function proxy_on()
{
	export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
	export http_proxy="http://127.0.0.1:8001"
	export https_proxy=$http_proxy
	export ftp_proxy=$http_proxy
	export rsync_proxy=$http_proxy
	export HTTP_PROXY=$http_proxy
	export HTTPS_PROXY=$http_proxy
	export FTP_PROXY=$http_proxy
	export RSYNC_PROXY=$http_proxy
	echo -e "已开启代理"
}

重启终端,或在终端中运行 source ~/.bash_profilesource ~/.zshrc

可使用命令 proxy_onproxy_off 开启或关闭代理了。 开启代理只对当前终端有效。

Copyright © 2025 LOLO