linux系统代理以及nodejs git npm pip docker等等

linux代理

export proxy="http://192.168.1.61:7890"
export http_proxy=$proxy
export https_proxy=$proxy
export ftp_proxy=$proxy
export no_proxy="localhost, 127.0.0.1, ::1"
curl cip.cc

yum代理

vi /etc/yum.conf
proxy=http://192.168.1.1:80

apt代理

sudo vi /etc/apt/apt.conf
Acquire::http::Proxy "socks5://172.xx.xx.xxx:1080";
Acquire::https::Proxy "socks5://172.xx.xx.xxx:1080";

pip代理

mkdir -p ~/.config/pip/pip.conf
tee ~/.config/pip/pip.conf <<-'EOF'
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF

git代理

git config --global http.proxy 'socks5://192.168.1.111:3213'
git config --global https.proxy 'socks5://192.168.1.111:3213'
git config --global --unset http.proxy
git config --global --unset https.proxy
git config --global http.proxy http://username:abc@123@proxy.baidu.com:8080

解析时会从第一个@解析,提示@123@proxyhk.huawei.com找不到,此时要对其中的特殊符号进行处理,使用百分比编码(Percent-encoding)对特殊字符进行转换,转换。

列表如下:

! --> %21    # --> %23    $ --> %24    & --> %26    ' --> %27
( --> %28    ) --> %29    * --> %2A    + --> %2B    , --> %2C
/ --> %2F    : --> %3A    ; --> %3B    = --> %3D    ? --> %3F
@ --> %40    [ --> %5B    ] --> %5D

npm代理

npm config set proxy http://proxyhost:proxyport 
npm config set https-proxy http://proxyhost:proxyport

npm config set proxy null 
npm config set https-proxy null
# 或者
npm config delete proxy

# go代理(Go 版本是 1.13 及以上)

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct

gradle代理

./gradlew -DsocksProxyHost=127.0.0.1 -DsocksProxyPort=1080 bootrun

mvn代理

vi ~/.m2/settings.xml

<settings>
<proxies>
        <proxy>
                <id>ss</id>
                <active>false</active>
                <protocol>http</protocol>
                <host>192.168.85.1</host>
                <port>1080</port>
                <nonProxyHosts>127.0.0.1</nonProxyHosts>
        </proxy>
</proxies>

<mirrors>
<mirror>
  <id>alimaven</id>
  <name>aliyun maven</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>;
  <mirrorOf>central</mirrorOf>
</mirror>
</mirrors>

</settings>

docker代理(着重说明)

为容器配置代理(全局配置)

# 如果有的话,先备份一下
cp ~/.docker/config.json ~/.docker/config.json.bk

# 修改内容如下
cat ~/.docker/config.json
{
        "auths": {},
        "HttpHeaders": {
                "User-Agent": "Docker-Client/19.03.2 (linux)"
        },

		"proxies":
 		{
   			"default":
   			{
     			"httpProxy": "http://173.39.112.117:80",
     			"httpsProxy": "http://173.39.112.117:80"
   			}
 		}
}

为了确保生效,重启下 docker :systemctl restart docker

curl cip.cc

局部修改

方法1-在 docker run 命令添加参数

docker run --privileged=true -ti \
-v /sys/fs/cgroup:/sys/fs/cgroup:ro \
--env HTTP_PROXY="http://173.39.112.117:80" \
--env HTTPS_PROXY="http://173.39.112.117:80" \
--env http_proxy="http://173.39.112.117:80" \
--env https_proxy="http://173.39.112.117:80" \
-p 80:80 -d nginx

方法2-在 Docker-file 添加

FROM nginx

ENV MY_PROXY_URL="http://173.39.112.117:80"
ENV HTTP_PROXY=$MY_PROXY_URL \
    HTTPS_PROXY=$MY_PROXY_URL \
    FTP_PROXY=$MY_PROXY_URL \
    http_proxy=$MY_PROXY_URL \
    https_proxy=$MY_PROXY_URL \
    ftp_proxy=$MY_PROXY_URL


RUN yum -y install httpd; yum clean all; systemctl enable httpd.service

EXPOSE 80

CMD ["/usr/sbin/init"]

为 Docker Daemon 添加代理#

# STEP1-创建文件夹
[root@localhost home]# sudo mkdir -p /etc/systemd/system/docker.service.d

# STEP2-创建代理文件 http 和 https
[root@localhost home]# cat /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://proxy.esl.cisco.com:80/"

[root@localhost home]# cat /etc/systemd/system/docker.service.d/https-proxy.conf
[Service]
Environment="HTTPS_PROXY=http://proxy.esl.cisco.com:80/"

# 如果希望访问某些 Docker registries 不是用代理,可以在上面的配置文件后追加
[Service]    
Environment="HTTP_PROXY=http://proxy.example.com:80/" "NO_PROXY=localhost,127.0.0.1,docker-registry.example.com,.corp"

# STEP3-刷新变更
[root@localhost home]# sudo systemctl daemon-reload

# STEP4-重启 Docker
[root@localhost home]# sudo systemctl restart docker

# STEP5-验证代理是否生效
[root@localhost home]# systemctl show --property=Environment docker
Environment=HTTP_PROXY=http://proxy.esl.cisco.com/ HTTPS_PROXY=http://proxy.esl.cisco.com:80/