编辑配置文件

1
sudo vim /etc/apt/apt.conf.d/proxy.conf

写入配置

1
2
Acquire::http::Proxy "http://user:[email protected]:port/";
Acquire::https::Proxy "http://user:[email protected]:port/";

或者

1
2
3
4
Acquire {
HTTP::proxy "http://127.0.0.1:8080";
HTTPS::proxy "http://127.0.0.1:8080";
}

虽然不一定总能找 APT 镜像源,但我们还是可以用魔法打败魔法。

参考How to Set the Proxy for APT on Ubuntu 18.04

快速附魔

可以写一个脚本来启用/禁用 proxy

创建脚本文件

1
sudo vim /usr/local/bin/apt-proxy

写入脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
if [ "$1" == "off" ]
then
rm -rf /etc/apt/apt.conf.d/proxy.conf
echo "apt's proxy removed"
else
echo 'apt proxy on'
PROXY=${1:-"<DEFAULT_PROXY_URL>"}
cat <<EOT > /etc/apt/apt.conf.d/proxy.conf
APT_PROXY='Acquire::http::Proxy "$PROXY";
APT_PROXY='Acquire::https::Proxy "$PROXY";
EOT
cat /etc/apt/apt.conf.d/proxy.conf
fi

添加运行权限

1
sudo chmod +x /usr/local/bin/apt-proxy

使用

1
2
3
4
5
6
# 为 apt 添加默认 proxy
sudo apt-proxy
# 为 apt 添加指定 proxy
sudo apt-proxy <PROXY_URL>
# 禁用 apt 的 proxy
sudo apt-proxy off