ubuntu镜像源

Ubuntu 22.04 LTS 离线镜像源制作指南

制作Ubuntu 22.04的离线镜像源可以让您在无网络环境中部署多台机器,或为内部网络提供快速软件安装服务。以下是完整的制作流程:

方法一:使用apt-mirror(推荐)

1. 准备环境

# 在具有充足存储空间(至少200GB)的服务器上操作
sudo apt update
sudo apt install -y apt-mirror apache2

2. 配置apt-mirror

编辑配置文件:

sudo nano /etc/apt/mirror.list

替换为以下内容(以阿里云镜像为例):

############# config ##################
set base_path    /var/spool/apt-mirror
set nthreads     20
set _tilde 0
############# end config ##############

deb-amd64 http://mirrors.aliyun.com/ubuntu jammy main restricted universe multiverse
deb-amd64 http://mirrors.aliyun.com/ubuntu jammy-security main restricted universe multiverse
deb-amd64 http://mirrors.aliyun.com/ubuntu jammy-updates main restricted universe multiverse
deb-amd64 http://mirrors.aliyun.com/ubuntu jammy-proposed main restricted universe multiverse
deb-amd64 http://mirrors.aliyun.com/ubuntu jammy-backports main restricted universe multiverse

clean http://mirrors.aliyun.com/ubuntu

3. 开始同步镜像

sudo apt-mirror

此过程可能需要数小时(取决于网络速度和镜像大小)

4. 配置Web服务器

# 创建符号链接
sudo ln -s /var/spool/apt-mirror/mirror/mirrors.aliyun.com/ubuntu /var/www/html/ubuntu

# 设置权限
sudo chmod -R 755 /var/www/html/ubuntu

5. 在客户端配置源

# 备份原配置
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

# 创建新配置
sudo tee /etc/apt/sources.list <<EOF
deb [arch=amd64] http://<镜像服务器IP>/ubuntu jammy main restricted universe multiverse
deb [arch=amd64] http://<镜像服务器IP>/ubuntu jammy-security main restricted universe multiverse
deb [arch=amd64] http://<镜像服务器IP>/ubuntu jammy-updates main restricted universe multiverse
deb [arch=amd64] http://<镜像服务器IP>/ubuntu jammy-proposed main restricted universe multiverse
deb [arch=amd64] http://<镜像服务器IP>/ubuntu jammy-backports main restricted universe multiverse
EOF

# 更新源
sudo apt update

方法二:使用debmirror(更灵活)

1. 安装必要工具

sudo apt install -y debmirror

2. 创建同步脚本

nano mirror-ubuntu.sh

脚本内容:

#!/bin/bash

MIRROR_DIR="/data/ubuntu-mirror"
MIRROR_URL="http://mirrors.aliyun.com/ubuntu"
DIST="jammy"
SECTIONS="main,restricted,universe,multiverse"
ARCH="amd64"

debmirror \
    --verbose \
    --progress \
    --method=http \
    --host=mirrors.aliyun.com \
    --root=:ubuntu \
    --dist=$DIST \
    --section=$SECTIONS \
    --arch=$ARCH \
    --no-check-gpg \
    --nosource \
    --diff=mirror \
    --cleanup \
    $MIRROR_DIR

3. 运行同步

chmod +x mirror-ubuntu.sh
sudo ./mirror-ubuntu.sh

4. 配置Nginx服务

sudo apt install -y nginx
sudo tee /etc/nginx/sites-available/ubuntu-mirror <<EOF
server {
    listen 80;
    server_name _;
    root /data/ubuntu-mirror;
    autoindex on;
    location / {
        try_files \$uri \$uri/ =404;
    }
}
EOF

sudo ln -s /etc/nginx/sites-available/ubuntu-mirror /etc/nginx/sites-enabled/
sudo systemctl restart nginx

方法三:使用Docker容器(简化部署)

1. 创建docker-compose.yml

version: '3'
services:
  apt-mirror:
    image: instrumentisto/apt-mirror
    container_name: ubuntu-mirror
    volumes:
      - ./mirror-data:/var/spool/apt-mirror
      - ./mirror.conf:/etc/apt/mirror.list
    ports:
      - "80:80"
    command: sh -c "apt-mirror && cp -r /var/spool/apt-mirror/mirror/* /var/www/html && apache2-foreground"

2. 创建镜像配置文件

nano mirror.conf

内容同apt-mirror配置

3. 启动镜像服务

docker-compose up -d

镜像维护与更新

1. 定期更新

# 对于apt-mirror
sudo apt-mirror

# 对于debmirror
sudo ./mirror-ubuntu.sh

2. 清理旧版本

# 清理90天前的旧版本
sudo find /path/to/mirror/pool -type f -mtime +90 -delete

3. 自动更新脚本

#!/bin/bash
LOG_FILE="/var/log/mirror-update.log"
echo "$(date) - Starting mirror update" >> $LOG_FILE

apt-mirror >> $LOG_FILE 2>&1

# 清理旧包
find /var/spool/apt-mirror/mirror -name '*.deb' -mtime +30 -delete >> $LOG_FILE 2>&1

echo "$(date) - Mirror update completed" >> $LOG_FILE

客户端使用技巧

1. 自动选择最快镜像

sudo apt install -y netselect-apt
sudo netselect-apt -s -n jammy

2. 仅下载软件包不安装

sudo apt-get install --download-only <package-name>

注意事项

  1. 存储空间

    • 完整镜像需要约250GB空间
    • 最小化镜像(仅常用包)约50GB
    • 建议使用LVM以便扩展
  2. 网络要求

    • 首次同步需要高速稳定网络
    • 后续更新约每天1-5GB流量
  3. 安全考虑

    • 在内部网络部署时启用HTTPS
    • 定期验证镜像完整性
    • 使用GPG签名验证
  4. 架构支持

    • 添加多架构支持:在mirror.list中添加 deb-i386
    • 添加ARM支持:--arch=amd64,arm64
  5. 定制内容

    • 添加第三方PPA源
    • 包含ISO镜像文件
    • 添加专有驱动仓库

通过以上方法,您可以构建一个完整的Ubuntu 22.04离线镜像源,支持大规模部署和离线环境下的软件安装需求。

posted @ 2025-07-13 16:46  mofy  阅读(95)  评论(0)    收藏  举报