文章

linux配置各种代理

linux配置各种代理

一、系统级代理配置

1. 临时全局代理(当前终端有效)

1
2
3
4
5
6
7
# 设置代理
export http_proxy=http://10.1.1.2:7890
export https_proxy=http://10.1.1.2:7890
export no_proxy="localhost,127.0.0.1,.example.com"  # 无需代理的地址

# 验证
echo $http_proxy  # 应输出代理地址

2. 永久全局代理(所有用户)

编辑 /etc/profile/etc/environment

1
2
3
4
5
6
7
8
9
# 追加代理配置
cat <<EOF | sudo tee -a /etc/profile
export http_proxy=http://10.1.1.2:7890
export https_proxy=http://10.1.1.2:7890
export no_proxy="localhost,127.0.0.1,.example.com"
EOF

# 使配置生效
source /etc/profile

3. 用户专属代理(当前用户)

编辑 ~/.bashrc~/.zshrc

1
2
3
4
5
6
7
# 追加代理配置
echo 'export http_proxy=http://10.1.1.2:7890' >> ~/.bashrc
echo 'export https_proxy=http://10.1.1.2:7890' >> ~/.bashrc
echo 'export no_proxy="localhost,127.0.0.1,.example.com"' >> ~/.bashrc

# 使配置生效
source ~/.bashrc

4. 删除系统代理

1
2
3
4
5
6
7
8
# 临时删除(当前终端)
unset http_proxy
unset https_proxy
unset no_proxy

# 永久删除(需编辑配置文件删除对应行)
sudo nano /etc/profile  # 删除相关 export 行
source /etc/profile     # 重新加载

二、Git 代理配置

1. 全局代理

1
2
3
4
5
6
# 设置全局代理
git config --global http.proxy http://10.1.1.2:7890
git config --global https.proxy http://10.1.1.2:7890

# 验证
git config --global --get http.proxy  # 应输出代理地址

2. 仅对特定域名使用代理

1
git config --global http.https://github.com.proxy http://10.1.1.2:7890

3. 临时代理(单次命令)

1
git -c http.proxy=http://10.1.1.2:7890 clone https://github.com/example/repo.git

4. 删除 Git 代理

1
2
3
4
5
6
# 删除全局代理
git config --global --unset http.proxy
git config --global --unset https.proxy

# 删除特定域名代理
git config --global --unset http.https://github.com.proxy

三、Docker 代理配置

1. Docker Build 代理(临时)

1
2
3
4
5
docker build . \
  --build-arg HTTP_PROXY=http://10.1.1.2:7890 \
  --build-arg HTTPS_PROXY=http://10.1.1.2:7890 \
  --build-arg NO_PROXY=localhost,127.0.0.1 \
  -t your-image:tag

2. Docker 守护进程全局代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 创建配置目录
sudo mkdir -p /etc/systemd/system/docker.service.d

# 创建代理配置文件
sudo tee /etc/systemd/system/docker.service.d/http-proxy.conf <<EOF
[Service]
Environment="HTTP_PROXY=http://10.1.1.2:7890" "HTTPS_PROXY=http://10.1.1.2:7890" "NO_PROXY=localhost,127.0.0.1"
EOF

# 重载配置并重启 Docker
sudo systemctl daemon-reload
sudo systemctl restart docker

# 验证
systemctl show docker --property=Environment | grep -i proxy

3. 含认证的代理配置

1
2
# 格式:http://用户名:密码@代理服务器:端口
Environment="HTTP_PROXY=http://user:pass@10.1.1.2:7890"

四、yum/dnf 代理配置

1. 全局配置(所有仓库)

1
2
3
4
5
6
7
8
# 编辑配置文件
sudo nano /etc/yum.conf

# 添加以下内容
[main]
proxy=http://10.1.1.2:7890
proxy_username=your_username  # 可选,代理用户名
proxy_password=your_password  # 可选,代理密码

2. 单个仓库配置(仅特定源使用代理)

1
2
3
4
5
# 编辑仓库配置文件
sudo nano /etc/yum.repos.d/epel.repo

# 在 [epel] 部分添加
proxy=http://10.1.1.2:7890

3. 临时使用代理

1
yum --proxy=http://10.1.1.2:7890 install package_name

五、wget 代理配置

1. 全局配置

1
2
3
4
5
6
7
8
# 编辑配置文件
sudo nano /etc/wgetrc

# 添加或修改以下行
http_proxy = http://10.1.1.2:7890
https_proxy = http://10.1.1.2:7890
ftp_proxy = http://10.1.1.2:7890
no_proxy = "localhost,127.0.0.1,.example.com"

2. 用户专属配置

1
2
3
# 创建或编辑个人配置
echo 'http_proxy = http://10.1.1.2:7890' >> ~/.wgetrc
echo 'https_proxy = http://10.1.1.2:7890' >> ~/.wgetrc

3. 临时使用代理

1
wget -e http_proxy=http://10.1.1.2:7890 https://example.com/file.zip

六、curl 代理配置

1. 环境变量配置(全局)

1
2
export http_proxy=http://10.1.1.2:7890
export https_proxy=http://10.1.1.2:7890

2. 命令行参数(临时)

1
curl -x http://10.1.1.2:7890 https://example.com

3. 配置文件(~/.curlrc)

1
echo 'proxy = "http://10.1.1.2:7890"' >> ~/.curlrc

七、npm 代理配置

1. 设置代理

1
2
npm config set proxy http://10.1.1.2:7890
npm config set https-proxy http://10.1.1.2:7890

2. 含认证的代理

1
2
npm config set proxy http://username:password@10.1.1.2:7890
npm config set https-proxy http://username:password@10.1.1.2:7890

3. 删除代理

1
2
npm config delete proxy
npm config delete https-proxy

八、特殊场景处理

1. socks5 代理支持

许多工具支持 socks5 协议,格式为:

1
2
3
4
5
6
# 示例
export http_proxy=socks5://10.1.1.2:1080
export https_proxy=socks5://10.1.1.2:1080

# Git 配置
git config --global http.proxy socks5://10.1.1.2:1080

2. 代理认证

1
2
# 格式:http://用户名:密码@代理服务器:端口
export http_proxy=http://user:pass@10.1.1.2:7890

3. 排除特定域名

1
2
# no_proxy 环境变量(多个域名用逗号分隔)
export no_proxy="localhost,127.0.0.1,192.168.1.0/24,.example.com"

九、验证代理配置

1. 网络连通性测试

1
curl -I https://www.google.com  # 有代理时应返回 200 OK

2. 查看请求来源

1
curl ifconfig.me  # 应返回代理服务器 IP

3. 检查环境变量

1
env | grep -i proxy
本文由作者按照 CC BY 4.0 进行授权