一、需求背景:

因为服务器是内部,不通互联网,去连接互联网的源是不可能的,所以需把外部的清华源搬到本地来,作为内部使用。


二、搭建方法对比:

  • apt-mirror也是用的最多的工具,但是apt-mirror是基于Ubuntu开发的,所以你的系统必须是Ubuntu(这里我的服务器是centos7)。 这里暂时不推荐apt-mirror,他在github的最新版本是2020年写的,2020年之后Ubuntu发布的新版本Ubuntu20,21源架构有变化,apt-mirror没更新工具,导致有些包下载不下来,比如cnf文件夹的包,dep11文件夹的包(Ubuntu图形界面用到此包),该文件夹的包缺失导致你无法使用。此问题有些人在百度有一些解决方法,个人试了下没效果。


  • debmirror工具。这个debian系统的工具,可以用来下载Ubuntu系统源,也可以下载debian系统,并且他内置在了cobbler工具。如果你不用cobbler,可以自己安装debmirror也是很简单的,这里自己百度了。补充:debmirror安装包来自epel源。


三、配置:

方法1:因为前期我电脑安装好了cobbler,这里我借用cobbler图形界面来配置的,我的cobbler版本是2.8,系统是centos7,截图如下:

cobbler搭建apt源(ubuntu)_服务器

cobbler搭建apt源(ubuntu)_apt_02

cobbler搭建apt源(ubuntu)_ubuntu_03


方法2:如果用命令行,如下,可以把自己的命令写到一个shell脚本定时执行:

#参数的含义自己去百度,都是很简单的。
/usr/bin/debmirror --progress --nocleanup --ignore-release-gpg -e rsync --host=mirrors.tuna.tsinghua.edu.cn --root=/ubuntu --dist=focal,focal-backports,focal-security,focal-updates --section=main,restricted,universe,multiverse /mirror/ubuntu/ubuntu20 --nosource -a amd64

#如果要在后台运行(使用nohup,请使用exit正常退出会话,不正常退出nohup后的命令也会失效),加个nohup & >> /mirror/ubuntu/ubuntu.log
nohup /usr/bin/debmirror --progress --nocleanup --ignore-release-gpg -e rsync --host=mirrors.tuna.tsinghua.edu.cn --root=/ubuntu --dist=focal,focal-backports,focal-security,focal-updates --section=main,restricted,universe,multiverse /mirror/ubuntu/ubuntu20 --nosource -a amd64 > /mirror/ubuntu/ubuntu.log &
tail -f /mirror/ubuntu/ubuntu.log #加了progress参数后,会看到日志有进度

#参数
nohup表示不挂断,在推出会话的时候;如果不将nohup命令的输出重定向,输出将附加到当前目录nohup.out文件。如果当前目录的nohup.out文件不可写,输出重定向到$HOME/nohup.out文件中,自己去看man文档了解更多。
& 表示放到后台运行,不要前端的屏幕。
-e 表示协议,可以写http,rsync(https没测,你自己试试)
--progress 是查看进度
--nocleanup 一定要加上,否则会把本地已经下载好的删除掉,而不会累加。(用方法1可以在日志看到会自动加上)

/etc/debmirror.conf #默认配置文件,有默认下载的路径;版本;使用协议等都可以在配置文件里写好默认的。
我这里写的参数都在命令上,默认用户、协议在配置文件注释掉,否则会使用配置文件的,我的配置文件如下:
[root@localhost ~]# cat /etc/debmirror.conf
# Default config for debmirror

# The config file is a perl script so take care to follow perl syntax.
# Any setting in /etc/debmirror.conf overrides these defaults and
# ~/.debmirror.conf overrides those again. Take only what you need.
#
# The syntax is the same as on the command line and variable names
# loosely match option names. If you don't recognize something here
# then just stick to the command line.
#
# Options specified on the command line override settings in the config
# files.

# Location of the local mirror (use with care)
# $mirrordir="/path/to/mirrordir"

# Output options
$verbose=0;
$progress=0;
$debug=0;

# Download options
#$host="mirrors.tuna.tsinghua.edu.cn";
#$user="";
#$passwd="anonymous@";
#$remoteroot="ubuntu";
#$download_method="rsync";
#@dists="";
#@sections="";
#@arches="";
# @ignores="";
# @excludes="";
# @includes="";
# @excludes_deb_section="";
# @limit_priority="";
$omit_suite_symlinks=0;
$skippackages=0;
# @rsync_extra="doc,tools";
$i18n=0;
$getcontents=0;
$do_source=1;
$max_batch=0;

# @di_dists="dists";
# @di_archs="arches";

# Save mirror state between runs; value sets validity of cache in days
$state_cache_days=0;

# Security/Sanity options
$ignore_release_gpg=0;
$ignore_release=0;
$check_md5sums=0;
$ignore_small_errors=0;

# Cleanup
$cleanup=0;
$post_cleanup=1;

# Locking options
$timeout=300;

# Rsync options
$rsync_batch=200;
$rsync_options="-aIL --partial";

