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

本站现有博文319篇,共被浏览751690

本站已经建立2408天!

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