如何让MacOS「终端」走代理

MacOS 操作系统中,默认情况下,终端命令行不会通过代理进行网络连接 。这导致在应用软件研发过程中,许多需要通过命令行下载安装的软件或依赖包无法成功安装。经常出现Failed to connect to xxx port 443 after 75329 ms: Couldn't connect to server错误提醒。

如果您需要让 Mac终端命令行走代理,可以按照以下步骤进行设置:

一、配置bash_profile文件

  • 首先:在当前用户根目录找到(新建)一个文件名为 .bash_profile文件,然后输入以下代码:
bash 复制代码
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:7890"
        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 "已开启代理"
}

函数中的 http_proxy="http://127.0.0.1:7890"就是你代理的端口和IP,比方说我用的代理,默认端口就是7890。

  • 然后:
    当让终端走代理的时候,输入: source ~/.bash_profile proxy_on
    当想关闭代理的时候,输入:source ~/.bash_profile proxy_off

二、实操

首先,打开终端,输入:

bash 复制代码
source  ~/.bash_profile
proxy_on

然后,测试是否开启成功:

bash 复制代码
curl www.google.com

当安装相应软件后,关闭代理,输入:

bash 复制代码
source  ~/.bash_profile
proxy_off #或者直接关闭终端窗口
相关推荐
岁忧5 小时前
macOS 安装 Homebrew
macos·homebrew
棒棒AIT5 小时前
mac 苹果电脑 Intel 芯片(Mac X86) 安卓虚拟机 Android模拟器 的救命稻草(下载安装指南)
android·游戏·macos·安卓·mac
*愿风载尘*12 小时前
SecureCRT连接密钥交换失败
linux·macos
mCell1 天前
从删库到跑路?这50个Linux命令能保你职业生涯
linux·windows·macos
fhf1 天前
2025年了你会卸载Macbook上的应用吗?
macos·shell
归辞...1 天前
「iOS」——内存五大分区
macos·ios·cocoa
穆雄雄1 天前
备份一下我的 mac mini 的环境变量配置情况
macos
秃然想通2 天前
mac电脑搭载c、c++环境(基于vs code)
macos
归辞...2 天前
「iOS」——GCD其他方法详解
macos·ios·cocoa
啊啊啊~~2 天前
新mac电脑软件安装指南(前端开发用)
macos·node·n·oh my zsh·solarized