给 APT 附魔

Mon Aug 23 2021

编辑配置文件

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

写入配置

conf
|
Acquire::http::Proxy "http://user:password@proxy.server:port/"; Acquire::https::Proxy "http://user:password@proxy.server:port/";

或者

conf
|
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

创建脚本文件

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

写入脚本

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

添加运行权限

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

使用

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