国外有很多优质资源,但国内的外网访问体验一向不好。不少内容因为国外服务器距离远而下载慢,甚至有的直接无法在国内访问,而在 Linux 下,很多优秀工具或网站都是外国人开发的,问题则尤为明显,这个时候如果能通过国外的代理网络访问可以大幅提高访问速度。

很多命令都提供自带的代理网络配置方法,可以在使用命令时直接配置参数,甚至有一些工具可以设置全局参数,这样就不用每次使用都要添加代理配置。当然了,也有一些命令暂时没有配置网络代理的功能,那么就可以通过第三方工具,例如 proxy4chains,单独为此次命令运行配置代理网络环境。

apt

临时使用

BASH
1sudo apt-get -o Acquire::http::proxy="http://127.0.0.1:8000/" update

特定源代理

BASH
1sudo tee -a /etc/apt/apt.conf.d/10proxy << EOF >/dev/null
2Acquire::https::Proxy::download.sublimetext.com "http://127.0.0.1:10801/";
3Acquire::https::Proxy::ppa.launchpadcontent.net "http://127.0.0.1:10801/";
4Acquire::https::Proxy::packages.mozilla.org "http://127.0.0.1:10801/";
5Acquire::https::Proxy::deb.debian.org "http://127.0.0.1:10801/";
6EOF

上面这几个源也是我常用的代理源

全部走代理

BASH
1sudo tee -a /etc/apt/apt.conf.d/10proxy << EOF >/dev/null
2Acquire::http::Proxy "http://127.0.0.1:10801";
3Acquire::https::Proxy "http://127.0.0.1:10801";
4EOF
说明
推荐在遇到连接慢或者连不上的源时,再针对这个源添加代理,如果全部走代理,那国内的源也会走代理,反而会变慢

git

设置

BASH
1git config --global https.proxy http://127.0.0.1:10801
2git config --global http.proxy http://127.0.0.1:10801

查看

BASH
1git config --global --get https.proxy
2git config --global --get https.proxy

取消

BASH
1git config --global --unset http.proxy
2git config --global --unset https.proxy

npm

设置代理

BASH
1npm config set proxy http://127.0.0.1:10801
2npm config set proxy http://127.0.0.1:10801

取消代理

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

npm除了设置代理外,也可以使用国内源,优点是不需要代理,缺点就是国内源是镜像官方源的,偶尔会抽风

切换淘宝源

BASH
1npm config set registry https://registry.npm.taobao.org

换回官方仓库

BASH
1npm config set registry https://registry.npmjs.org/
说明
使用淘宝源有时会遇到报错:npm ERR! Cannot read properties of null (reading ‘pickAlgorithm’),切换回官方源即可

yarn

yarn的设置跟npm差不多

设置

BASH
1yarn config set proxy http://127.0.0.1:10801
2yarn confit set https-proxy http://127.0.0.1:10801

查看

BASH
1yarn config list

取消

BASH
1yarn config delete proxy
2yarn config delete https-proxy

切换淘宝源

BASH
1yarn config set registry https://registry.npm.taobao.org

wget

wget本身没有专门设置代理的命令行参数,但是有一个"-e"参数,可以在命令行上指定一个原本出现在".wgetrc"中的设置

BASH
1# 针对 https 资源
2wget -e "https_proxy=http://127.0.0.1:10801"
3
4# 针对 http 资源
5wget -e "http_proxy=http://127.0.0.1:10801"

也可以直接通过 export https_proxy=socks5://127.0.0.1:10800; wget 链接 的方式走代理,这种方式其实就是配置了终端的代理环境

Go

临时

BASH
1export GOPROXY=https://goproxy.io,direct

永久

BASH
1go env -w GOPROXY=goproxy.cn,direct

取消代理

BASH
1go env -w GOPROXY=direct

或设置系统代理

BASH
1export http_proxy=http://127.0.0.1:10801
2export https_proxy=http://127.0.0.1:10801

Curl

临时

BASH
1curl -x socks5://127.0.0.1:10800 $url

永久

BASH
1echo 'socks5 = "127.0.0.1:10800"' >> ~/.curlrc

临时不用

BASH
1curl --noproxy "*" $url

docker

docker需要创建一个配置文件才行,且HTTP和HTTPS需要分别用一个配置文件

BASH
 1
 2sudo mkdir -p /etc/systemd/system/docker.service.d
 3
 4sudo tee -a /etc/systemd/system/docker.service.d/http-proxy.conf << EOF > /dev/null
 5[Service]
 6Environment="HTTP_PROXY=http://127.0.0.1:10801"
 7EOF
 8
 9sudo tee -a /etc/systemd/system/docker.service.d/https-proxy.conf << EOF > /dev/null
10[Service]
11Environment="HTTPS_PROXY=http://127.0.0.1:10801"
12EOF

添加完后重启一下docker

BASH
1sudo systemctl daemon-reload
2sudo systemctl restart docker