EADST

Linux 环境变量代理的设置与清除

在企业内网(如百度、阿里等厂内环境)进行开发时,我们经常面临一个尴尬的网络困境:

  1. 想下载模型/库(Hugging Face, GitHub, pip):需要挂代理才能连通外网。
  2. 想连接内网服务(BOS, S3, 内部 API):挂了代理反而会报 502 Bad Gateway 或连接超时,需要关代理直连。

本文将快速介绍如何在终端中灵活地切换这两种状态。


一、 开启代理:连接外网世界

当你需要 pip installgit clone 或者下载 Hugging Face 模型时,你需要配置 http_proxyhttps_proxy 环境变量。

1. 临时设置(仅当前终端有效)

直接在终端执行以下命令。注意:关闭窗口后失效。

# 厂内常见代理地址示例 (请将 8xxx 替换为实际端口)
export http_proxy=http://agent.baidu.com:8xxx
export https_proxy=http://agent.baidu.com:8xxx

# 部分工具可能识别大写变量,建议同时设置
export HTTP_PROXY=http://agent.baidu.com:8xxx
export HTTPS_PROXY=http://agent.baidu.com:8xxx

2. 验证代理是否生效

设置完成后,可以通过 curl 测试一下 Google 或 GitHub 是否联通:

curl -I https://www.google.com
# 如果返回 HTTP/2 200 或 301,说明代理生效了

二、 查看当前代理状态

很多时候脚本跑不通,是因为我们忘了自己当前到底有没有挂代理。使用 echo 命令查看环境变量:

# 查看 http 代理
echo $http_proxy

# 查看 https 代理
echo $https_proxy
  • 如果有输出内容(如 http://agent...):说明当前开启了代理。
  • 如果输出为空:说明当前是直连模式。

三、 关闭代理:回归内网直连(重要!)

这是最容易踩坑的地方。当你的 Python 脚本需要访问内网资源(如 BOS 存储链接、内网微服务)时,如果终端里残留着上面的代理设置,请求会被强制发往代理服务器,导致 502 Bad Gateway403 Forbidden

此时,你需要彻底清除代理变量。

1. 一键清除命令

执行以下命令,将所有相关的环境变量重置为空:

unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy

2. 验证清除结果

再次检查,确保没有任何输出:

echo $http_proxy
# 输出应为空白

此时再运行你的 Python 脚本(例如访问内网 BOS 链接),流量就会直接走内网路由,不再绕道代理服务器了。


四、 进阶技巧:智能分流 (no_proxy)

如果你既想上外网,又不想影响内网访问,可以设置 no_proxy 变量。它告诉系统:“除了这些域名,其他都走代理”。

# 开启代理
export http_proxy=http://agent.baidu.com:8xxx
export https_proxy=http://agent.baidu.com:8xxx

# 设置不走代理的域名(用逗号分隔)
# 下面设置了 localhost, 本地IP, 以及百度内部域名后缀
export no_proxy="localhost,127.0.0.1,.baidu.com,.bcebos.com"

这样配置后,访问 google.com 会走代理,而访问 http://agent-distribute.bj.bcebos.com 则会自动直连。


总结

  • 下载外网资源 $\rightarrow$ export http_proxy=...
  • 访问内网报错 $\rightarrow$ unset http_proxy ...
  • 不确定网络环境 $\rightarrow$ 先 echo $http_proxy 看一眼

只需记住那个 unset 命令,能帮你节省 90% 排查网络报错的时间。

相关标签
About Me
XD
Goals determine what you are going to be.
Category
标签云
TensorRT FP16 GPTQ torchinfo Domain llama.cpp Paddle Statistics WebCrawler Tracking SQLite API Template Streamlit 第一性原理 Video Transformers Base64 BeautifulSoup BF16 Plate NameSilo Permission Nginx Logo Qwen2.5 Animate Heatmap Mixtral Tiktoken diffusers 递归学习法 MD5 Sklearn FP32 继承 EXCEL Datetime logger 算法题 ResNet-50 CC Firewall Web 证件照 Augmentation Bipartite Image2Text UNIX CSV Interview LeetCode FastAPI LaTeX Proxy Clash 腾讯云 Llama Docker VGG-16 Agent Random Gemma 阿里云 SVR COCO RGB Hotel Ubuntu PIP BTC 多进程 Python Ptyhon CEIR Windows Git 顶会 Google TensorFlow Website Cloudreve v2ray Attention HuggingFace Quantize 报税 Pillow QWEN Pickle git-lfs git 财报 Password C++ CV Disk Search FP8 v0.dev SAM Data IndexTTS2 Excel SQL Shortcut Diagram UI 飞书 NLP Tensor Qwen2 tar Jupyter LLAMA icon LLM VSCode Miniforge 图标 云服务器 Use Pandas InvalidArgumentError AI Translation hf CTC PDF GIT Breakpoint Input FP64 音频 RAR Bert WAN Markdown mmap 净利润 Plotly Qwen Jetson PyCharm 公式 Safetensors DeepStream Django 版权 Hilton Bitcoin LoRA Vim Conda 多线程 XGBoost VPN Numpy CUDA Crawler SPIE GoogLeNet JSON XML Baidu Freesound uWSGI Linux Quantization Knowledge PDB FlashAttention Pytorch Dataset printf Zip 强化学习 tqdm News Claude 搞笑 GGML Paper Food Math ChatGPT CAM NLTK 签证 Rebuttal 域名 Magnet uwsgi GPT4 OpenAI ModelScope OpenCV 关于博主 CLAP transformers TTS Github OCR Algorithm PyTorch 图形思考法 HaggingFace Color DeepSeek Vmess Bin Anaconda Review Land Distillation ONNX Hungarian scipy YOLO Card Michelin TSV
站点统计

本站现有博文323篇,共被浏览798613

本站已经建立2497天!

热门文章
文章归档
回到顶部