Ubuntu 18.04 搭建私有软件镜像源(支持 Ubuntu 和 CentOS)

系统环境为 Ubuntu 18.04,搭建支持 Ubuntu 系和 CentOS 系的双私有软件仓库。
Ubuntu 本地软件镜像源使用 apt-mirror 工具与远程仓库同步,CentOS 本地镜像源使用 reposync 工具与远程仓库同步。
上述两个工具都可以通过 Ubuntu 的包管理器 apt-get 命令直接安装使用。

一、环境准备

我这里搭建了 Ubuntu 18.04、Ubuntu 16.04 和 CentOS7 三个镜像源,从远程仓库拉取的软件包总共耗费了约 300G 硬盘空间,其中两个 Ubuntu 占用了约 261G,CentOS7(包含 epel 源)约 29G 。
创建镜像源前需保证本地有足够的存储空间。

建议给磁盘分区时使用 LVM(逻辑卷管理) 的方式,便于之后对存储空间进行扩容等操作。
关于 Linux 系统磁盘分区和 LVM 的介绍可以参考 Linux 磁盘设备和 LVM 管理命令详解。此处不作赘述。

安装同步工具:
$ sudo apt-get install apt-mirror reposync createrepo

安装 web 服务器(nginx):
$ sudo apt-get install nginx

二、Ubuntu 镜像源搭建

编辑 /etc/apt/mirror.list 配置文件,修改远程仓库的地址为国内的阿里镜像站,本地的 Ubuntu 软件仓库将定期从阿里源同步软件包。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
set nthreads     20
set _tilde 0

set base_path /opt/mirrors/ubuntu
set defaultarch amd64

#Ubuntu 16.04
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
#Ubuntu 18.04
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

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

上述配置文件中 base_path 指定的目录,即本地仓库存放软件包的路径。
$ sudo mkdir -p /opt/mirrors/ubuntu

运行 apt-mirror 命令拉取软件包,同步完成花费的时间视网速而定:
$ sudo apt-mirror

配置 web 服务

镜像源同步完成之后,需配置 web 服务使得本地仓库可以被其他 Linux 机器使用。
编辑 /etc/nginx/sites-available/default 配置文件,开启目录浏览(auto_index)功能:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;

location / {
try_files $uri $uri/ =404;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}

创建软链接,将软件包存储路径指向到 web 目录下:
$ sudo ln -s /opt/mirrors/ubuntu/mirror/mirrors.aliyun.com/ubuntu /var/www/html/ubuntu

重新载入 nginx 服务:$ sudo service nginx reload

此时访问 http://127.0.0.1/ubuntu ,应该可以在 web 界面中浏览本地仓库中的软件包。ubuntu

使用本地镜像源

前面的配置完成后,即可进入任意一台 Ubuntu 主机,配置其镜像源为刚刚创建的本地软件仓库。
编辑 /etc/apt/sources.list 文件,修改镜像源(Ubuntu 18.04):

1
2
3
4
5
deb http://127.0.0.1/ubuntu/ bionic main restricted universe multiverse
deb http://127.0.0.1/ubuntu/ bionic-updates main restricted universe multiverse
deb http://127.0.0.1/ubuntu/ bionic-backports main restricted universe multiverse
deb http://127.0.0.1/ubuntu/ bionic-security main restricted universe multiverse
deb http://127.0.0.1/ubuntu/ bionic-proposed main restricted universe multiverse

运行 sudo apt-get update 命令更新索引文件,之后即可使用 sudo apt-get install 命令从本地仓库中安装软件包了。

