很多项目要求内网环境,导致服务器不能使用公网的apt源,所以需要在内网中搭建一个本地的apt源。本教程将指导你如何利用apt-mirror建立局域网内的Debian/Ubuntu源镜像。如果你需要在局域网内安装多套系统,那么建立一个本地的Debian/Ubuntu源镜像将会有益,在您更新系统的时候将获得比互联网连接快得多的速度,而且还能节省您的互联网带宽。
一、使用场景
自建镜像站基本有两大使用场景:
- 提升软件包拉取速度,安装在有大带宽的机器上提供服务;
- 局域网客户端无外网访问能力,需搭建内网镜像站作为源站。
二、前期准备
你可以在debian和ubuntu系统上建立Debian/Ubuntu源镜像,也就是说,debian系统可以建立Debian和Ubuntu源镜像,ubuntu系统上也能建立Debian和Ubuntu源镜像。
需要注意的是要建立一个源镜像你需要有一个快一点的连接和足够大的硬盘空间。在我的测试过程中我做了一个Debian Sarge的main,contrib 和non-free部分的镜像和 Ubuntu Edgy Eft的 main, restricted, universe部分的镜像,这总共占用了大概25G的硬盘空间,在16M的DSL的带宽下,花了6小时的时间才下载完。
三、安装apt-mirror
这里以ubuntu为例,其他版本同理,服务器所用的系统并不需要和搭建的镜像站匹配,一个系统上可以搭建多个操作系统、不同代号版本的镜像站,你可以理解为镜像站只是基于HTTP服务提供文件下载、实时更新的功能即可。
1,首先需要准备一个安装Ubuntu系统的机器连接外网,如Ubuntu18.0.4,用于下载源文件。修改source.list安装源
# vi /etc/apt/sources.list
添加:
deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main universe multiverse restricted
deb http://archive2.kylinos.cn/deb/kylin/production/PART-V10-SP1/custom/partner/V10-SP1 default all
2、更新软件源信息
sudo apt update
3,外网机器上安装下载工具apt-mirror
apt-get install -y apt-mirror
apt-mirrot是deb同步工具,使用apt-mirror可直接同步相关软件源上的deb至本地。
四、配置镜像站源站
配置文件所在路径:/etc/apt/mirror.list
设置源站地址,即从哪里同步拉取镜像仓库,这里以清华源为例,设置源站为Debian11 bullseye版本的源站地址:
############# config ##################
# 主目录
# set base_path /var/spool/apt-mirror
# 镜像文件下载目录
# set mirror_path $base_path/mirror
# 临时索引下载文件目录,既存放软件仓库的dists目录下的文件,默认
# set skel_path $base_path/skel
# 配置日志,默认
# set var_path $base_path/var
# clean脚本
# set cleanscript $var_path/clean.sh
# 架构配置,参数:i386/amd64/arm64,默认下载本机相同的架构的源,如果为别的架构下载,
#这里是为了下载飞腾1500服务器的源,需要指定arm64
# 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 https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
# mirror additional architectures
#deb-alpha http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-amd64 http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-armel http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-hppa http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-i386 http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-ia64 http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-m68k http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-mips http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-mipsel http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-powerpc http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-s390 http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-sparc http://ftp.us.debian.org/debian unstable main contrib non-free
clean http://ftp.cn.debian.org/debian
skip-clean http://ftp.cn.debian.org/doc/
skip-clean http://ftp.cn.debian.org/tools/
示例:KylinOS:
# 主目录
set base_path /var/spool/apt-mirror
# 镜像文件下载目录
# set mirror_path $base_path/mirror
# 临时索引下载文件目录,既存放软件仓库的dists目录下的文件,默认
# set skel_path $base_path/skel
# 配置日志,默认
# set var_path $base_path/var
# clean脚本
# set cleanscript $var_path/clean.sh
# 架构配置,参数:i386/amd64/arm64,默认下载本机相同的架构的源,如果为别的架构下载,
#这里是为了下载飞腾1500服务器的源,需要指定arm64
set defaultarch arm64
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
# 下载最大线程数
set nthreads 20
set _tilde 0
# 配置下载源的地址,这里是银河麒麟的各个源地址(查看银行麒麟版本命令:cat /etc/.kyinfo)
# 银河麒麟官网源地址:http://archive.kylinos.cn/kylin/KYLIN-ALL/
#4.0.2桌面版本:
#deb http://archive.kylinos.cn/kylin/KYLIN-ALL 4.0.2-desktop main restricted universe multiverse
#4.0.2-sp1桌面版本:
#deb http://archive.kylinos.cn/kylin/KYLIN-ALL 4.0.2sp1-desktop main restricted universe multiverse
#4.0.2-sp2桌面版本:
#deb http://archive.kylinos.cn/kylin/KYLIN-ALL 4.0.2sp2-desktop main restricted universe multiverse
#4.0.2服务器版本:
#deb http://archive.kylinos.cn/kylin/KYLIN-ALL 4.0.2-server main restricted universe multiverse
#4.0.2-sp1服务器版本:
#deb http://archive.kylinos.cn/kylin/KYLIN-ALL 4.0.2sp1-server main restricted universe multiverse
#4.0.2-sp2服务器版本:
#deb http://archive.kylinos.cn/kylin/KYLIN-ALL 4.0.2sp2-server main restricted universe multiverse
#4.0.2-sp2 FT2000+服务器版本:
#deb http://archive.kylinos.cn/kylin/KYLIN-ALL 4.0.2sp2-server-ft2000 main restricted universe multiverse
#4.0.2-sp3桌面版本:
#deb http://archive.kylinos.cn/kylin/KYLIN-ALL 4.0.2sp3-desktop main restricted universe multiverse
#4.0.2-sp3服务器版本:
deb http://archive.kylinos.cn/kylin/KYLIN-ALL 4.0.2sp3-server main restricted universe multiverse
#4.0.2-sp4桌面版本:
#deb http://archive.kylinos.cn/kylin/KYLIN-ALL 4.0.2sp4-desktop main restricted universe multiverse
#10.0版本:
#deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.0 main restricted universe multiverse
#10.1版本:
#deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main restricted universe multiverse
clean http://archive.kylinos.cn/kylin/KYLIN-ALL
五、创建本地软件仓库
现在apt-mirror已经配置好了,直接在终端上执行apt-mirror即可下载相对应的软件包,重点注意软件的包大小,大部分的软件包大小在70G-150G之间,因此下载前先准备好存储空间,下载所有的包构建本地的Debian/Ubuntu软件仓库。我们只需要执行:
# sudo apt-mirror
当 apt-mirror 被意外中断时,只需要重新运行即可,apt-mirror 支持断点续存!!另外,意外关闭,需要 apt-mirror/var 目录下面删除 apt-mirror.lock 文件sudo rm apt-mirror.lock,之后执行 apt-mirror 重新启动
apt-mirror会告诉你到底要下载多少GB的文件。这将会花上几个小时,所以要耐心点。这些包将会存储在/var/spool/apt-mirror/mirror的子目录中,所以你要保证该分区还有足够的硬盘空间。
debian的在/var/spool/apt-mirror/mirror/ftp.de.debian.org/debian,
unbuntu的在/var/spool/apt-mirror/mirror/de.archive.ubuntu.com/ubuntu.
如果你在第四步选择了一个不同的镜像,那么这个路径或许有些不同。
六、清理软件仓库
等到apt-mirror下载完了所有的包,它非常可能会告诉你可以清除出一些硬盘空间来,使用以下脚本:
/bin/bash /var/spool/apt-mirror/var/clean.sh
输出如下:
server1:~# /bin/bash /var/spool/apt-mirror/var/clean.sh
Removing 157 unnecessary files [13316096 bytes]...
[0%]................done.
Removing 18 unnecessary directories...
[0%]..................done.
这样你就能看到有多少硬盘空间被清理出来了。
七、更新本地软件仓库
如果你不想手工执行更新,那么你可以编写一个定时执行的corn任务。只需要编辑/etc/corn.d/apt-mirror,在最后一行添加如下即可:
# vi /etc/cron.d/apt-mirror
#
# Regular cron jobs for the apt-mirror package
#
0 4 * * * apt-mirror /usr/bin/apt-mirror > /var/spool/apt-mirror/var/cron.log
如果想立马拉取,临时改成特定时间点即可,或者后台运行(第一次拉取会花很长时间,不建议前台执行):
nohup apt-mirror &
八、通过http访问镜像站
apt-mirror拉取镜像站默认存储到的路径为:/var/spool/apt-mirror,通过/etc/apt/mirror.list的set base_path指定,如果需要修改路径,修改此参数即可。也可以将拉取的路径设置一个软链接到web路径:Apache默认的根目录是/var/www,但是软件仓库却在/var/spool/apt-mirror/mirror 。
为了能够通过 http://192.168.0.100/debian and http://192.168.0.100/ubuntu
访问到相应的软件库,我们可以symlinks /var/www/debian and /var/www/ubuntu到真正的库地址,
ln -s /var/spool/apt-mirror/mirror/ftp.de.debian.org/debian /var/www/debian
ln -s /var/spool/apt-mirror/mirror/de.archive.ubuntu.com/ubuntu /var/www/ubuntu
如果你使用了其他镜像,记得调整这两行命令。
同时需要开启允许列出目录文件,否则会403。
root /var/www/mirror;
location / {
autoindex on;
}
九、配置你的客户端电脑使用本地镜像
1、编辑/etc/apt/source.list,加入以下内容
root@yv-virtual-machine:~# vim /etc/apt/source.list
# ip和port是自己本机的,其中apache端口默认为80
deb [arch=arm64] http://[host]:[port]/kylin/KYLIN-ALL 10.1 main restricted universe multiverse
deb [arch=arm64] http://[host]:[port]/kylin/partner 10.1 main
2、更新源
apt-get update
3、查看/查找安装软件,安装软件:
apt-cache search mysql-server
apt install mysql-server-5.7
十、在ubuntu20.04创建本地源问题汇总
1、 apt-mirror无法下载文件名带有@的文件
vim /usr/bin/apt-mirror
注释以下代码段中红色那一行
sub sanitise_uri {
my $uri = shift; $uri =~ s[^(\w+)://][];
#$uri =~ s/^([^@]+)?@?// if $uri =~ /@/;
$uri =~ s&:\d+/&/&; # and port information
$uri =~ s/~/\%7E/g if get_variable("_tilde");
return $uri;
}
2、apt-mirror无法下载cnf文件夹(apt-mirror软件最后一次更新在2017年,而ubuntu20.04在2020年才发布,软件没有对系统做适配,就是bug)
vim /usr/bin/apt-mirror
在以下代码段处加入红色的那一行
add_url_to_download( $url . $_ . "/binary-" . $arch . "/Release" );
add_url_to_download( $url . $_ . "/binary-" . $arch . "/Packages.gz" );
add_url_to_download( $url . $_ . "/binary-" . $arch . "/Packages.bz2" );
add_url_to_download( $url . $_ . "/binary-" . $arch . "/Packages.xz" );
add_url_to_download( $url . $_ . "/cnf/Commands-" . $arch . ".xz" );
add_url_to_download( $url . $_ . "/i18n/Index" );
3、不处理.xz后缀的压缩文件
apt-mirror有个小bug,可以参考bug report页面,它不处理.xz后缀的压缩文件,会导致同步后有些版本仓库404,比如bullseye-backports。
修正方法也很简单:
vim $(which apt-mirror) #修改apt-mirror脚本
大概在第523行,正则匹配后面加上.xz后缀情况就行:
#修改前:
if ( $filename =~ m{^$component/i18n/Translation-[^./]*\.bz2$} )
#修改后:
if ( $filename =~ m{^$component/i18n/Translation-[^./]*\.(bz2|xz)$} )
或者使用sed命令写入即可:sed -ine '523s/bz2/(bz2|xz)/g' $(which apt-mirror)
4、重新运行 sudo apt-mirror即可。
十一、参考文档
自建Debian/CentOS镜像站- apt-mirror与rsync-RokasYang's Blog