Linux

Linux下的apt-get、wget、curl、git、docker、pip、npm、系统代理方式。

一、系统代理

设置代理:

export http_proxy=http://192.168.0.1:10811
export https_proxy=http://192.168.0.1:10811
export all_proxy=http://192.168.0.1:10811
export HTTP_PROXY=http://192.168.0.1:10811
export HTTPS_PROXY=http://192.168.0.1:10811
export ALL_PROXY=http://192.168.0.1:10811

自动设置:

  1. 所有用户:

    /etc/profile
    
  2. 当前用户:

    ~/.bashrc
    

取消设置代理:

unset ALL_PROXY
unset all_proxy
unset HTTP_PROXY
unset http_proxy
unset HTTPS_PROXY
unset https_proxy

二、git代理

设置代理:

git config --global http.proxy 'http://192.168.0.1:10811'
git config --global https.proxy 'http://192.168.0.1:10811'

取消设置代理:

git config --global --unset http.proxy
git config --global --unset https.proxy

三、wget代理

设置代理:

  1. 所有用户:

    /etc/wgetrc
    
  2. 当前用户:

    ~/.wgetrc
    

注:系统代理的优先级比配置文件更高。

use_proxy=yes
http_proxy=192.168.0.1:10811
https_proxy=192.168.0.1:10811

单次使用:

wget ... -e use_proxy=yes -e http_proxy=192.168.0.1:10811 -e https_proxy=192.168.0.1:10811 ...

四、Docker代理

设置代理:

  • (一)dockerd代理(docker pull时):
mkdir -p /etc/systemd/system/docker.service.d
nano /etc/systemd/system/docker.service.d/proxy.conf
[Service]
Environment="HTTP_PROXY=http://192.168.0.1:10811"
Environment="HTTPS_PROXY=http://192.168.0.1:10811"
Environment="NO_PROXY=localhost,127.0.0.1"

重启生效:

sudo systemctl daemon-reload
sudo systemctl restart docker
  • (二)docker build时(本质上也是在容器内):
docker build . \
    --build-arg "HTTP_PROXY=http://192.168.0.1:10811" \
    --build-arg "HTTPS_PROXY=http://192.168.0.1:10811" \
    --build-arg "NO_PROXY=localhost,127.0.0.1" \
    -t your/image:tag

一行就行:

docker build --build-arg "HTTP_PROXY=http://192.168.0.1:10811" --build-arg "HTTPS_PROXY=http://192.168.0.1:10811" --build-arg "NO_PROXY=localhost,127.0.0.1" -t your/image:tag .

Dockerfile:

ARG http_proxy=http://192.168.0.1:10811
ARG https_proxy=http://192.168.0.1:10811
ARG all_proxy=http://192.168.0.1:10811
ARG no_proxy=localhost,127.*.*.*,*.local
ARG HTTP_PROXY=http://192.168.0.1:10811
ARG HTTPS_PROXY=http://192.168.0.1:10811
ARG ALL_PROXY=http://192.168.0.1:10811
ARG NO_PROXY=localhost,127.*.*.*,*.local
  • (三)容器代理:
{
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://192.168.0.1:10811",
     "httpsProxy": "http://192.168.0.1:10811",
     "noProxy": "localhost,127.0.0.1"
   }
 }
}

当前用户:

~/.docker/config.json

五、pip代理

使用系统代理,无单独设置文件。
设置代理:
单次使用:

pip3 --proxy="http://192.168.0.1:10811" install sunraycanter

六、apt-get代理

设置代理:
一般来说环境变量里的系统代理设置就可以让apt-get通过代理,但apt-get也有自己的代理方式,优先级比环境变量高。

/etc/apt/apt.conf
Acquire::http::proxy "http://192.168.0.1:10811";
Acquire::ftp::proxy "http://192.168.0.1:10811";
Acquire::https::proxy "http://192.168.0.1:10811";

单次使用:

apt-get -o Acquire::http::proxy="http://192.168.0.1:10811" update

七、curl代理

设置代理:
环境变量里的系统代理设置就可以让curl通过代理,但curl也有自己的代理方式,优先级比环境变量高。

~/.curlrc
proxy="http://192.168.0.1:10811"

单次使用:

curl --proxy "http://192.168.0.1:10811" "https://sunraycanter.top/curlTest"

curl -x "http://192.168.0.1:10811" "https://sunraycanter.top/curlTest"

八、npm代理

设置代理:

npm config set proxy http://192.168.0.1:10811
npm config set https-proxy http://192.168.0.1:10811

如果代理不支持https的话需要修改npm存放package的网站地址。

npm config set registry "http://registry.npmjs.org/"

国内源:

npm config set registry "https://registry.npm.taobao.org"

记得更新数据库:

npm update