Ubuntu 26.04 搭建软件同步源完整教程
前言
在企业内网或实验室环境中,多台 Ubuntu 服务器需要安装和更新软件包时,如果每台都从外网下载,不仅速度慢,还浪费带宽。搭建一个本地软件同步源(镜像站),可以让所有内网服务器从本地高速获取软件包,同时还能控制软件版本、提高安全性。
本教程将详细介绍如何在 Ubuntu 26.04 上搭建一个完整的软件同步源,包括官方仓库和第三方仓库的同步。
环境说明
- 操作系统:Ubuntu 26.04 LTS(代号:Noble Numbat)
- 同步方式:
apt-mirror(适合完整镜像) /reprepro(适合定制化) - Web 服务:Nginx
- 存储空间:建议至少 200GB(完整镜像需要 500GB+)
方案选择
| 方案 | 适用场景 | 优点 | 缺点 |
|---|---|---|---|
| apt-mirror | 完整镜像 | 配置简单,自动化程度高 | 占用空间大,定制性较差 |
| reprepro | 定制化同步 | 灵活,可精细控制 | 配置复杂 |
| rsync + debmirror | 高级定制 | 速度快,功能强大 | 学习成本高 |
本教程以 apt-mirror 方案为主,兼顾 reprepro 方案。
第一步:安装必要软件包
# 更新软件包列表
apt update
# 安装 apt-mirror 和相关工具
apt install -y apt-mirror nginx lftp wget
# apt-mirror 用于同步软件源
# nginx 用于对外提供 HTTP 服务
# lftp 用于下载文件(支持断点续传)
第二步:配置 apt-mirror
编辑 apt-mirror 配置文件
# 编辑 apt-mirror 主配置文件
vim /etc/apt/mirror.list
配置内容
# apt-mirror 配置文件示例
# 全局设置
set base_path /var/www/apt-mirror
set mirror_path /var/www/apt-mirror/mirror
set skel_path /var/www/apt-mirror/skel
set var_path /var/www/apt-mirror/var
set cleanscript /var/www/apt-mirror/var/clean.sh
defaulturi http://archive.ubuntu.com/ubuntu
# 设置镜像源列表
# Ubuntu 26.04 LTS (Noble Numbat) - 主源
deb http://archive.ubuntu.com/ubuntu noble main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu noble-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu noble-backports main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu noble-security main restricted universe multiverse
# Ubuntu 26.04 LTS - 源代码
deb-src http://archive.ubuntu.com/ubuntu noble main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu noble-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu noble-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu noble-security main restricted universe multiverse
# Ubuntu 24.04 LTS (Jammy) - 旧版本
deb http://archive.ubuntu.com/ubuntu jammy main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu jammy-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu jammy-backports main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu jammy-security main restricted universe multiverse
# Ubuntu 22.04 LTS (Jammy) - 旧版本
deb http://archive.ubuntu.com/ubuntu focal main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu focal-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu focal-backports main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu focal-security main restricted universe multiverse
# Aliyun 镜像源(国内速度快)
# deb http://mirrors.aliyun.com/ubuntu noble main restricted universe multiverse
# deb http://mirrors.aliyun.com/ubuntu noble-updates main restricted universe multiverse
# deb http://mirrors.aliyun.com/ubuntu noble-backports main restricted universe multiverse
# deb http://mirrors.aliyun.com/ubuntu noble-security main restricted universe multiverse
# 163 镜像源
# deb http://mirrors.163.com/ubuntu noble main restricted universe multiverse
# deb http://mirrors.163.com/ubuntu noble-updates main restricted universe multiverse
# deb http://mirrors.163.com/ubuntu noble-backports main restricted universe multiverse
# deb http://mirrors.163.com/ubuntu noble-security main restricted universe multiverse
# 清华镜像源
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu noble main restricted universe multiverse
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu noble-updates main restricted universe multiverse
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu noble-backports main restricted universe multiverse
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu noble-security main restricted universe multiverse
# 设置下载选项
set nthreads 10
set _tilde 0
配置说明
set base_path:镜像根目录set mirror_path:实际下载的软件包目录set skel_path:模板目录set var_path:日志和临时文件目录deb http://...:指定要同步的软件源,格式为deb URL dist componentdeb-src:指定源代码包nthreads:并发下载线程数,根据网络带宽调整_tilde:是否使用~进行压缩(0 表示不使用)
第三步:开始同步
首次同步
# 执行同步(首次同步时间较长,可能需要数小时)
apt-mirror
同步过程会:
1. 连接上游镜像源
2. 下载软件包列表
3. 下载所有软件包文件
4. 生成索引文件
同步过程监控
# 监控下载进度
watch -n 5 'du -sh /var/www/apt-mirror/mirror/archive.ubuntu.com'
# 查看同步日志
tail -f /var/www/apt-mirror/var/log/apt-mirror.log
同步完成后
# 生成索引文件
cd /var/www/apt-mirror/mirror/archive.ubuntu.com/ubuntu
dpkg-scanpackages . /dev/null > Packages.gz
gzip -d Packages.gz
apt-ftparchive packages . > Packages
apt-ftparchive sources . > Sources
# 为 noble-security 生成索引
cd noble-security
dpkg-scanpackages . /dev/null > Packages.gz
gzip -d Packages.gz
apt-ftparchive packages . > Packages
apt-ftparchive sources . > Sources
提示:Ubuntu 26.04 默认包含
multiverse组件,这是受限软件集合。
第四步:配置 Nginx 对外提供服务
安装和启动 Nginx
# 启动 Nginx 并设置开机自启
systemctl enable nginx --now
配置 Nginx 虚拟主机
创建配置文件 /etc/nginx/sites-available/apt-mirror:
server {
listen 80;
server_name repo.yourdomain.com;
# 仓库根目录
root /var/www/apt-mirror/mirror;
# 开启目录浏览
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
# 访问日志
access_log /var/log/nginx/apt-mirror-access.log;
error_log /var/log/nginx/apt-mirror-error.log;
# 对 deb 文件启用缓存
location ~* \.deb$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
# 对索引文件启用缓存
location ~* \.(gz|bz2|xz)$ {
expires 30d;
add_header Cache-Control "public";
}
# 允许所有内网访问
location / {
allow 192.168.0.0/16;
allow 10.0.0.0/8;
allow 172.16.0.0/12;
deny all;
}
}
启用配置:
# 创建软链接
ln -s /etc/nginx/sites-available/apt-mirror /etc/nginx/sites-enabled/
# 检查配置语法
nginx -t
# 重载配置
systemctl reload nginx
防火墙配置
# 开放 HTTP 端口
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
第五步:客户端配置
在内网其他 Ubuntu 服务器上,编辑 /etc/apt/sources.list:
# 备份原配置
cp /etc/apt/sources.list /etc/apt/sources.list.backup
# 编辑配置文件
vim /etc/apt/sources.list
配置内容(根据同步的版本选择)
# Ubuntu 26.04 LTS - noble (稳定版)
deb http://repo.yourdomain.com/ubuntu noble main restricted universe multiverse
deb http://repo.yourdomain.com/ubuntu noble-updates main restricted universe multiverse
deb http://repo.yourdomain.com/ubuntu noble-backports main restricted universe multiverse
deb http://repo.yourdomain.com/ubuntu noble-security main restricted universe multiverse
# 或者使用阿里云镜像
# deb http://mirrors.aliyun.com/ubuntu noble main restricted universe multiverse
# deb http://mirrors.aliyun.com/ubuntu noble-updates main restricted universe multiverse
# deb http://mirrors.aliyun.com/ubuntu noble-backports main restricted universe multiverse
# deb http://mirrors.aliyun.com/ubuntu noble-security main restricted universe multiverse
# Ubuntu 24.04 LTS - jammy
deb http://repo.yourdomain.com/ubuntu jammy main restricted universe multiverse
deb http://repo.yourdomain.com/ubuntu jammy-updates main restricted universe multiverse
deb http://repo.yourdomain.com/ubuntu jammy-backports main restricted universe multiverse
deb http://repo.yourdomain.com/ubuntu jammy-security main restricted universe multiverse
# Ubuntu 22.04 LTS - focal
deb http://repo.yourdomain.com/ubuntu focal main restricted universe multiverse
deb http://repo.yourdomain.com/ubuntu focal-updates main restricted universe multiverse
deb http://repo.yourdomain.com/ubuntu focal-backports main restricted universe multiverse
deb http://repo.yourdomain.com/ubuntu focal-security main restricted universe multiverse
更新软件包列表
# 清理缓存
apt clean
# 重建缓存
apt update
# 验证源是否生效
apt update
测试安装
# 安装一个常用软件测试
apt install -y vim
第六步:使用 reprepro 定制化同步(可选)
如果需要更精细的控制,可以使用 reprepro。
安装 reprepro
apt install -y reprepro
创建仓库结构
# 创建仓库根目录
mkdir -p /var/www/ubuntu-repo/conf
# 初始化仓库
cd /var/www/ubuntu-repo
reprepro -b . export
配置仓库
编辑 /var/www/ubuntu-repo/conf/distributions:
# noble 仓库
Origin: Your Ubuntu Repository
Label: Your Ubuntu Repository
Suite: noble
Codename: noble
Architectures: amd64 i386 source
Components: main restricted universe multiverse
Description: Local Ubuntu 26.04 Repository
# noble-updates 仓库
Origin: Your Ubuntu Repository
Label: Your Ubuntu Repository
Suite: noble-updates
Codename: noble-updates
Architectures: amd64 i386 source
Components: main restricted universe multiverse
Description: Local Ubuntu 26.04 Updates Repository
# noble-security 仓库
Origin: Your Ubuntu Repository
Label: Your Ubuntu Repository
Suite: noble-security
Codename: noble-security
Architectures: amd64 i386 source
Components: main restricted universe multiverse
Description: Local Ubuntu 26.04 Security Repository
# jammy 仓库
Origin: Your Ubuntu Repository
Label: Your Ubuntu Repository
Suite: jammy
Codename: jammy
Architectures: amd64 i386 source
Components: main restricted universe multiverse
Description: Local Ubuntu 24.04 Repository
添加软件包
# 从本地目录添加软件包
reprepro includedeb noble /path/to/package.deb
# 从文件系统递归添加所有 deb 包
reprepro includedeb noble /var/www/apt-mirror/mirror/archive.ubuntu.com/ubuntu/pool
生成索引
# 生成所有索引
reprepro export
客户端配置
客户端 /etc/apt/sources.list:
deb http://repo.yourdomain.com noble main restricted universe multiverse
deb http://repo.yourdomain.com noble-updates main restricted universe multiverse
deb http://repo.yourdomain.com noble-security main restricted universe multiverse
第七步:配置定时同步
使用 cron 设置定时同步任务:
# 编辑 crontab
crontab -e
添加以下内容:
# 每天凌晨 3 点同步(使用阿里云镜像)
0 3 * * * /usr/bin/apt-mirror /etc/apt/mirror.list >> /var/log/apt-mirror.log 2>&1
# 每周日凌晨 2 点同步(使用官方源)
0 2 * * 0 /usr/bin/apt-mirror /etc/apt/mirror.list >> /var/log/apt-mirror.log 2>&1
同步脚本优化
创建 /usr/local/bin/apt-mirror-sync.sh:
#!/bin/bash
#
# Ubuntu 软件源自动同步脚本
#
set -e
LOG_FILE="/var/log/apt-mirror-sync.log"
REPO_BASE="/var/www/apt-mirror"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
log "========== 开始同步任务 =========="
# 执行同步
log "执行 apt-mirror 同步..."
/usr/bin/apt-mirror /etc/apt/mirror.list
# 生成索引
log "生成索引文件..."
cd "${REPO_BASE}/mirror/archive.ubuntu.com/ubuntu"
dpkg-scanpackages . /dev/null > Packages.gz
gzip -d Packages.gz
apt-ftparchive packages . > Packages
apt-ftparchive sources . > Sources
cd "../noble-security"
dpkg-scanpackages . /dev/null > Packages.gz
gzip -d Packages.gz
apt-ftparchive packages . > Packages
apt-ftparchive sources . > Sources
# 清理旧文件(可选)
log "清理旧文件..."
find "${REPO_BASE}/mirror" -type f -name "*.deb~" -delete
find "${REPO_BASE}/mirror" -type f -name "*.deb~" -delete
log "========== 同步任务结束 =========="
设置脚本权限:
chmod +x /usr/local/bin/apt-mirror-sync.sh
第八步:监控与维护
检查磁盘空间
# 查看仓库占用空间
du -sh /var/www/apt-mirror/mirror/archive.ubuntu.com/*
# 设置磁盘告警
df -h /var/www/apt-mirror
查看同步日志
tail -f /var/log/apt-mirror-sync.log
查看客户端访问统计
# 查看最近访问的客户端 IP
awk '{print $1}' /var/log/nginx/apt-mirror-access.log | sort | uniq -c | sort -rn | head -10
# 查看最常请求的 deb 包
awk '{print $7}' /var/log/nginx/apt-mirror-access.log | grep '\.deb$' | sort | uniq -c | sort -rn | head -10
更新软件包列表
# 在客户端执行
apt update
检查源配置
# 检查源是否可访问
curl http://repo.yourdomain.com/ubuntu/dists/noble/Release
curl http://repo.yourdomain.com/ubuntu/dists/noble-updates/Release
curl http://repo.yourdomain.com/ubuntu/dists/noble-security/Release
检查 GPG 密钥
# 导入 Ubuntu GPG 密钥
curl -fsSL https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x843938DF228D22F7B3742BC0D94AA3F0EFE21092 | gpg --dearmor -o /usr/share/keyrings/ubuntu-archive-keyring.gpg
# 在客户端配置 GPG 密钥
echo "deb [signed-by=/usr/share/keyrings/ubuntu-archive-keyring.gpg] http://repo.yourdomain.com/ubuntu noble main restricted universe multiverse" > /etc/apt/sources.list.d/local.list
第九步:常见问题与排错
Q1:apt-mirror 同步失败
# 检查网络连接
ping archive.ubuntu.com
# 检查 apt-mirror 配置文件
cat /etc/apt/mirror.list
# 手动执行 apt-mirror 查看错误信息
apt-mirror
Q2:客户端提示 “Unable to locate package”
# 检查 sources.list 配置
cat /etc/apt/sources.list
# 确认源地址正确
curl http://repo.yourdomain.com/ubuntu/dists/noble/Release
# 重建缓存
apt clean
apt update
Q3:索引文件不正确
# 删除旧的索引
rm -rf /var/www/apt-mirror/mirror/archive.ubuntu.com/ubuntu/dists/*
rm -rf /var/www/apt-mirror/mirror/archive.ubuntu.com/ubuntu/pool/*
# 重新生成索引
cd /var/www/apt-mirror/mirror/archive.ubuntu.com/ubuntu
dpkg-scanpackages . /dev/null > Packages.gz
gzip -d Packages.gz
apt-ftparchive packages . > Packages
apt-ftparchive sources . > Sources
Q4:GPG 密钥验证失败
# 导入 Ubuntu GPG 密钥
curl -fsSL https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x843938DF228D22F7B3742BC0D94AA3F0EFE21092 | gpg --dearmor -o /usr/share/keyrings/ubuntu-archive-keyring.gpg
# 在客户端配置 GPG 密钥
echo "deb [signed-by=/usr/share/keyrings/ubuntu-archive-keyring.gpg] http://repo.yourdomain.com/ubuntu noble main restricted universe multiverse" > /etc/apt/sources.list.d/local.list
Q5:同步速度太慢
# 方案一:使用国内镜像源
# 修改 /etc/apt/mirror.list,将 archive.ubuntu.com 替换为 mirrors.aliyun.com
# 方案二:调整并发线程数
# 在 /etc/apt/mirror.list 中设置
set nthreads 20
# 方案三:限制带宽
# 在 apt-mirror 配置中添加
set limit 100M
第十步:高级配置
使用 rsync 加速同步
# 安装 rsync
apt install -y rsync
# 使用 rsync 同步
rsync -avzP --delete \
rsync://archive.ubuntu.com/ubuntu/ \
/var/www/apt-mirror/mirror/archive.ubuntu.com/ubuntu/
使用 debmirror 高级定制
# 安装 debmirror
apt install -y debmirror
# 配置 debmirror
debmirror /var/www/apt-mirror/mirror \
--host=archive.ubuntu.com \
--root=/ubuntu \
--dist=noble,noble-updates,noble-security \
--components=main,restricted,universe,multiverse \
--arch=amd64,i386,source \
--method=http \
--getcontents \
--postcleanup
添加第三方仓库
# 添加 Docker 仓库
deb http://repo.yourdomain.com/ubuntu docker noble main
# 添加 Node.js 仓库
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
# 添加后编辑 sources.list
# 添加 Kubernetes 仓库
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | gpg --dearmor -o /usr/share/keyrings/kubernetes-apt-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /" > /etc/apt/sources.list.d/kubernetes.list
配置 HTTPS(可选)
# 安装 Certbot
apt install -y certbot python3-certbot-nginx
# 获取 SSL 证书
certbot --nginx -d repo.yourdomain.com
# Nginx 将自动配置 HTTPS
总结
通过本教程,你已经可以在 Ubuntu 26.04 上搭建一个功能完整的软件同步源。主要要点回顾:
- apt-mirror 适合完整镜像,配置简单
- reprepro 适合定制化,功能强大
- Nginx 提供高效的 HTTP 服务
- 定时任务 保证仓库持续更新
- GPG 密钥 确保软件包安全性
搭建完成后,内网所有 Ubuntu 服务器都可以从本地源高速安装和更新软件包,大大提升运维效率。
延伸阅读
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
















暂无评论内容