搭建本地 apt 源

发布时间:2026/6/14 22:20:37

搭建本地 apt 源 搭建本地 Ubuntu APT 源类似阿里云或中科大镜像站的模式。这通常有两种方案方案一使用apt-mirror完整镜像推荐适合需要完整镜像官方仓库的场景。1. 安装 apt-mirrorsudoaptupdatesudoaptinstallapt-mirror apache22. 配置镜像列表编辑/etc/apt/mirror.listsudotee/etc/apt/mirror.listEOF ############# 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.confEOF 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-mirrorEOF # 每天凌晨 2 点同步 0 2 * * * root /usr/bin/apt-mirror /var/log/apt-mirror.log 21 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/distributionscat/var/www/html/apt/conf/distributionsEOF 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# 选择 RSA4096位设置姓名/邮箱# 导出公钥给客户端使用gpg--armor--exportyour-emailexample.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. 配置 Apachesudotee/etc/apache2/sites-available/local-apt.confEOF 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.listEOF # 本地完整镜像方案一 deb [archamd64] http://your-server-ip/ubuntu/archive noble main restricted universe multiverse deb [archamd64] http://your-server-ip/ubuntu/archive noble-updates main restricted universe multiverse deb [archamd64] http://your-server-ip/ubuntu/archive noble-security main restricted universe multiverse # 或者本地自建仓库方案二 # deb [archamd64 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如果需要完整内网镜像类似阿里云选方案一如果只需要托管自定义软件包选方案二更轻量。

相关新闻