1. 操作系统级代理设置

设置系统代理通常会影响大部分依赖系统网络配置的应用(如浏览器、系统更新)。

Windows 10/11

  1. 打开 设置 (Settings)。
  2. 导航至 网络和 Internet (Network & Internet)。
  3. 选择左侧菜单的 代理 (Proxy)。
  4. 手动设置代理 (Manual proxy setup) 下,启用 使用代理服务器 开关。
  5. 输入 代理服务器地址端口 (例如:192.168.1.18080)。
  6. (可选)在排除列表(”不使用代理服务器的地址”)中输入内网地址等,用分号 (;) 隔开。
  7. 点击 保存 (Save)。

Linux (环境变量方式,适用于大多数命令行工具)

编辑您的 shell 配置文件(例如 ~/.bashrc~/.zshrc),添加以下行:

# 代理地址格式:http://[用户名:密码]@代理IP:端口
PROXY_ADDR="http://192.168.1.1:8080"
# 如果代理需要认证
# PROXY_ADDR="http://user:password@192.168.1.1:8080"

export http_proxy=$PROXY_ADDR
export https_proxy=$PROXY_ADDR
export ftp_proxy=$PROXY_ADDR
# 排除地址列表,用逗号分隔
export no_proxy="localhost,127.0.0.1,::1,localaddress,.localdomain.com"

# 使配置生效
source ~/.bashrc

2. 开发与运维工具代理设置

🛠️ Git

通过命令行配置,设置全局代理。

协议设置命令取消命令
HTTPgit config --global http.proxy http://host:portgit config --global --unset http.proxy
HTTPSgit config --global https.proxy http://host:portgit config --global --unset https.proxy

示例: git config --global http.proxy http://user:pwd@192.168.1.1:8080

🐋 Docker

配置 Docker 守护进程,用于 pull/search 镜像。

  1. 创建或编辑 Docker 守护进程配置文件 /etc/docker/daemon.json
  2. 添加代理配置:{
     “proxies”: {
       “default”: {
         “httpProxy”: “http://user:password@proxy.example.com:port”,
         “httpsProxy”: “http://user:password@proxy.example.com:port”,
         “noProxy”: “*.local, 169.254.0.0/16”
      }
    }
    }
  3. 重启 Docker 服务:sudo systemctl restart docker (Linux)

注意: Docker Desktop (Windows/macOS) 用户应通过其 Settings/Preferences -> Resources -> Proxies 图形界面设置。

📦 npm (Node.js 包管理器)

通过命令行设置 npm 的配置。

# 设置 HTTP 代理
npm config set proxy http://user:password@host:port
# 设置 HTTPS 代理
npm config set https-proxy http://user:password@host:port
# 查看当前配置
npm config get proxy
# 取消代理
# npm config rm proxy
# npm config rm https-proxy

🐍 pip (Python 包管理器)

通过命令行参数或配置文件设置。

  • 临时使用命令行参数:pip install package_name –proxy http://user:password@host:port
  • 永久使用配置文件:
    1. 创建或编辑配置文件:Linux/macOS 为 ~/.config/pip/pip.conf;Windows 为 %APPDATA%\pip\pip.ini
    2. 添加以下内容:[global]
      proxy = http://user:password@host:port

☕ Maven (Java 构建工具)

编辑 Maven 的全局配置文件 settings.xml(通常位于 $M2_HOME/conf/settings.xml~/.m2/settings.xml)。

<settings> 标签内添加 <proxies> 标签:

<proxies>
 <proxy>
   <id>my-proxy</id>
   <active>true</active>
   <protocol>http</protocol>
   <host>proxy.example.com</host>
   <port>8080</port>
   <username>user</username>
   <password>password</password>
   <nonProxyHosts>local.net|*.google.com</nonProxyHosts>
 </proxy>
</proxies>