Linux中几种配置代理加速的方式

前言

由于网络原因,导致国内下载某些国外的包或者GitHub等网站上的文件非常慢的情况,这里记录一下工作中常用的几种需要设置代理来加速访问的方式。

开始之前,先假设我这里已经部署好了代理,地址:proxy.bunian.cn:62200

一、为wget命令设置代理

1.1、临时方式

export JQ_VERSION=1.6
wget -e use_proxy=yes \
-e http_proxy="hkproxy.bunian.cn:62200" \
-e https_proxy="hkproxy.bunian.cn:62200" \
-e HTTP_PROXY="hkproxy.bunian.cn:62200" \
-e HTTPS_PROXY="hkproxy.bunian.cn:62200" \
-e no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com" \
https://github.com/stedolan/jq/releases/download/jq-${JQ_VERSION}/jq-linux64
export JQ_VERSION=1.6
wget -e use_proxy=yes \
-e http_proxy="hkproxy.bunian.cn:62200" \
-e https_proxy="hkproxy.bunian.cn:62200" \
-e HTTP_PROXY="hkproxy.bunian.cn:62200" \
-e HTTPS_PROXY="hkproxy.bunian.cn:62200" \
-e no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com" \
https://github.com/stedolan/jq/releases/download/jq-${JQ_VERSION}/jq-linux64
export JQ_VERSION=1.6 wget -e use_proxy=yes \ -e http_proxy="hkproxy.bunian.cn:62200" \ -e https_proxy="hkproxy.bunian.cn:62200" \ -e HTTP_PROXY="hkproxy.bunian.cn:62200" \ -e HTTPS_PROXY="hkproxy.bunian.cn:62200" \ -e no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com" \ https://github.com/stedolan/jq/releases/download/jq-${JQ_VERSION}/jq-linux64

1.2、永久方式

1.2.1、针对用户而言
vi ~/.wgetrc
http_proxy="hkproxy.bunian.cn:62200"
https_proxy="hkproxy.bunian.cn:62200"
HTTP_PROXY="hkproxy.bunian.cn:62200"
HTTPS_PROXY="hkproxy.bunian.cn:62200"
no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
export http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy
source ~/.wgetrc
vi ~/.wgetrc
http_proxy="hkproxy.bunian.cn:62200"
https_proxy="hkproxy.bunian.cn:62200"
HTTP_PROXY="hkproxy.bunian.cn:62200"
HTTPS_PROXY="hkproxy.bunian.cn:62200"
no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
export http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy

source ~/.wgetrc
vi ~/.wgetrc http_proxy="hkproxy.bunian.cn:62200" https_proxy="hkproxy.bunian.cn:62200" HTTP_PROXY="hkproxy.bunian.cn:62200" HTTPS_PROXY="hkproxy.bunian.cn:62200" no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com" export http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy source ~/.wgetrc
1.2.2、针对整个系统而言
vi /etc/wgetrc
http_proxy="hkproxy.bunian.cn:62200"
https_proxy="hkproxy.bunian.cn:62200"
HTTP_PROXY="hkproxy.bunian.cn:62200"
HTTPS_PROXY="hkproxy.bunian.cn:62200"
no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
export http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy
source /etc/wgetrc
vi /etc/wgetrc
http_proxy="hkproxy.bunian.cn:62200"
https_proxy="hkproxy.bunian.cn:62200"
HTTP_PROXY="hkproxy.bunian.cn:62200"
HTTPS_PROXY="hkproxy.bunian.cn:62200"
no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
export http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy

source /etc/wgetrc
vi /etc/wgetrc http_proxy="hkproxy.bunian.cn:62200" https_proxy="hkproxy.bunian.cn:62200" HTTP_PROXY="hkproxy.bunian.cn:62200" HTTPS_PROXY="hkproxy.bunian.cn:62200" no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com" export http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy source /etc/wgetrc

二、设置系统代理

2.1、为当前登录用户设置代理

vi ~/.bash_profile
http_proxy="hkproxy.bunian.cn:62200"
https_proxy="hkproxy.bunian.cn:62200"
HTTP_PROXY="hkproxy.bunian.cn:62200"
HTTPS_PROXY="hkproxy.bunian.cn:62200"
no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
export http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy
source /etc/profile
vi ~/.bash_profile
http_proxy="hkproxy.bunian.cn:62200"
https_proxy="hkproxy.bunian.cn:62200"
HTTP_PROXY="hkproxy.bunian.cn:62200"
HTTPS_PROXY="hkproxy.bunian.cn:62200"
no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"

