当前位置: 首页 > news >正文

搭建本地 apt 源

搭建本地 Ubuntu APT 源,类似阿里云或中科大镜像站的模式。这通常有两种方案:


方案一:使用apt-mirror完整镜像(推荐)

适合需要完整镜像官方仓库的场景。

1. 安装 apt-mirror

sudoaptupdatesudoaptinstallapt-mirror apache2

2. 配置镜像列表

编辑/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 EOF

3. 开始同步(首次需数小时至数天)

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 apache2

5. 设置定时同步(可选)

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-dev

2. 创建仓库结构

sudomkdir-p/var/www/html/apt/{conf,dists,incoming,pool}sudochown-R$USER:$USER/var/www/html/apt

3. 配置 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 EOF

4. 生成 GPG 密钥(用于签名)

# 生成密钥(交互式,或使用批处理模式)gpg --full-generate-key# 选择 RSA,4096位,设置姓名/邮箱# 导出公钥给客户端使用gpg--armor--exportyour-email@example.com>/var/www/html/apt/repo.pub

5. 添加软件包

# 下载或复制 .deb 包到 incomingcd/var/www/html/apt# 添加包到仓库reprepro includedeb noble /path/to/package.deb# 或者批量添加reprepro includedeb noble incoming/*.deb

6. 配置 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 EOF

2. 导入 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

如果需要完整内网镜像(类似阿里云),选方案一;如果只需要托管自定义软件包,选方案二更轻量。

http://www.jsqmd.com/news/1014458/

相关文章:

  • 别再只调solvePnP了!深入对比EPnP、IPPE等6种算法在无人机着陆标志识别中的精度与速度
  • 安能物流200公斤跨省邮寄多少钱?安能物流200公斤跨省运费多少?省钱技巧来了 - 快递物流资讯
  • 技术方案:解决网盘直链下载的跨平台集成挑战
  • 118、【Agent】【OpenCode】项目配置(重复依赖分析)
  • ctf show web入门115
  • 下一代金融数据处理系统:实时订单簿重建技术深度解析
  • Java毕设选题推荐:基于 SpringBoot 的水果商品展示与交易管理系统的设计与实现 生鲜水果线上零售管理平台【附源码、mysql、文档、调试+代码讲解+全bao等】
  • 免费IDM激活脚本完整指南:一键解锁下载加速器
  • 深入解析dex2jar:从Dalvik字节码到Java字节码的专业转换引擎
  • 2026免费视频转MOV在线保姆级教程!无限制工具手把手教学,苹果Final Cut Pro直接导入 - 时时资讯
  • 3DGRUT实战指南:高效高斯粒子光线追踪与栅格化技术深度解析
  • 从写完就发到AI发布策略_CSDN_AI数字营销让内容分发变了什么
  • Java毕设选题推荐:基于 B/S 架构的调查问卷管理系统的设计与实现 基于 Spring Boot 的轻量化问卷采集系统的设计与实现【附源码、mysql、文档、调试+代码讲解+全bao等】
  • Nature 子刊观点:AI 检测让论文写作陷入两难
  • 2026免费音频转AC3在线保姆级教程!无限制工具手把手教学,杜比数字环绕声制作必备 - 时时资讯
  • 2026年怀化手表回收到底该怎么选?给你推荐五家靠谱的(2026年6月14日最新版) - 空空是也
  • 2026免费视频转WMV在线保姆级教程!无限制工具手把手教学,Windows老系统兼容神器 - 时时资讯
  • MPC7450处理器信号接口深度解析:L3缓存、中断与时钟配置实战
  • HackMyVM-Canto
  • 2026免费视频转WEBM在线保姆级教程!无限制工具手把手教学,HTML5现代网页最佳格式 - 时时资讯
  • 如何让Paperless-ngx说你的语言:从中文界面到多语言文档管理
  • 从直播小白到多平台达人:obs-multi-rtmp带你玩转同步直播
  • Deep-Live-Cam:3步实现实时AI换脸,开启移动端深度伪造新纪元
  • 微信社交关系管理神器:3分钟检测谁删了你,告别单向好友烦恼
  • shutil模块
  • 3步实现缠论自动分析:通达信免费插件实战指南
  • 后ChatGPT时代的杀手级应用:会使用工具的AI Agent产品预测
  • 2026免费音频转AMR在线保姆级教程!无限制工具手把手教学,老旧录音笔也能轻松播放 - 时时资讯
  • 如何构建互动桌面宠物:打造响应式Live2D动画系统
  • MPC7450缓存架构与MPX总线设计:从原理到工程实践