效果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ sudo apt-get update
Hit:1 http://127.0.0.1/ubuntu bionic InRelease
Hit:2 http://127.0.0.1/ubuntu bionic-updates InRelease
Hit:3 http://127.0.0.1/ubuntu bionic-backports InRelease
Hit:4 http://127.0.0.1/ubuntu bionic-security InRelease
Hit:5 http://127.0.0.1/ubuntu bionic-proposed InRelease
Reading package lists... Done
$ sudo apt-get install nmap
...
Get:1 http://127.0.0.1/ubuntu bionic/main amd64 libblas3 amd64 3.7.1-4ubuntu1 [140 kB]
Get:2 http://127.0.0.1/ubuntu bionic/main amd64 liblinear3 amd64 2.1.0+dfsg-2 [39.3 kB]
Get:3 http://127.0.0.1/ubuntu bionic-updates/main amd64 liblua5.3-0 amd64 5.3.3-1ubuntu0.18.04.1 [115 kB]
Get:4 http://127.0.0.1/ubuntu bionic/main amd64 nmap amd64 7.60-1ubuntu5 [5174 kB]
Fetched 5467 kB in 1s (9200 kB/s)
...

三、CentOS 镜像源搭建

创建 /opt/mirrors/CentOS-7.repo 配置文件,添加远程软件仓库的地址。这里使用清华大学开源软件镜像站

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[base]
name=CentOS-7 - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-7 - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/7/updates/x86_64/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-7 - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/7/extras/x86_64/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-7 - Plus
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/7/centosplus/x86_64/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=0
enabled=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[epel]
name=Extra Packages for Enterprise Linux 7
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=0
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

此处为了简单起见,gpgcheck 都设置为 0 关闭了 GPG KEY 检查。

创建软件仓库本地路径:$ sudo mkdir -p /opt/mirrors/centos

拉取远程仓库到本地:$ sudo reposync -np /opt/mirrors/centos -c /opt/mirrors/CentOS-7.repo

同步完成后,/opt/mirrors/centos 路径下会生成如下 4 个文件夹:

1
2
$ ls /opt/mirrors/centos
base epel extras updates

切换到 root 用户,使用 createrepo 命令在上述 4 个文件夹中分别创建私有仓库的索引文件:

1
2
3
4
$ cd /opt/mirrors/centos
$ for i in base extras updates epel; do
createrepo $i
done

索引创建完成后,上述四个路径中都会多出 repodata.repodata 两个文件夹:

1
2
$ ls -a base
. .. Packages repodata .repodata

配置 web 服务

创建软链接,将 CentOS 软件包存储路径指向到 web 目录下:
$ sudo ln -s /opt/mirrors/centos /var/www/html/centos

重新载入 nginx 服务:$ sudo service nginx reload

此时访问 http://127.0.0.1/centos ,应该可以在 web 界面中浏览本地 CentOS 仓库中的软件包。
centos

使用本地镜像源

登录任意一台 CentOS 主机,备份 /etc/yum.repos.d 目录下的所有 .repo 配置文件到其他位置。
创建软件仓库配置文件 /etc/yum.repos.d/CentOS-7.repo,编辑远程仓库地址(baseurl)为前面搭建的私有仓库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[base]
name=CentOS-7 - Base
baseurl=http://127.0.0.1/centos/base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-7 - Updates
baseurl=http://127.0.0.1/centos/updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-7 - Extras
baseurl=http://127.0.0.1/centos/extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-7 - Plus
baseurl=http://127.0.0.1/centos/centosplus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=0
enabled=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[epel]
name=Extra Packages for Enterprise Linux 7
baseurl=http://127.0.0.1/centos/epel
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=0
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

运行 sudo yum clean all && yum makecache 命令重建软件包索引。
之后即可通过 sudo yum install 命令从本地的私有仓库中安装软件了。

四、crontab

创建 crontab 计划任务,让本地仓库定期从远程仓库拉取有更新的软件包:
$ sudo crontab -e

参考配置:

1
2
0 3 * * 0 reposync -np /opt/mirrors/centos -c /opt/mirrors/CentOS-7.repo
0 22 * * 6 apt-mirror

每周日凌晨 3 点运行 reposync 命令同步 CentOS 仓库。
每周六晚上 10 点运行 apt-mirror 命令同步 Ubuntu 仓库。

参考文章

centos 下创建本地镜像源,结合 nginx