当前位置:   article > 正文

如何让MacOS「终端」走代理

如何让MacOS「终端」走代理

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

错误提醒

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

一、配置bash_profile文件

  • 首先:在当前用户根目录找到(新建)一个文件名为 .bash_profile文件,然后输入以下代码:
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 "已开启代理"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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

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

二、实操

首先,打开终端,输入:

source  ~/.bash_profile
proxy_on
  • 1
  • 2

让MacOS终端走代理

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

curl www.google.com
  • 1

测试是否开启成功

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

source  ~/.bash_profile
proxy_off #或者直接关闭终端窗口
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/436367
推荐阅读
相关标签
  

闽ICP备14008679号