默认现在本机已经安装好了代理软件,http端口为7890,socks5端口为7891

Linux Shell使用网络代理

命令行使用代理

设置代理:export http_proxy=http://127.0.0.1:7890 && export https_proxy=http://127.0.0.1:7890

取消代理:unset http_proxy && unset https_proxy

全局使用代理

如果是root用户,修改/etc/profile。如果是普通用户,修改~/.bashrc。在文件最后添加如下内容

1
2
3
# >>> proxy settings >>>
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7891
# >>> proxy settings >>>

然后刷新配置文件:source /etc/profile或者source ~/.bashrc

Docker使用网络代理

Docker本身使用代理(如docker pull)

1
mkdir -p /etc/systemd/system/docker.service.d

添加代理

1
vim /etc/systemd/system/docker.service.d/http-proxy.conf
1
2
3
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:7890"
Environment="HTTPS_PROXY=http://127.0.0.1:7890"

重启docker

1
2
systemctl daemon-reload
systemctl restart docker

Docker容器使用代理

  1. 如果是在Dockerfile中,则直接设置
1
2
ENV http_proxy='http://127.0.0.1:7890'
ENV https_proxy='http://127.0.0.1:7890'
  1. 如果是在docker run时想添加代理,则使用-e进行添加,如
1
docker run -d -e http_proxy=http://127.0.0.1:7890 -e https_proxy=http://127.0.0.1:7890 image

Git使用网络代理

  1. 方法一:编辑文件

编辑文件~/.gitconfig

1
vim ~/.gitconfig
1
2
3
4
[https]
proxy = http://127.0.0.1:7890
[http]
proxy = http://127.0.0.1:7890
  1. 方法二:命令行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 设置Git全局代理
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
# 或者
git config --global http.http://github.com.proxy socks5://127.0.0.1:7891
git config --global https.https://github.com.proxy socks5://127.0.0.1:7891

# 仅针对Github网站设置代理
git config --global http.http://github.com.proxy http://127.0.0.1:7890
git config --global https.https://github.com.proxy http://127.0.0.1:7890

# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy