2023-12-28    2023-12-28    1288 字  3 分钟

为了提高软件包的下载速度,减轻网络负担,提高系统的安全性,我们可自建yum源服务器。

本地yum服务器:自己准备软件安装包,通过ftphttp的方式共享给其他主机,适用于服务器不可访问互联网的情况。

网络yum服务器:从互联网上同步安装包,通过ftphttp的方式共享给其他主机使用,适用于加速下载,减轻网络负担的情况。

为了展示ftphttp共享安装包的配置。本文档部署本地yum服务器使用的是ftp共享安装包的方式,部署网络yum服务器使用的是通过 http共享安装包的方式。

环境

所有服务器均在10.0.0.0/24的内网中,服务器通过内网可互通。

服务器操作系统为CentOS

服务器 客户端
10.0.0.200 10.0.0.201

本地yum服务器

CentOS镜像中包含了大量的安装包。我们可以将这些安装包放在指定的目录中,部署一个yum仓库,提供给其他服务器使用。

服务端配置

软件包准备

CentOS光盘挂载到/mnt目录。当然你也可以新建一个目录,将挂载出来的安装包拷贝到新建的目录中。

1
[root@localhost ~]# mount /dev/cdrom /mnt/

共享软件包

通过ftp的方式共享软件包,安装并启动vsftpd,确保服务正常启动。

1
2
3
[root@localhost ~]# systemctl start vsftpd 
[root@localhost ~]# systemctl enable vsftpd
[root@localhost ~]# systemctl status vsftpd

修改vsftpd的配置文件,将安装包目录共享出去。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
[root@localhost ~]# egrep -v "^$|#" /etc/vsftpd/vsftpd.conf 
anon_root=/mnt/Packages
# 指定ftp共享的目录
anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

通过浏览器验证vsftpd是否配置成功。

验证ftp

客户端配置

客户端仅需新建repo文件,将baseurl指向服务端即可。

1
2
3
4
5
6
[root@localhost ~]# cat /etc/yum.repos.d/local.repo 
[ftp-repo]
name = This is ftp repo
baseurl = ftp://10.0.0.200/
enabled =1
gpgcheck = 0

验证。

1
2
3
4
5
6
[root@localhost ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                   repo name                                         status
ftp-repo                                  This is ftp repo                                  436
repolist: 436

网络yum服务器

服务端配置

软件包准备

安装rsync用于从互联网同步安装包到本地。

1
[root@localhost ~]# yum install rsync -y

创建目录用于存放安装包。

1
2
3
4
[root@localhost ~]# mkdir -p /data/wwwroot/yum-server/os/Packages/
[root@localhost ~]# mkdir -p /data/wwwroot/yum-server/extras/Packages/
[root@localhost ~]# mkdir -p /data/wwwroot/yum-server/updates/Packages/
[root@localhost ~]# mkdir -p /data/wwwroot/yum-server/centosplus/Packages/

mirrors.ustc.edu.cn同步安装包到创建的目录中。

1
2
3
4
[root@localhost ~]# rsync -avz --delete rsync://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/ /data/wwwroot/yum-server/os/Packages/
[root@localhost ~]# rsync -avz --delete rsync://mirrors.ustc.edu.cn/centos/7/extras/x86_64/Packages/ /data/wwwroot/yum-server/extras/Packages/
[root@localhost ~]# rsync -avz --delete rsync://mirrors.ustc.edu.cn/centos/7/updates/x86_64/Packages/ /data/wwwroot/yum-server/updates/Packages/
[root@localhost ~]# rsync -avz --delete rsync://mirrors.ustc.edu.cn/centos/7/centosplus/x86_64/Packages/ /data/wwwroot/yum-server/centosplus/Packages/

共享软件包

通过http的方式共享软件包,安装nginx服务用于提供http服务。

1
2
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]# yum install nginx -y

配置nginx服务,指定共享文件的目录。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
[root@localhost ~]# cat /etc/nginx/conf.d/default.conf
server {
    listen  80;
    # 自己PC的ip或者服务器的域名
    server_name 192.168.55.10;
    # 避免中文乱码
    charset utf-8;
    # 存放文件的目录
    root /data/wwwroot/yum-server;
    location / {
        # 索引
        autoindex on;
        # 显示文件大小
        autoindex_exact_size on;
        # 显示文件时间
        autoindex_localtime on;
    }
}

启动nginx并确保服务正常运行。

1
2
3
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl enable nginx
[root@localhost ~]# systemctl status nginx

使用createrepo创建仓库(生成repodata目录)。

1
2
3
4
5
[root@localhost ~]# yum install -y createrepo
[root@localhost ~]# createrepo -o /data/wwwroot/yum-server/os /data/wwwroot/yum-server/os/Packages/
[root@localhost ~]# createrepo -o /data/wwwroot/yum-server/extras /data/wwwroot/yum-server/extras/Packages/
[root@localhost ~]# createrepo -o /data/wwwroot/yum-server/updates /data/wwwroot/yum-server/updates/Packages/
[root@localhost ~]# createrepo -o /data/wwwroot/yum-server/centosplus /data/wwwroot/yum-server/centosplus/Packa

用浏览器验证nginx共享。

验证nginx共享

客户端配置

客户端仅需新建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
[root@node1 ~]# cat /etc/yum.repos.d/local-httpd.repo 
[baseurl]
name = Base
baseurl = http://192.168.55.10/os
enabled = 1
gpgcheck = 0

[updates]
name = updates
baseurl = http://192.168.55.10/updates
enabled = 1
gpgcheck = 0

[extras]
name = extras
baseurl = http://192.168.55.10/extras
enbaled = 1
gpgcheck = 0

[centosplus]
name = centosplus
baseurl =  http://192.168.55.10/centosplus
enabled = 1
gpgcheck = 0

客户端验证。

1
2
3
4
5
6
7
8
9
[root@node1 yum.repos.d]# yum repolist 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                               repo name                              status
baseurl                               Base                                   10,019
centosplus                            centosplus                                103
extras                                extras                                    435
updates                               updates                                 2,500
repolist: 13,057

生产yum服务器需要定时向网络更新yum源。


image-20231028232834657