Ubuntu 20.04 LTS与22.04 LTS 内网apt源搭建

搭建说明

采用官方提供apt-mirror方式进行搭建
由于单个发行版apt包磁盘占用600G左右,建议1T磁盘空间

部署环境

名称 ip 主机名 配置 操作系统
apt内网源服务器 192.168.0.201 CT1 1C 2G 1T Ubuntu 20.04 LTS
内网客户端 192.168.0.202 CT2 1C 1G 50G Ubuntu 20.04 LTS

阿里云apt同步源

国内apt下载下载较慢,设置阿里云apt同步源
备份 sources.list

cp /etc/apt/sources.list /etc/apt/sources.list-bak

Ubuntu 20.04 LTS 配置修改如下

cat > /etc/apt/sources.list <<EOF
deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

# deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
EOF

apt-mirror安装

安装

更新apt 索引与安装

apt update
apt install apt-mirror -y

配置文件

apt-mirror默认配置文件: /etc/apt/mirror.list
修改默认路径: (可选,目录可不创建)
set base_path /data/apt-mirror
set run_postmirror 0
使用阿里云apt同步源
可以设置多个,当前同步focal版本

vim /etc/apt/mirror.list

############# config ##################
#
# set base_path    /var/spool/apt-mirror
set base_path    /data/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  
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set run_postmirror 0
set nthreads     20
set _tilde 0
#
############# end config ##############
deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

# deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse


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

若要添加22.04 TLS版本,在 /etc/apt/mirror.list配置文件新增

deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse

# deb https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

运行同步

apt-mirror

等待执行完毕(首次执行耗时较长,可放入后台执行)
Ubuntu 20.04 LTS与22.04 LTS 内网apt源搭建_第1张图片

目录结构

同步目录结构如下
Ubuntu 20.04 LTS与22.04 LTS 内网apt源搭建_第2张图片
mirror目录中包含了同步源的名称

root@CT1:/data/apt-mirror# ll mirror/
total 0
drwxr-x--- 3 root root 32 Jun  9 16:08 ./
drwxr-xr-x 5 root root 87 Jun  9 23:20 ../
drwxr-x--- 3 root root 20 Jun  9 16:08 mirrors.aliyun.com/

需要代理的目录为当前同步源下/ubuntu目录

root@CT1:/data/apt-mirror# ll mirror/mirrors.aliyun.com/ubuntu/
total 0
drwxr-x--- 4 root root 31 Jun  9 18:36 ./
drwxr-x--- 3 root root 20 Jun  9 16:08 ../
drwxr-x--- 6 root root 85 Jun  9 18:36 dists/
drwxr-x--- 6 root root 70 Jun  9 18:09 pool/

代理访问

配置

使用nginx docker-compose方式进行代理
安装docker与docker-compose,也可使用apt方式安装nginx

vim docker-compose.yaml
version: '3'
services:
  apt-mirror:   
    image: nginx:1.24.0-alpine
    container_name: apt-mirror
    restart: always
    ports:
      - '80:80'
    volumes:
      - /data/apt-mirror/mirror/mirrors.aliyun.com/ubuntu:/usr/share/nginx/html

启动

docker-compose up -d 

客户机验证

配置

配置内网主机使用搭建的apt源进行更新
备份 sources.list

cp /etc/apt/sources.list /etc/apt/sources.list-bak

修改配置

cat > /etc/apt/sources.list << EOF
deb http://192.168.0.201/ubuntu/ focal main restricted universe multiverse
deb-src http://192.168.0.201/ubuntu/ focal main restricted universe multiverse

deb http://192.168.0.201/ubuntu/ focal-security main restricted universe multiverse
deb-src http://192.168.0.201/ubuntu/ focal-security main restricted universe multiverse

deb http://192.168.0.201/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://192.168.0.201/ubuntu/ focal-updates main restricted universe multiverse

deb http://192.168.0.201/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://192.168.0.201/ubuntu/ focal-backports main restricted universe multiverse
EOF

更新apt索引

apt update
Reading package lists... Done
E: Failed to fetch http://192.168.0.201/ubuntu/dists/focal/main/cnf/Commands-amd64  404  Not Found [IP: 192.168.0.201]
E: Failed to fetch http://192.168.0.201/ubuntu/dists/focal-security/main/cnf/Commands-amd64  404  Not Found [IP: 192.168.0.201]
E: Failed to fetch http://192.168.0.201/ubuntu/dists/focal-updates/main/cnf/Commands-amd64  404  Not Found [IP: 192.168.0.201]
E: Failed to fetch http://192.168.0.201/ubuntu/dists/focal-backports/main/cnf/Commands-amd64  404  Not Found [IP: 192.168.0.201]
E: Some index files failed to download. They have been ignored, or old ones used instead.
root@CT1:/data/apt-sources# 

报错处理

apt源缺少cnf下的一些索引文件,导致更新失败
经查找ubuntu官网apt-mirror软件包信息,apt-mirror_0.5.4-2已修复该问题
Ubuntu 20.04 LTS与22.04 LTS 内网apt源搭建_第3张图片
官网下载连接:https://packages.ubuntu.com/zh-tw/mantic/all/apt-mirror/download

回到apt内网源服务器

查看当前安装包版本
root@CT1:~# dpkg -l|grep apt-mirror
ii  apt-mirror                            0.5.4-1                           all          APT sources mirroring tool

安装apt-mirror_0.5.4-2版本
使用阿里云仓库下载:

wget https://mirrors.aliyun.com/ubuntu/pool/universe/a/apt-mirror/apt-mirror_0.5.4-2_all.deb

安装

dpkg -i apt-mirror_0.5.4-2_all.deb

执行apt-mirror进行同步

apt-mirror

apt内网源服务器更新完毕后,在apt客户端再次进行更新,提示成功

apt update

你可能感兴趣的:(ubuntu,linux,服务器)