export http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy

source /etc/profile
vi ~/.bash_profile http_proxy="hkproxy.bunian.cn:62200" https_proxy="hkproxy.bunian.cn:62200" HTTP_PROXY="hkproxy.bunian.cn:62200" HTTPS_PROXY="hkproxy.bunian.cn:62200" no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com" export http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy source /etc/profile

2.2、为系统范围内所有用户设置代理

vi /etc/profile
http_proxy="hkproxy.bunian.cn:62200"
https_proxy="hkproxy.bunian.cn:62200"
HTTP_PROXY="hkproxy.bunian.cn:62200"
HTTPS_PROXY="hkproxy.bunian.cn:62200"
no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
export http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy
source /etc/profile
vi /etc/profile
http_proxy="hkproxy.bunian.cn:62200"
https_proxy="hkproxy.bunian.cn:62200"
HTTP_PROXY="hkproxy.bunian.cn:62200"
HTTPS_PROXY="hkproxy.bunian.cn:62200"
no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"

export http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy

source /etc/profile
vi /etc/profile http_proxy="hkproxy.bunian.cn:62200" https_proxy="hkproxy.bunian.cn:62200" HTTP_PROXY="hkproxy.bunian.cn:62200" HTTPS_PROXY="hkproxy.bunian.cn:62200" no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com" export http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy source /etc/profile

温馨提示: 如果不生效,就将export命令放在每一个命令前面。

如:export http_proxy=”hkproxy.bunian.cn:62200″ 这样。

以上是不需要设置代理的用户名、密码的方式。如果要设置该怎么解决?

http_proxy=http://username:password@hkproxy.bunian.cn:62200 wget http://file.bunian.com/test.txt
https_proxy=http://username:password@hkproxy.bunian.cn:62200 wget https://file.bunian.com/test.txt
# wgetrc或者/etc/profile文件中设置
http_proxy = http://uname:passwd@hkproxy.bunian.cn:62200
https_proxy = http://uname:passwd@hkproxy.bunian.cn:62200
use_proxy = on
http_proxy=http://username:password@hkproxy.bunian.cn:62200 wget http://file.bunian.com/test.txt
https_proxy=http://username:password@hkproxy.bunian.cn:62200 wget https://file.bunian.com/test.txt

# wgetrc或者/etc/profile文件中设置
http_proxy = http://uname:passwd@hkproxy.bunian.cn:62200
https_proxy = http://uname:passwd@hkproxy.bunian.cn:62200
use_proxy = on
http_proxy=http://username:password@hkproxy.bunian.cn:62200 wget http://file.bunian.com/test.txt https_proxy=http://username:password@hkproxy.bunian.cn:62200 wget https://file.bunian.com/test.txt # wgetrc或者/etc/profile文件中设置 http_proxy = http://uname:passwd@hkproxy.bunian.cn:62200 https_proxy = http://uname:passwd@hkproxy.bunian.cn:62200 use_proxy = on

三、为apt-get设置代理

3.1、临时方式

export http_proxy=http://hkproxy.bunian.cn:62200
export https_proxy=http://hkproxy.bunian.cn:62200
export HTTP_PROXY=http://hkproxy.bunian.cn:62200
export HTTPS_PROXY=http://hkproxy.bunian.cn:62200
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
apt-get -o Acquire::http::proxy=$HTTP_PROXY -o Acquire::https::proxy=$HTTPS_PROXY update
apt-get -o Acquire::http::proxy=$HTTP_PROXY -o Acquire::https::proxy=$HTTPS_PROXY install -y vim python3 python3-pip
export http_proxy=http://hkproxy.bunian.cn:62200
export https_proxy=http://hkproxy.bunian.cn:62200
export HTTP_PROXY=http://hkproxy.bunian.cn:62200
export HTTPS_PROXY=http://hkproxy.bunian.cn:62200
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"

