内网搭建 apt mirror

需求

Ubuntu20.04 服务器在公司内网,不能连接 Internet,需要在内网搭建 apt 源方便服务器管理。

环境

用来搭 apt 源的机器是 Win10,只能通过特定的 http 代理才能连接 Internet。

步骤

在 Win10 内新建 Ubuntu 虚拟机,设置好 Ubuntu 里 apt 的走公司的 http 代理。

虚拟机里安装 apt-mirror:

1
sudo apt-get install apt-mirror

修改 apt-mirror 配置文件:

1
sudo vim /etc/apt/mirror.list

默认是下面这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
############# config ##################
#
# set base_path /var/spool/apt-mirror
#
# set mirror_path $base_path/mirror
# set skel_path $base_path/skel
# set var_path $base_path/var
# set cleanscript $var_path/clean.sh
# set defaultarch <running host architecture>
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set nthreads 20
set _tilde 0
#
############# end config ##############

deb http://archive.ubuntu.com/ubuntu artful main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu artful-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu artful-updates main restricted universe multiverse
#deb http://archive.ubuntu.com/ubuntu artful-proposed main restricted universe multiverse
#deb http://archive.ubuntu.com/ubuntu artful-backports main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu artful main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu artful-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu artful-updates main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu artful-proposed main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu artful-backports main restricted universe multiverse

clean http://archive.ubuntu.com/ubuntu

设置项不做修改,把源改成国内比较快的

1
2
3
4
5
...

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

...

因为 apt-mirror 是用 wget 进行下载,修改 /etc/wgetrc 给 wget 设置好代理。

1
2
3
4
5
6
7
8
9
10
11
12
...

# You can set the default proxies for Wget to use for http, https, and ftp.
# They will override the value in the environment.
#https_proxy = http://proxy.yoyodyne.com:18023/
#http_proxy = http://proxy.yoyodyne.com:18023/
#ftp_proxy = http://proxy.yoyodyne.com:18023/

# If you do not want to use proxy at all, set this to off.
#use_proxy = on

...

准备好硬盘空间,我没下载源码 deb-src,用掉了将近 200G。

然后运行

1
sudo apt-mirror

耐心等待。

下载完成后安装 Apache、Nginx 或其它 http 服务,将下载好的文件用 http 发布出来。

注意链接地址(按我上述设置的话) http://your.address/ubuntu/
对应目录 /var/spool/apt-mirror/mirror/mirrors.aliyun.com/ubuntu/

问题处理

cnf/Commands-amd64 404 Not Found

原因:apt-mirror 版本太老

1
2
ls -l  /usr/bin/apt-mirror
-rwxr-xr-x 1 root root 32351 mai 29 2017 /usr/bin/apt-mirror

解决:/usr/bin/apt-mirrorhttps://github.com/Stifler6996/apt-mirror 的替换。

i386 Packages 404 Not Found

原因:mirror.list 默认设置只下载与本机相同架构的内容 set defaultarch <running host architecture>

解决:在用此 apt 源的机器上设置 sources.list 时,在 debhttp 中间加 [arch=amd64]

问题:如何让 apt-mirror 同时下载 64 和 32 位的内容?