avatar

ShinChven's Blog

给 APT 附魔

apt, proxy, ubuntu2021-08-24 06:30:15

编辑配置文件

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

写入配置

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

或者

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

创建脚本文件

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

写入脚本

#!/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

添加运行权限

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

使用

# 为 apt 添加默认 proxy
sudo apt-proxy
# 为 apt 添加指定 proxy
sudo apt-proxy <PROXY_URL>
# 禁用 apt 的 proxy
sudo apt-proxy off