EADST

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

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

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

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


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

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

本站现有博文338篇,共被浏览978273次

本站已经建立2691天!

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