# FTP/HTTP options
$passive=0;
# $proxy="http://proxy:port/";

# Dry run
$dry_run=0;

# Don't keep diff files but use them
$diff_mode="use";

# The config file must return true or perl complains.
# Always copy this.
1;



#使用rsync下载方式,日志如下
[root@localhost ~]# head /mirror/ubuntu/ubuntu.log -n 20
Mirroring to /mirror/ubuntu/ubuntu20 from rsync://anonymous@mirrors.tuna.tsinghua.edu.cn/ubuntu/
Arches: amd64
Dists: focal,focal-backports,focal-security,focal-updates
Sections: ,main,restricted,universe,multiverse
Pdiff mode: use
Download at most 200 files per rsync call.
Will clean up after mirroring.
Attempting to get lock ...
Updating remote trace files (using

#使用http下载方式,日志如下
[root@localhost ~]# head /mirror/ubuntu/20220610-debmirror-focal.log -n 30
Mirroring to /mirror/ubuntu/ubuntu20 from http://mirrors.tuna.tsinghua.edu.cn/ubuntu/
Arches: amd64
Dists: focal,focal-backports,focal-security,focal-updates
Sections: main,restricted,universe,multiverse
Pdiff mode: use
Will NOT clean up.
Attempting to get lock ...
Updating remote trace files (using rsync) ...
rsync: safe_read failed to read 1 bytes [Receiver]: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(276) [Receiver=3.1.3]
Warning: failed to use rsync to download extra files.
Getting meta files ...
[ 0%] Getting: dists/focal/Release... #** GET http://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/focal/Release ==> 200 OK (1s)
ok
[ 0%] Getting: dists/focal/InRelease... #** GET http://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/focal/InRelease ==> 200 OK (1s)
ok
[ 0%] Getting: dists/focal/Release.gpg... #** GET http://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/focal/Release.gpg ==> 200 OK
ok
[GNUPG:] ERRSIG 3B4FE6ACC0B21F32 1 10 00 1587663256 9
[GNUPG:] NO_PUBKEY 3B4FE6ACC0B21F32
[GNUPG:] ERRSIG 871920D1991BC93C 1 10 00 1587663256 9
[GNUPG:] NO_PUBKEY 871920D1991BC93C
gpgv: keyblock resource `/root/.gnupg/trustedkeys.gpg': No such file or directory
gpgv: Signature made Fri 24 Apr 2020 01:34:16 AM CST using RSA key ID C0B21F32
gpgv: Can't check signature: No public key
gpgv: Signature made Fri 24 Apr 2020 01:34:16 AM CST using RSA key ID 991BC93C
gpgv: Can't check signature: No public key
.temp/.tmp/dists/focal/Release.gpg signature does not verify.
[GNUPG:] ERRSIG 3B4FE6ACC0B21F32 1 10 01 1587663257 9
[GNUPG:] NO_PUBKEY 3B4FE6ACC0B21F32

@#

附录:自己写的脚本

#!/bin/bash
#author:liang ge
#date:20220204
#此脚本实现:更新Ubuntu20,21以上版本的源。Long Term Support (LTS) 长期支持版本
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

#声明版本清单。
# ubuntu18.04 bionic LTS,
# Ubuntu20.04 focal LTS,
# Ubuntu21.04 hirsute,
# Ubuntu21.10 impish (4个版本也是官方用的)
versionlist="bionic focal hirsute "
daydate=`date "+%Y%m%d"`

#循环更新函数
function updt() {
update_name=$1
if [ X"$update_name" == X"all" ];then
for i in $versionlist
do
nohup /usr/bin/debmirror --progress --nocleanup --ignore-release-gpg -e rsync --host=mirrors.tuna.tsinghua.edu.cn --root=/ubuntu --dist=${i},${i}-backports,${i}-security,${i}-updates --section=main,restricted,universe,multiverse /mirror/ubuntu/ubuntu --nosource -a amd64 > /mirror/ubuntu/${daydate}-${i}.log &
done
elif echo "$versionlist" | grep -wq "$update_name";then
nohup /usr/bin/debmirror --progress --nocleanup --ignore-release-gpg -e rsync --host=mirrors.tuna.tsinghua.edu.cn --root=/ubuntu --dist=${update_name},${update_name}-backports,${update_name}-security,${update_name}-updates --section=main,restricted,universe,multiverse /mirror/ubuntu/ubuntu --nosource -a amd64 > /mirror/ubuntu/${daydate}-${update_name}.log &
fi
}

#帮助如何使用函数
function help() {
green_echo "Usage: $0 update all|bionic|focal|hirsute"
exit 1
}

#开始
case $1 in
update )
if echo "$versionlist all" | grep -wq "$2";then
updt $2
else
help
fi
;;
* )
help
esac

问题记录:

执行debmirror的时候,日志提示:Protocol scheme ‘https’ is not supported (LWP::Protocol::https not installed)

答:yum install perl-LWP-Protocol-https