Debian 搭建软件同步源完整教程
前言
在企业内网或实验室环境中,多台 Debian 服务器需要安装和更新软件包时,如果每台都从外网下载,不仅速度慢,还浪费带宽。搭建一个本地软件同步源(镜像站),可以让所有内网服务器从本地高速获取软件包,同时还能控制软件版本、提高安全性。
本教程将详细介绍如何在 Debian 上搭建一个完整的软件同步源,包括官方仓库和第三方仓库的同步。
环境说明
- 操作系统:Debian 12 (Bookworm) / 11 (Bullseye)
- 同步方式:
apt-mirror(适合完整镜像) /reprepro(适合定制化) - Web 服务:Nginx
- 存储空间:建议至少 200GB(完整镜像需要 500GB+)
方案选择
| 方案 | 适用场景 | 优点 | 缺点 |
|---|---|---|---|
| apt-mirror | 完整镜像 | 配置简单,自动化程度高 | 占用空间大,定制性较差 |
| reprepro | 定制化同步 | 灵活,可精细控制 | 配置复杂 |
| rsync + debmirror | 高级定制 | 速度快,功能强大 | 学习成本高 |
本教程以 apt-mirror 方案为主,兼顾 reprepro 方案。
第一步:安装必要软件包
# 安装 apt-mirror 和相关工具
apt update
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://deb.debian.org/debian
# 设置镜像源列表
# Debian 官方源(主源)
deb http://deb.debian.org/debian bookworm main contrib non-free non-free-firmware
deb http://deb.debian.org/debian bookworm-updates main contrib non-free non-free-firmware
deb http://deb.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
# Debian 官方源(测试版,可选)
# deb http://deb.debian.org/debian testing main contrib non-free non-free-firmware
# Debian 官方源(稳定版,可选)
# deb http://deb.debian.org/debian stable main contrib non-free non-free-firmware
# Debian 官方源(旧版本,可选)
# deb http://deb.debian.org/debian bullseye main contrib non-free non-free-firmware
# Aliyun 镜像源(国内速度快)
# deb http://mirrors.aliyun.com/debian bookworm main contrib non-free non-free-firmware
# deb http://mirrors.aliyun.com/debian bookworm-updates main contrib non-free non-free-firmware
# deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware
# 163 镜像源
# deb http://mirrors.163.com/debian/ bookworm main contrib non-free non-free-firmware
# deb http://mirrors.163.com/debian/ bookworm-updates main contrib non-free non-free-firmware
# deb http://mirrors.163.com/debian-security bookworm-security main contrib non-free non-free-firmware
# 设置下载选项
set nthreads 10
set _tilde 0
配置说明
set base_path:镜像根目录set mirror_path:实际下载的软件包目录set skel_path:模板目录set var_path:日志和临时文件目录deb http://...:指定要同步的软件源,格式为deb URL dist componentnthreads:并发下载线程数,根据网络带宽调整_tilde:是否使用~进行压缩(0 表示不使用)
第三步:开始同步
首次同步
# 执行同步(首次同步时间较长,可能需要数小时)
apt-mirror
同步过程会:
1. 连接上游镜像源
2. 下载软件包列表
3. 下载所有软件包文件
4. 生成索引文件
同步过程监控
# 监控下载进度
watch -n 5 'du -sh /var/www/apt-mirror/mirror/deb.debian.org'
# 查看同步日志
tail -f /var/www/apt-mirror/var/log/apt-mirror.log
同步完成后
# 生成索引文件
cd /var/www/apt-mirror/mirror/deb.debian.org/debian
dpkg-scanpackages . /dev/null > Packages.gz
gzip -d Packages.gz
apt-ftparchive packages . > Packages
apt-ftparchive sources . > Sources
# 为其他源也生成索引
cd ../debian-security
dpkg-scanpackages . /dev/null > Packages.gz
gzip -d Packages.gz
apt-ftparchive packages . > Packages
apt-ftparchive sources . > Sources
提示:从 Debian 12 开始,默认包含
non-free-firmware组件,这是固件包的新分类。
第四步:配置 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
第五步:客户端配置
在内网其他 Debian 服务器上,编辑 /etc/apt/sources.list:
# 备份原配置
cp /etc/apt/sources.list /etc/apt/sources.list.backup
# 编辑配置文件
vim /etc/apt/sources.list
配置内容(根据同步的版本选择)
# 官方源 - bookworm(稳定版)
deb http://repo.yourdomain.com/debian bookworm main contrib non-free non-free-firmware
deb http://repo.yourdomain.com/debian bookworm-updates main contrib non-free non-free-firmware
deb http://repo.yourdomain.com/debian-security bookworm-security main contrib non-free non-free-firmware
# 或者使用阿里云镜像
# deb http://mirrors.aliyun.com/debian bookworm main contrib non-free non-free-firmware
# deb http://mirrors.aliyun.com/debian bookworm-updates main contrib non-free non-free-firmware
# deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware
# 测试版
# deb http://repo.yourdomain.com/debian testing main contrib non-free non-free-firmware
# deb http://repo.yourdomain.com/debian testing-updates main contrib non-free non-free-firmware
# 旧版本(如 bullseye)
# deb http://repo.yourdomain.com/debian bullseye main contrib non-free non-free-firmware
# deb http://repo.yourdomain.com/debian bullseye-updates main contrib non-free non-free-firmware
更新软件包列表
# 清理缓存
apt clean
# 重建缓存
apt update
# 验证源是否生效
apt update
测试安装
# 安装一个常用软件测试
apt install -y vim
第六步:使用 reprepro 定制化同步(可选)
如果需要更精细的控制,可以使用 reprepro。
安装 reprepro
apt install -y reprepro
创建仓库结构
# 创建仓库根目录
mkdir -p /var/www/debian-repo/conf
# 初始化仓库
cd /var/www/debian-repo
reprepro -b . export
配置仓库
编辑 /var/www/debian-repo/conf/distributions:
# 主仓库
Origin: Your Repository
Label: Your Repository
Suite: stable
Codename: bookworm
Architectures: amd64 i386 source
Components: main contrib non-free non-free-firmware
Description: Local Debian Repository
# 更新仓库
Origin: Your Repository
Label: Your Repository
Suite: updates
Codename: bookworm-updates
Architectures: amd64 i386 source
Components: main contrib non-free non-free-firmware
Description: Local Debian Updates Repository
# 安全更新仓库
Origin: Your Repository
Label: Your Repository
Suite: security
Codename: bookworm-security
Architectures: amd64 i386 source
Components: main contrib non-free non-free-firmware
Description: Local Debian Security Repository
添加软件包
# 从本地目录添加软件包
reprepro includedeb bookworm /path/to/package.deb
# 从文件系统递归添加所有 deb 包
reprepro includedeb bookworm /var/www/apt-mirror/mirror/deb.debian.org/debian/pool
生成索引
# 生成所有索引
reprepro export
客户端配置
客户端 /etc/apt/sources.list:
deb http://repo.yourdomain.com bookworm main contrib non-free non-free-firmware
deb http://repo.yourdomain.com bookworm-updates main contrib non-free non-free-firmware
deb http://repo.yourdomain.com bookworm-security main contrib non-free non-free-firmware
第七步:配置定时同步
使用 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
#
# Debian 软件源自动同步脚本
#
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/deb.debian.org/debian"
dpkg-scanpackages . /dev/null > Packages.gz
gzip -d Packages.gz
apt-ftparchive packages . > Packages
apt-ftparchive sources . > Sources
cd "../debian-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/*
# 设置磁盘告警
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/debian/dists/bookworm/Release
curl http://repo.yourdomain.com/debian/dists/bookworm-updates/Release
curl http://repo.yourdomain.com/debian/dists/bookworm-security/Release
第九步:常见问题与排错
Q1:apt-mirror 同步失败
# 检查网络连接
ping deb.debian.org
# 检查 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/debian/dists/bookworm/Release
# 重建缓存
apt clean
apt update
Q3:索引文件不正确
# 删除旧的索引
rm -rf /var/www/apt-mirror/mirror/deb.debian.org/debian/dists/*
rm -rf /var/www/apt-mirror/mirror/deb.debian.org/debian/pool/*
# 重新生成索引
cd /var/www/apt-mirror/mirror/deb.debian.org/debian
dpkg-scanpackages . /dev/null > Packages.gz
gzip -d Packages.gz
apt-ftparchive packages . > Packages
apt-ftparchive sources . > Sources
Q4:GPG 密钥验证失败
# 导入 Debian GPG 密钥
curl -fsSL https://deb.debian.org/debian/archive-key-11.asc | gpg --dearmor -o /usr/share/keyrings/debian-archive-keyring.gpg
curl -fsSL https://deb.debian.org/debian/archive-key-12.asc | gpg --dearmor -o /usr/share/keyrings/debian-archive-keyring-12.gpg
# 在客户端配置 GPG 密钥
echo "deb [signed-by=/usr/share/keyrings/debian-archive-keyring.gpg] http://repo.yourdomain.com/debian bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list.d/local.list
Q5:同步速度太慢
# 方案一:使用国内镜像源
# 修改 /etc/apt/mirror.list,将 deb.debian.org 替换为 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://deb.debian.org/debian/ \
/var/www/apt-mirror/mirror/deb.debian.org/debian/
使用 debmirror 高级定制
# 安装 debmirror
apt install -y debmirror
# 配置 debmirror
debmirror /var/www/apt-mirror/mirror \
--host=deb.debian.org \
--root=/debian \
--dist=bookworm,bookworm-updates,bookworm-security \
--components=main,contrib,non-free,non-free-firmware \
--arch=amd64,i386,source \
--method=http \
--getcontents \
--postcleanup
添加第三方仓库
# 添加 Docker 仓库
deb http://repo.yourdomain.com/debian docker bookworm main
# 添加 Node.js 仓库
deb https://deb.nodesource.com/node_20.x bookworm main
# 添加 Kubernetes 仓库
deb https://pkgs.k8s.io/core:/stable:/v1.28/deb/ / main
总结
通过本教程,你已经可以在 Debian 上搭建一个功能完整的软件同步源。主要要点回顾:
- apt-mirror 适合完整镜像,配置简单
- reprepro 适合定制化,功能强大
- Nginx 提供高效的 HTTP 服务
- 定时任务 保证仓库持续更新
- GPG 密钥 确保软件包安全性
搭建完成后,内网所有 Debian 服务器都可以从本地源高速安装和更新软件包,大大提升运维效率。
延伸阅读
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END














暂无评论内容