搭建本地 apt 源
搭建本地 Ubuntu APT 源,类似阿里云或中科大镜像站的模式。这通常有两种方案:
方案一:使用apt-mirror完整镜像(推荐)
适合需要完整镜像官方仓库的场景。
1. 安装 apt-mirror
sudoaptupdatesudoaptinstallapt-mirror apache22. 配置镜像列表
编辑/etc/apt/mirror.list:
sudotee/etc/apt/mirror.list<<'EOF' ############# config ################## # set base_path /var/spool/apt-mirror # # 镜像存放路径 set mirror_path $base_path/mirror set skel_path $base_path/skel set var_path $base_path/var set cleanscript $var_path/clean.sh set defaultarch amd64 set postmirror_script $var_path/postmirror.sh set run_postmirror 0 set nthreads 20 set _tilde 0 # ############# end config ############## # Ubuntu 24.04 (noble) 主仓库 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://security.ubuntu.com/ubuntu noble-security main restricted universe multiverse # 如需镜像源码,取消注释: # deb-src http://archive.ubuntu.com/ubuntu noble main restricted universe multiverse # 清理配置 clean http://archive.ubuntu.com/ubuntu clean http://security.ubuntu.com/ubuntu EOF3. 开始同步(首次需数小时至数天)
sudoapt-mirror数据将下载到/var/spool/apt-mirror/mirror/archive.ubuntu.com/ubuntu/。
4. 配置 Web 服务(Apache2)
# 创建软链接到 Apache 根目录sudomkdir-p/var/www/html/ubuntusudoln-s/var/spool/apt-mirror/mirror/archive.ubuntu.com/ubuntu /var/www/html/ubuntu/archive# 或者配置 Apache 虚拟主机sudotee/etc/apache2/sites-available/apt-mirror.conf<<'EOF' <<VirtualHost *:80> ServerName apt.local DocumentRoot /var/spool/apt-mirror/mirror <Directory /var/spool/apt-mirror/mirror> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> # 启用目录列表(方便浏览) <Directory /> Options +Indexes </Directory> ErrorLog ${APACHE_LOG_DIR}/apt-mirror-error.log CustomLog ${APACHE_LOG_DIR}/apt-mirror-access.log combined </VirtualHost> EOFsudoa2ensite apt-mirrorsudosystemctl reload apache25. 设置定时同步(可选)
sudotee/etc/cron.d/apt-mirror<<'EOF' # 每天凌晨 2 点同步 0 2 * * * root /usr/bin/apt-mirror > /var/log/apt-mirror.log 2>&1 EOF方案二:使用reprepro自建小型仓库
适合只托管自定义软件包或精选包的轻量场景。
1. 安装工具
sudoaptinstallreprepro apache2 dpkg-dev2. 创建仓库结构
sudomkdir-p/var/www/html/apt/{conf,dists,incoming,pool}sudochown-R$USER:$USER/var/www/html/apt3. 配置 reprepro
编辑/var/www/html/apt/conf/distributions:
cat>/var/www/html/apt/conf/distributions<<'EOF' Origin: Local APT Repo Label: Local Repo Suite: noble Codename: noble Architectures: amd64 arm64 source Components: main restricted universe multiverse Description: Local Ubuntu 24.04 Repository SignWith: yes EOF4. 生成 GPG 密钥(用于签名)
# 生成密钥(交互式,或使用批处理模式)gpg --full-generate-key# 选择 RSA,4096位,设置姓名/邮箱# 导出公钥给客户端使用gpg--armor--exportyour-email@example.com>/var/www/html/apt/repo.pub5. 添加软件包
# 下载或复制 .deb 包到 incomingcd/var/www/html/apt# 添加包到仓库reprepro includedeb noble /path/to/package.deb# 或者批量添加reprepro includedeb noble incoming/*.deb6. 配置 Apache
sudotee/etc/apache2/sites-available/local-apt.conf<<'EOF' <<VirtualHost *:80> ServerName apt.local DocumentRoot /var/www/html/apt <Directory /var/www/html/apt> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/local-apt-error.log CustomLog ${APACHE_LOG_DIR}/local-apt-access.log combined </VirtualHost> EOFsudoa2ensite local-aptsudosystemctl reload apache2客户端配置(使用自己的源)
1. 创建源列表文件
sudotee/etc/apt/sources.list.d/local-mirror.list<<'EOF' # 本地完整镜像(方案一) deb [arch=amd64] http://your-server-ip/ubuntu/archive noble main restricted universe multiverse deb [arch=amd64] http://your-server-ip/ubuntu/archive noble-updates main restricted universe multiverse deb [arch=amd64] http://your-server-ip/ubuntu/archive noble-security main restricted universe multiverse # 或者本地自建仓库(方案二) # deb [arch=amd64 signed-by=/usr/share/keyrings/local-repo.gpg] http://your-server-ip/apt noble main EOF2. 导入 GPG 密钥(方案二需要)
# 下载并导入公钥wget-qO- http://your-server-ip/apt/repo.pub|sudogpg--dearmor-o/usr/share/keyrings/local-repo.gpg# 更新sudoaptupdate方案对比
| 特性 | apt-mirror完整镜像 | reprepro自建仓库 |
|---|---|---|
| 存储需求 | 数百 GB(完整 Ubuntu 仓库) | 几 MB 起(按需添加) |
| 同步时间 | 首次数小时,增量较快 | 即时生效 |
| 适用场景 | 内网大量机器、无外网环境 | 自定义软件包、小型团队 |
| 维护复杂度 | 中等(需定期同步) | 低 |
| 带宽节省 | 高(本地缓存) | 取决于包数量 |
| 签名验证 | 使用官方签名 | 需自建 GPG 密钥 |
常见问题
磁盘空间不足
# 查看镜像大小du-sh/var/spool/apt-mirror/mirror/# Ubuntu 完整镜像约 1-2TB,建议预留空间# 可只镜像部分组件(如只保留 main)同步失败/断点续传
# apt-mirror 支持断点续传,重新运行即可sudoapt-mirror# 查看日志排查问题tail-f/var/log/apt-mirror.log多版本 Ubuntu 支持
在/etc/apt/mirror.list中添加多行:
deb http://archive.ubuntu.com/ubuntu jammy main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu noble main restricted universe multiverse如果需要完整内网镜像(类似阿里云),选方案一;如果只需要托管自定义软件包,选方案二更轻量。
