如何让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 #或者直接关闭终端窗口
相关推荐
pe7er2 天前
macOS 应用无法打开(权限问题)解决方案
macos·mac
harmful_sheep5 天前
mac生效的终端查看
macos
iOS门童5 天前
macOS 应用"已损坏"无法打开?一文搞懂 Gatekeeper 与解决方案
macos
NPE~5 天前
[工具分享]Maccy —— 优雅的 macOS 剪贴板历史管理工具
macos·教程·工具·实用工具
差不多程序员5 天前
Mac安装OpenClaw-cn保姆级教程
macos
dzl843945 天前
mac 安装python
开发语言·python·macos
Bruce_Liuxiaowei5 天前
在 macOS 上通过 Docker 本地安装 OpenClaw 完整教程
macos·docker·容器·openclaw
阿捏利5 天前
详解Mach-O(十五)Mach-O __DATA_CONST
macos·ios·c/c++·mach-o
ShikiSuen5 天前
macOS 的 CpLk 中英切换卡顿的元凶在 InputMethodKit 本身
macos
xiayutian_c5 天前
如虎添翼-MacOS
macos