apt-get -o Acquire::http::proxy=$HTTP_PROXY -o Acquire::https::proxy=$HTTPS_PROXY update
apt-get -o Acquire::http::proxy=$HTTP_PROXY -o Acquire::https::proxy=$HTTPS_PROXY install -y vim python3 python3-pip
export http_proxy=http://hkproxy.bunian.cn:62200 export https_proxy=http://hkproxy.bunian.cn:62200 export HTTP_PROXY=http://hkproxy.bunian.cn:62200 export HTTPS_PROXY=http://hkproxy.bunian.cn:62200 export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com" apt-get -o Acquire::http::proxy=$HTTP_PROXY -o Acquire::https::proxy=$HTTPS_PROXY update apt-get -o Acquire::http::proxy=$HTTP_PROXY -o Acquire::https::proxy=$HTTPS_PROXY install -y vim python3 python3-pip

3.2、永久方式

# 方法一:
touch /etc/apt/apt.conf.d/proxy.conf
vi /etc/apt/apt.conf.d/proxy.conf
Acquire::http::Proxy "http://user:password@hkproxy.bunian.cn:62200/";
Acquire::https::Proxy "http://user:password@hkproxy.bunian.cn:62200";
# 方法二
touch /etc/apt/apt.conf.d/proxy.conf
vi /etc/apt/apt.conf.d/proxy.conf
Acquire {
HTTP::proxy "http://hkproxy.bunian.cn:62200";
HTTPS::proxy "http://hkproxy.bunian.cn:62200";
}
# 方法一:
touch /etc/apt/apt.conf.d/proxy.conf
vi /etc/apt/apt.conf.d/proxy.conf
Acquire::http::Proxy "http://user:password@hkproxy.bunian.cn:62200/";
Acquire::https::Proxy "http://user:password@hkproxy.bunian.cn:62200";


# 方法二
touch /etc/apt/apt.conf.d/proxy.conf
vi /etc/apt/apt.conf.d/proxy.conf
Acquire {
  HTTP::proxy "http://hkproxy.bunian.cn:62200";
  HTTPS::proxy "http://hkproxy.bunian.cn:62200";
}
# 方法一: touch /etc/apt/apt.conf.d/proxy.conf vi /etc/apt/apt.conf.d/proxy.conf Acquire::http::Proxy "http://user:password@hkproxy.bunian.cn:62200/"; Acquire::https::Proxy "http://user:password@hkproxy.bunian.cn:62200"; # 方法二 touch /etc/apt/apt.conf.d/proxy.conf vi /etc/apt/apt.conf.d/proxy.conf Acquire { HTTP::proxy "http://hkproxy.bunian.cn:62200"; HTTPS::proxy "http://hkproxy.bunian.cn:62200"; }

四、为pip配置代理

4.1、临时方式

# 方式一
export hkproxy.bunian.cn:62200
pip3 install kubernetes
# 方式二
pip install --proxy=https://username:password@hkproxy.bunian.cn:62200 kubernetes
pip3 install --proxy hkproxy.bunian.cn:62200 kubernetes
# 方式一
export hkproxy.bunian.cn:62200
pip3 install kubernetes 

# 方式二
pip install --proxy=https://username:password@hkproxy.bunian.cn:62200 kubernetes 
pip3 install --proxy hkproxy.bunian.cn:62200 kubernetes 
# 方式一 export hkproxy.bunian.cn:62200 pip3 install kubernetes # 方式二 pip install --proxy=https://username:password@hkproxy.bunian.cn:62200 kubernetes pip3 install --proxy hkproxy.bunian.cn:62200 kubernetes

4.2、永久方式

vi ~/.bashrc
HTTP_PROXY=http://<usr_name>:<password>@hkproxy.bunian.cn:62200
HTTPS_PROXY=http://<usr_name>:<password>@hkproxy.bunian.cn:62200
export HTTP_PROXY HTTPS_PROXY
source ~/.bashrc
vi ~/.bashrc
HTTP_PROXY=http://<usr_name>:<password>@hkproxy.bunian.cn:62200
HTTPS_PROXY=http://<usr_name>:<password>@hkproxy.bunian.cn:62200
export HTTP_PROXY HTTPS_PROXY

source ~/.bashrc
vi ~/.bashrc HTTP_PROXY=http://<usr_name>:<password>@hkproxy.bunian.cn:62200 HTTPS_PROXY=http://<usr_name>:<password>@hkproxy.bunian.cn:62200 export HTTP_PROXY HTTPS_PROXY source ~/.bashrc

取消临时代理

上面一直在说设置代理,那如果我们临时设置了代理以后,用完后又需要取消代理设置怎么办?方法如下:

unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy
unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy
unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy
© 版权声明
THE END