都什么年代了,还在手动修改 Linux 软件源?APT Proxy 使用体验

介绍

APT Proxy 是一款轻量的、可靠的 APT / YUM / APK 包(Ubuntu / Debian / CentOS / ALPINE )缓存工具,能够在各种不同的操作系统环境中运行。

这是来自 官方文档 开头的介绍,现在的作者 苏洋 是一位能力很强的博主,他对停留在 2015 年的原项目进行修改,有了现在这个版本。

而且已经不只是给 apt 用而已了,还可以给 centos 和 alpine 用,更加强大了。

想要更深入的了解,可以观看苏洋大佬的两篇文章:

轻量小巧的零配置 APT 加速工具:APT Proxy - 苏洋博客 (soulteary.com)

Linux 软件包下载加速工具:APT Proxy - 苏洋博客 (soulteary.com)

现在使用它,我们在本地装好服务器后,可以不用去修改软件源了。

平时我得到一个纯净的 Ubuntu 系统后,一般都会去修改 /etc/apt/sources.list ,将里面的内容改为清华或者阿里云的镜像源。现在使用这个工具,我们就可以不用去修改了。只要在使用 apt 的时候设置代理,指向这个工具,就会自动使用最快的镜像源。

安装

运行起来也很简单,用 docker 一条命令即可。

docker run -d --name=apt-proxy -p 3142:3142 soulteary/apt-proxy

只需要映射一个端口 3142 而已。查看容器的 log 可以看到这些。

2023/03/14 06:15:43 running apt-proxy 
2023/03/14 06:15:44 Start benchmarking mirrors
2023/03/14 06:15:44 Finished benchmarking mirrors
2023/03/14 06:15:44 using fastest Ubuntu mirror http://ftp.sjtu.edu.cn/ubuntu/
2023/03/14 06:15:44 Start benchmarking mirrors
2023/03/14 06:15:44 Finished benchmarking mirrors
2023/03/14 06:15:44 using fastest Debian mirror http://repo.huaweicloud.com/debian/
2023/03/14 06:15:44 Start benchmarking mirrors
2023/03/14 06:15:45 Finished benchmarking mirrors
2023/03/14 06:15:45 using fastest CentOS mirror https://mirrors.aliyun.com/centos/
2023/03/14 06:15:45 Start benchmarking mirrors
2023/03/14 06:15:45 Finished benchmarking mirrors
2023/03/14 06:15:45 using fastest Alpine mirror http://mirrors.aliyun.com/alpine/
2023/03/14 06:15:45 proxy listening on 0.0.0.0:3142
2023/03/14 06:15:45 Program has been started 🚀

从日志可以看到,它会自动测出速度最快的镜像站。而且此时我们访问 3142 端口,还可以看到 web 界面。

apt-proxy-web

使用

我运行 apt-proxy 的机器 IP 是 192.168.68.172,所以我可以这样这样来使用它。

# `apt-get update` with apt-proxy service
http_proxy=http://192.168.68.172:3142 apt-get -o pkgProblemResolver=true -o Acquire::http=true update
# `apt-get install vim -y` with apt-proxy service
http_proxy=http://192.168.68.172:3142 apt-get -o pkgProblemResolver=true -o Acquire::http=true install vim -y

虽然执行之后输出的信息依然显示

Get:1 http://archive.ubuntu.com/ubuntu jammy InRelease [270 kB]
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Get:4 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [829 kB]

但是它因为使用了代理,其实是用了 http://ftp.sjtu.edu.cn/ubuntu/ 这个镜像源,从速度我们也可以明显感受到。或者直接查看 apt-proxy 的日志也能看到。

2023/03/18 03:01:30 rewrote "http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease" to "http://ftp.sjtu.edu.cn/ubuntu/dists/jammy/InRelease"

然后它另一个优势,就是它会缓存。如果我们有多台服务器的话,那么第一台安装 vim,apt-proxy 会将 vim 的安装文件缓存下来,而之后的第二台,它就可以直接使用缓存了,不需要去镜像源下载了,速度会快很多。

缩短命令

不过它这命令也太长了吧,每次都得复制粘贴。我的解决方法是,使用 alias

alias aptp="http_proxy=http://192.168.68.172:3142 apt-get -o pkgProblemResolver=true -o Acquire::http=true"

我把这么一串命令,起个别名为 aptp,之后我可以这样来使用它

aptp update
aptp install zsh

非常好用的一款工具,推荐经常在本地折腾服务器的朋友,都可以去尝试一下。