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

本站现有博文312篇,共被浏览744394

本站已经建立2387天!

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