搭建Ubuntu Server本地仓库
海上添翼
22 Jun 2021 • 8 min read
基础环境
- Ubuntu Server 20.04
- APT-Mirror
- 可以配置下载多个版本的仓库,如16.04/18.04
- 20.04仓库容量:118G,每个版本的仓库大约130G左右,如果规划多个仓库下载,需要注意磁盘空间
配置主机更新源
先把本机的/etc/apt/sources.list
更新为最快的镜像地址,一般国内使用各大云厂商的镜像库;20.04 focal的如下;可以使用Vim编辑器的批量替换功能:%s/cn.archive.ubuntu.com/mirrors.aliyun.com
deb-src
一般用不上,可以先注释。
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
安装工具
创建镜像仓库的工具为apt-mirror
,安装一下就好
sudo apt install apt-mirror -y
之后进行对应的配置
hai@apt-mirror:~$ cat /etc/apt/mirror.list
############# config ##################
#
set base_path /home/hai/apt-mirror # 下载存储路径
#
set mirror_path $base_path/mirror # 1
set skel_path $base_path/skel # 2
set var_path $base_path/var # 3
set cleanscript $var_path/clean.sh # 4 以上即便取消注释,也会作为默认参数生效
set defaultarch amd64 #
set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set nthreads 20
set _tilde 0
#
############# end config ##############
# 16.04
#deb-amd64 http://mirrors.aliyun.com/ubuntu/ xenial main
#deb-amd64 http://mirrors.aliyun.com/ubuntu/ xenial-updates main
#deb-amd64 http://mirrors.aliyun.com/ubuntu/ xenial universe
#deb-amd64 http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
#deb-amd64 http://mirrors.aliyun.com/ubuntu/ xenial-security main
#deb-amd64 http://mirrors.aliyun.com/ubuntu/ xenial-security universe
# 18.04
#deb-amd64 http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
#deb-amd64 http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
#deb-amd64 http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
#deb-amd64 http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
#deb-amd64 http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
# 20.04
deb-amd64 http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-amd64 http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-amd64 http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-amd64 http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-amd64 http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
#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
clean http://mirrors.aliyun.com/ubuntu
创建好定义的存储目录,然后运行一下,如果存储在home
的话,无需sudo
apt-mirror
如果出现错误,找不到postmirror.sh
,则拷贝脚本
cp -v /var/spool/apt-mirror/var/postmirror.sh ~/apt-mirror/var/
cp -v /var/spool/apt-mirror/var/clean.sh ~/apt-mirror/var/
# 编辑postmirror.sh;将clean.sh加入到其中
对外发布
下载完成仓库之后,使用nginx对外发布服务
sudo apt install nginx -y
对于nginx无需额外的配置,简单修改即可
sudo mv /etc/nginx/site-enabled/default /etc/nginx/site-enabled/mirror.conf
server {
listen 80 default_server;
root /home/hai/apt-mirror/mirror/mirrors.aliyun.com/ubuntu/; # 注意
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
server_name _;
location /favicon.ico {
log_not_found off;
access_log off;
}
location /robots.txt {
log_not_found off;
access_log off;
}
}
注意:root目录需要说明一下,如果包含了ubuntu目录,则sources.list
的路径最后就不需要以ubuntu为结尾,反之亦然。
因为nginx对于root目录只需要读的权限,因此可以考虑将nginx加入到当前用户的组,以确保从O权限到G的权限够用;
sudo usermod -aG 组名 www-data
# 然后加入开机自启动并启动nginx
sudo systemctl enable --now nginx
防火墙(可选)
如果启用了ufw防火墙,那么加入nginx规则即可
sudo ufw allow nginx-full
sudo ufw reload
方便使用
为了方便使用,可以按照不同的发行版,把sources.list
放到Nginx的root发布目录下,这样新系统就可以使用curl -O
的方式直接下载使用。
vim apt-mirror/mirror/mirrors.aliyun.com/ubuntu/20.04-sources.list
# Local APT-Mirror
deb http://192.168.0.90/ focal main restricted universe multiverse
deb http://192.168.0.90/ focal-security main restricted universe multiverse
deb http://192.168.0.90/ focal-updates main restricted universe multiverse
deb http://192.168.0.90/ focal-proposed main restricted universe multiverse
deb http://192.168.0.90/ focal-backports main restricted universe multiverse
#deb-src http://192.168.0.90/ focal main restricted universe multiverse
#deb-src http://192.168.0.90/ focal-security main restricted universe multiverse
#deb-src http://192.168.0.90/ focal-updates main restricted universe multiverse
#deb-src http://192.168.0.90/ focal-proposed main restricted universe multiverse
#deb-src http://192.168.0.90/ focal-backports main restricted universe multiverse
在新机器上使用以下命令快速下载使用
# 先备份
sudo mv /etc/apt/sources.list{,.bak}
# 下载使用
sudo curl -o /etc/apt/sources.list http://192.168.0.90/20.04-sources.list
完成
刷新源,马上使用
sudo apt update
添加到开机启动或定时任务
创建定时任务(选一)
每天2:30开始执行
crontab -e
30 2 * * * /usr/bin/apt-mirror 2> /home/hai/apt-mirror/log/rc-local.log
# 启动crond服务
sudo systemctl enable --now crond
添加为开机启动(选二)
每次开机将执行一次
sudo vim /lib/systemd/system/rc-local.service
# 在文件尾部添加:
[Install]
WantedBy=mulit-user.target
Alias=rc-local.service
# 创建rc.local
sudo vim /etc/rc.local
# 添加以下内容;(不添加将阻塞进程,开机卡死)
#! /bin/bash
/usr/bin/apt-mirror 2> /home/hai/apt-mirror/log/rc-local.log
exit 0
# 记得创建日志目录
sudo chmod u+x /etc/rc.local
sudo ln -s /lib/systemd/system/rc-local.service /etc/systemd/system
sudo systemctl daemon-reload
sudo systemctl start rc-local
sudo systemctl enable rc-local
遇到Bug
我在ubuntu 20.04.1 lts中遇到了apt-mirror的bug,表现为“Commands-amd64 404 Not Found”
Ign:53 http://apt-mirror.local/ubuntu focal-proposed/multiverse amd64 c-n-f Metadata
Err:44 http://apt-mirror.local/ubuntu focal-proposed/main amd64 c-n-f Metadata
404 Not Found [IP: 192.168.0.111 80]
Ign:47 http://apt-mirror.local/ubuntu focal-proposed/restricted amd64 c-n-f Metadata
Ign:56 http://apt-mirror.local/ubuntu focal-backports/main amd64 c-n-f Metadata
Ign:57 http://apt-mirror.local/ubuntu focal-backports/restricted amd64 c-n-f Metadata
Ign:58 http://apt-mirror.local/ubuntu focal-backports/universe amd64 c-n-f Metadata
Ign:59 http://apt-mirror.local/ubuntu focal-backports/multiverse amd64 c-n-f Metadata
Ign:50 http://apt-mirror.local/ubuntu focal-proposed/universe amd64 c-n-f Metadata
Ign:53 http://apt-mirror.local/ubuntu focal-proposed/multiverse amd64 c-n-f Metadata
Ign:56 http://apt-mirror.local/ubuntu focal-backports/main amd64 c-n-f Metadata
Ign:57 http://apt-mirror.local/ubuntu focal-backports/restricted amd64 c-n-f Metadata
Ign:58 http://apt-mirror.local/ubuntu focal-backports/universe amd64 c-n-f Metadata
Ign:59 http://apt-mirror.local/ubuntu focal-backports/multiverse amd64 c-n-f Metadata
Err:56 http://apt-mirror.local/ubuntu focal-backports/main amd64 c-n-f Metadata
404 Not Found [IP: 192.168.0.111 80]
Ign:57 http://apt-mirror.local/ubuntu focal-backports/restricted amd64 c-n-f Metadata
Ign:58 http://apt-mirror.local/ubuntu focal-backports/universe amd64 c-n-f Metadata
Ign:59 http://apt-mirror.local/ubuntu focal-backports/multiverse amd64 c-n-f Metadata
Fetched 1,174 kB in 1s (1,166 kB/s)
Reading package lists... Done
E: Failed to fetch http://apt-mirror.local/ubuntu/dists/focal/main/cnf/Commands-amd64 404 Not Found [IP: 192.168.0.111 80]
E: Failed to fetch http://apt-mirror.local/ubuntu/dists/focal-security/main/cnf/Commands-amd64 404 Not Found [IP: 192.168.0.111 80]
E: Failed to fetch http://apt-mirror.local/ubuntu/dists/focal-updates/main/cnf/Commands-amd64 404 Not Found [IP: 192.168.0.111 80]
E: Failed to fetch http://apt-mirror.local/ubuntu/dists/focal-proposed/main/cnf/Commands-amd64 404 Not Found [IP: 192.168.0.111 80]
E: Failed to fetch http://apt-mirror.local/ubuntu/dists/focal-backports/main/cnf/Commands-amd64 404 Not Found [IP: 192.168.0.111 80]
E: Some index files failed to download. They have been ignored, or old ones used instead.
现在还不知道什么能合并到主线中进行更新修复,但是基于强大的社区,总是有人在来解决问题,在GitHub上已经有人给出了解决办法;GitHub地址,下载其中的apt-mirror
文件替换即可;它其实是个Perl脚本;
# 备份原先的
sudo mv /usr/bin/apt-mirror{,.bak}
# 进行替换
sudo mv apt-mirror /usr/bin/apt-mirror
# 需要修复权限;原先的权限
sudo ls -al /usr/bin/apt-mirror.bak
-rwxr-xr-x 1 root root 32351 May 29 2017 /usr/bin/apt-mirror.bak
# 替换后的权限对齐
sudo chmod 755 /usr/bin/apt-mirror
# 再次查看
sudo ls -al /usr/bin/apt-mirror
-rw-rw-r-- 1 hai hai 32509 Dec 19 09:45 /usr/bin/apt-mirror
在仓库所在机器上再次执行sudo apt-mirror
之后,会下载对应的文件,然后去需要更新的机器上执行sudo apt update
就没问题了。
原文链接:搭建Ubuntu Server本地仓库 (cpci.dev)