本地yum源服务器搭建及同步阿里源

1、系统环境

[root@yum-server ~]# cat /etc/centos-release
CentOS Linux release 7.3.1611 (Core) 
[root@yum-server ~]# uname -r
3.10.0-514.el7.x86_64
[root@yum-server ~]# ip a |awk 'NR==9{print $2}'|awk -F '/' '{print $1}'
10.0.7.190
[root@yum-server ~]# lsblk
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0            11:0    1   10G  0 rom  /mnt
sr1            11:1    1  484K  0 rom  
vda           253:0    0   20G  0 disk 
└─vda1        253:1    0   20G  0 part /
vdb           253:16   0  500G  0 disk 
└─yumvg-yumlv 252:0    0  500G  0 lvm  /yum  //yum仓库存储位置,一般实际生产中建议容量设置较大

2、配置环境

由于网络环境复杂,yum源服务有多种来源

base源epel源zabbix源

源类型 repoid 来源 使用者
base源-ISO media.repo CentOS-7-x86_64-Everything-1810.iso 服务端
base源-ISO Centos-media.repo file:///yum/repository/c7 服务端
base源-ISO media.repo http://10.0.7.190/c7 客户端
base源-云 base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 服务端
base源-云 Centos-base.repo file:///yum/repository/base 服务端
base源-云 base.repo http://10.0.7.190/base 客户端
epel源 epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 服务端
epel源 Centos-epel.repo file:///yum/repository/epel 服务端
epel源 epel.repo http://10.0.7.190/epel 客户端
zabbix源 zabbix.repo https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/ 服务端
zabbix源 Centos-zabbix.repo file:///yum/repository/zabbix 服务端
zabbix源 zabbix.repo http://10.0.7.190/zabbix 客户端
源类型 存储路径
base源-ISO /yum/repository/c7
base源-云 /yum/repository/base
epel源 /yum/repository/epel
zabbix源 /yum/repository/zabbix
应用程序 作用 替代程序
httpd baseurl nginx/vsftp
yum-utils repository的管理工具,提供reporsync服务
createrepo 编辑yum库工具
plugin-priorities 控制yum源更新优先级工具

3、搭建base-yum源-基于ISO镜像

3-1、备份并移除其他repo
[root@yum-server yum.repos.d]# cd 
[root@yum-server ~]# cd /etc/yum.repos.d/
[root@yum-server yum.repos.d]# tar czvf repo-bk.tar.gz CentOS-*
CentOS-Base.repo
CentOS-CR.repo
CentOS-Debuginfo.repo
CentOS-fasttrack.repo
CentOS-Media.repo
CentOS-Sources.repo
CentOS-Vault.repo
[root@yum-server yum.repos.d]# rm -rf *.repo
[root@yum-server yum.repos.d]# ls
repo-bk.tar.gz

3-2、挂载ISO镜像:
[root@yum-server yum.repos.d]# mount /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only
3-3、配置ISO镜像源
区别于云base源,ISO本地源名称为media.repo

[root@yum-server yum.repos.d]# vim media.repo
[c7-media]
name=CentOS-$releasever-media
baseurl=file:///mnt
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

注:配置repo路径、关闭gbg校验、启用ISO镜像仓库

3-4、制作cache
[root@yum-server yum.repos.d]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: c7-media
Cleaning up everything
Cleaning up list of fastest mirrors

[root@yum-server yum.repos.d]# yum makecache
Loaded plugins: fastestmirror
c7-media                    | 3.6 kB  00:00:00     
(1/4): c7-media/group_gz                | 166 kB  00:00:00     
(2/4): c7-media/primary_db              | 6.0 MB  00:00:00     
(3/4): c7-media/filelists_db            | 7.1 MB  00:00:00     
(4/4): c7-media/other_db                | 2.6 MB  00:00:00     
Determining fastest mirrors
Metadata Cache Created
3-5、拷贝rpm包
[root@yum-server yum.repos.d]# mkdir -p /yum/repository/c7
[root@yum-server yum.repos.d]# cp -ra  /mnt/* /yum/repository/c7/  

或者使用reposync同步
[root@yum-server yum.repos.d]#reposync -r media -p /yum/repository/c7/

3-6、创建索引:
[root@yum-server ~]# createrepo -po /yum/repository/c7 /yum/repository/c7
3-7、media源创建完毕,本地验证:
[root@yum-server yum.repos.d]# vim CentOS-c7.repo
[c7-c7]
name=CentOS-$releasever-c7
baseurl=file:///yum/repository/c7
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

注:配置repo路径、关闭gbg校验、启用epel镜像仓库

3-8、制作cache
[root@yum-server yum.repos.d]# yum clean all
[root@yum-server yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
c7-c7                                           | 2.9 kB  00:00:00     
c7-c7/primary_db                                | 6.7 MB  00:00:00     
Determining fastest mirrors
repo id                         repo name           status
c7-c7                           CentOS-7-c7         10,019
repolist: 20,038

4、搭建base-yum源-基于云源

4-1、备份原有源:
[root@yum-server ~]# cd /etc/yum.repos.d/
[root@yum-server yum.repos.d]# mv CentOS-Media.repo CentOS-Media.repobak
[root@yum-server yum.repos.d]# ls
CentOS-Media.repobak repo-bk.tar.gz
4-2、下载云base源:
[root@yum-server yum.repos.d]# wget -O /etc/yum.repos.d/base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@yum-server yum.repos.d]# ls
backup  base.repo
4-3、检验阿里云源是否正常:
[root@yum-server yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
base                                            | 3.6 kB  00:00:00     
extras                                          | 2.9 kB  00:00:00     
updates                                         | 2.9 kB  00:00:00     
(1/4): extras/7/x86_64/primary_db               | 165 kB  00:00:00     
(2/4): base/7/x86_64/group_gz                   | 165 kB  00:00:00     
(3/4): base/7/x86_64/primary_db                 | 6.0 MB  00:00:00     
(4/4): updates/7/x86_64/primary_db              | 7.6 MB  00:00:01     
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
    repo id             repo name                                   status
base/7/x86_64           CentOS-7 - Base - mirrors.aliyun.com        10,097
extras/7/x86_64         CentOS-7 - Extras - mirrors.aliyun.com      341
updates/7/x86_64        CentOS-7 - Updates - mirrors.aliyun.com     1,787
repolist: 12,225
4-4、根据源标识同步源到本地目录:
创建本地epel目录:
[root@yum-server ~]# mkdir /yum/repository/
同步阿里base源到本地base目录:
[root@yum-server ~]# reposync -r base -p /yum/repository/
注:不用担心没有创建相关目录,系统自动创建相关目录,并下载,时间较长请耐心等待。

4-5、创建索引:
[root@yum-server ~]# createrepo -po /yum/repository/base /yum/repository/base
4-6、云base源创建完毕,本地验证:
[root@yum-server yum.repos.d]# vim CentOS-base.repo
[c7-base]
name=CentOS-$releasever-base
baseurl=file:///yum/repository/base
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

注:配置repo路径、关闭gbg校验、启用epel镜像仓库

4-7、制作cache
[root@yum-server yum.repos.d]# yum clean all
[root@yum-server yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
c7-base                                             | 2.9 kB  00:00:00     
c7-base/primary_db                                  | 1.0 MB  00:00:00     
Determining fastest mirrors
repo id                         repo name           status
c7-base                         CentOS-7-base       1,0009
repolist: 1,890
4-8、更新新的rpm包
[root@yum-server ~]# reposync -r base -np /yum/repository/
注:时间同样较长,请耐心等待。
4-9、更新源数据:
[root@yum-server ~]# createrepo --update /yum/repository/base
4-10、创建定时任务脚本
[root@yum-server yum.repos.d]# vim /yum/script-reposync/yum-update-base.sh
#!/bin/bash
echo 'Updating Base Aliyum Source'
DATETIME=`date +%F_%T`
exec > /var/log/update-base.repo_$DATETIME.log
     reposync -r base -np /yum/repository/
if [ $? -eq 0 ];then
      createrepo --update /yum/repository/base
    echo "SUCESS: $DATETIME base.repo update successful"
  else
    echo "ERROR: $DATETIME base.repo update failed"
fi
4-11、将脚本加入到定时任务中
[root@yum-server script-reposync]# crontab -e
# Updating Aliyum Source
00 06 * * 6 [ $(date +%d) -eq $(cal | awk 'NR==3{print $NF}') ] && /bin/bash /yum/script-reposync/yum-update-base.sh
每月第三个周六的06点更新阿里云base yum源

5、搭建epel-yum源

5-1、备份原有源:
[root@yum-server ~]# cd /etc/yum.repos.d/
[root@yum-server yum.repos.d]# mv CentOS-Media.repo CentOS-Media.repobak
[root@yum-server yum.repos.d]# ls
CentOS-Media.repobak repo-bk.tar.gz
5-2、下载epel源:
[root@yum-server yum.repos.d]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@yum-server yum.repos.d]# ls
CentOS-Media.repobak  epel.repo  repo-bk.tar.gz
5-3、检验阿里云源是否正常:
[root@yum-server yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Loading mirror speeds from cached hostfile
repo id             repo name                                           status
!epel/x86_64        Extra Packages for Enterprise Linux 7 - x86_64      11,989
repolist: 11,989
5-4、根据源标识同步源到本地目录:
创建本地epel目录:
[root@yum-server ~]# mkdir /yum/repository/
同步阿里epel源到本地epel目录:
[root@yum-server ~]# reposync -r epel -p /yum/repository/
注:不用担心没有创建相关目录,系统自动创建相关目录,并下载,时间较长请耐心等待。

5-5、创建索引:
[root@yum-server ~]# createrepo -po /yum/repository/epel /yum/repository/epel
5-6、epel源创建完毕,本地验证:
[root@yum-server yum.repos.d]# vim CentOS-epel.repo
[c7-epel]
name=CentOS-$releasever-epel
baseurl=file:///yum/repository/epel
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

注:配置repo路径、关闭gbg校验、启用epel镜像仓库

5-7、制作cache
[root@yum-server yum.repos.d]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: c7-epel
Cleaning up everything
Cleaning up list of fastest mirrors
[root@yum-server yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
c7-epel                                             | 2.9 kB  00:00:00     
c7-epel/primary_db                                  | 6.7 MB  00:00:00     
Determining fastest mirrors
repo id                         repo name           status
c7-epel                         CentOS-7-epel       13,229
repolist: 13,229
5-8、更新新的rpm包
[root@yum-server ~]# reposync -r epel -np /yum/repository/
注:时间同样较长,请耐心等待。
5-9、更新源数据:
[root@yum-server ~]# createrepo --update /yum/repository/epel
5-10、创建定时任务脚本
[root@yum-server yum.repos.d]# vim /yum/script-reposync/yum-update-epel.sh
#!/bin/bash
echo 'Updating Epel Aliyum Source'
DATETIME=`date +%F_%T`
exec > /var/log/update-epel.repo_$DATETIME.log
     reposync -r epel -np /yum/repository/
if [ $? -eq 0 ];then
      createrepo --update /yum/repository/epel
    echo "SUCESS: $DATETIME epel.repo update successful"
  else
    echo "ERROR: $DATETIME epel.repo update failed"
fi
5-11、将脚本加入到定时任务中
[root@yum-server script-reposync]# crontab -e
# Updating Aliyum Source
00 06 * * 6 [ $(date +%d) -eq $(cal | awk 'NR==3{print $NF}') ] && /bin/bash /yum/script-reposync/yum-update-epel.sh
每月第三个周六的06点更新阿里云epel yum源

搭建zabbix-yum源

6-1、备份原有源:
[root@yum-server ~]# cd /etc/yum.repos.d/
[root@yum-server yum.repos.d]# ls
CentOS-epel.repo  CentOS-Media.repo  epel.repo  repo-bk.tar.gz
[root@yum-server yum.repos.d]# mkdir backup   
[root@yum-server yum.repos.d]# mv *.repo backup/
[root@yum-server yum.repos.d]# ls
backup  repo-bk.tar.gz
6-2、配置zabbix源:
[root@yum-server yum.repos.d]# vim zabix.repo
[zabbix]
name=Zabbix Official Repository - \$basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/\$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-non-supported]
name=Zabbix Official Repository non-supported - \$basearch
baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/\$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1
~            
6-3、检验阿里云源是否正常:
[root@yum-server yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                 repo name                                               status
zabbix/x86_64           Zabbix Official Repository - x86_64                     289
zabbix-non-supported/x86_64 Zabbix Official Repository non-supported - x86_64   4
repolist: 293
6-4、根据源标识同步源到本地目录:
创建本地zabbix目录:
[root@yum-server ~]# mkdir /yum/repository/
同步阿里zabbix源到本地zabbix目录:
[root@yum-server ~]# reposync -r zabbix -p /yum/repository/
注:不用担心没有创建相关目录,系统自动创建相关目录,并下载,时间较长请耐心等待。

6-5、创建索引:
[root@yum-server ~]# createrepo -po /yum/repository/zabbix /yum/repository/zabbix
6-6、zabbix源创建完毕,本地验证:
[root@yum-server yum.repos.d]# vim CentOS-zabbix.repo
[c7-zabbix]
name=CentOS-$releasever-zabbix
baseurl=file:///yum/repository/zabbix
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX

注:配置repo路径、关闭gbg校验、启用epel镜像仓库

6-7、制作cache
[root@yum-server yum.repos.d]# yum clean all
[root@yum-server yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                 repo name                                           status
c7-zabbix                               CentOS-7-zabbix                                     289
zabbix/x86_64                           Zabbix Official Repository - x86_64                 289
zabbix-non-supported/x86_64             Zabbix Official Repository non-supported - x86_64   4
repolist: 582
6-8、更新新的rpm包
[root@yum-server ~]# reposync -r zabbix -np /yum/repository/
6-9、更新源数据:
[root@yum-server ~]# createrepo --update /yum/repository/zabbix
6-10、创建定时任务脚本
[root@yum-server yum.repos.d]# vim /yum/script-reposync/yum-update-zabbix.sh
#!/bin/bash
echo 'Updating Zabbix Aliyum Source'
DATETIME=`date +%F_%T`
exec > /var/log/update-zabbi.repo_$DATETIME.log
     reposync -r zabbix -np /yum/repository/
if [ $? -eq 0 ];then
      createrepo --update /yum/repository/zabbix
    echo "SUCESS: $DATETIME zabbix.repo update successful"
  else
    echo "ERROR: $DATETIME zabbix.repo update failed"
fi
5-11、将脚本加入到定时任务中
[root@yum-server script-reposync]# crontab -e
# Updating Aliyum Source
00 06 * * 6 [ $(date +%d) -eq $(cal | awk 'NR==3{print $NF}') ] && /bin/bash /yum/script-reposync/yum-update-zabbix.sh
每月第三个周六的06点更新阿里云zabbix yum源

7、搭建web服务

     打开httpd服务程序的主配置文件/etc/httpd/conf/httpd.conf ,将约第119行用于定义网站数据保存路径的参数DocumentRoot修改为/yum/repository,同时还需要将约第124行用于定义目录权限的参数Directory后面的路径也修改为/yum/repository,配置文件修改完毕后即可保存并退出。

[root@yum-server yum.repos.d]yum install -y httpd
[root@yum-server yum.repos.d]firewall-cmd --add-service=httpd --permanent
[root@yum-server conf]# vim /etc/httpd/conf/httpd.conf
………………省略部分输出信息………………
113 
114 #
115 # DocumentRoot: The directory out of which you will serve your
116 # documents. By default, all requests are taken from this directory, bu t
117 # symbolic links and aliases may be used to point to other locations.
118 #
119 DocumentRoot "/yum/repository"
120 
121 #
122 # Relax access to content within /var/www.
123 #
124 
125 AllowOverride None
126 # Allow open access:
127 Require all granted
128 
………………省略部分输出信息………………
[root@yum-server ~]systemctl restart httpd && systemctl enable httpd
访问测试:
base-ISO-yum源: http://10.0.7.190/c7/
c7
base-云-yum源: http://10.0.7.190/base/
base
eple-yum源: http://10.0.7.190/epel/
epel
zabbix-yum源: http://10.0.7.190/zabbix/
zabbix

8、客户端yum源测试

8-1、base-ISO-yum源:
[root@centos73_test /etc/yum.repos.d]#vim etc/yum.repos.d/media.repo    
[media]
name=media
baseurl=http://10.0.7.190/c7
enabled=1
gpgcheck=0
[root@centos73_test /etc/yum.repos.d]yum clean all
[root@centos73_test /etc/yum.repos.d]yum makecache 
[root@centos73_test /etc/yum.repos.d]yum install httpd
Loaded plugins: langpacks, product-id, subscription-manager

………………省略部分输出信息………………
Dependencies Resolved
================================================================================
 Package                        Arch    Version Repository  Size
================================================================================
Installing:
 httpd x86_64 2.4.6-17.el7      rhel                        1.2 M
Installing for dependencies:
 apr x86_64 1.4.8-3.el7 rhel 103 k
 apr-util x86_64 1.5.2-6.el7 rhel 92 k
 httpd-tools x86_64 2.4.6-17.el7 rhel 77 k
 mailcap noarch 2.1.41-2.el7 rhel 31 k
Transaction Summary
================================================================================
Install 1 Package (+4 Dependent packages)
Total download size: 1.5 M
Installed size: 4.3 M
Is this ok [y/d/N]: y
Downloading packages:
--------------------------------------------------------------------------------
………………省略部分输出信息………………
Complete!
8-2、base-云-yum源:
[root@centos73_test /etc/yum.repos.d]#vim etc/yum.repos.d/base.repo    
[base]
name=base
baseurl=http://10.0.7.190/base
enabled=1
gpgcheck=0

[root@centos73_test /etc/yum.repos.d]yum clean all
[root@centos73_test /etc/yum.repos.d]yum makecache 
[root@centos73_test /etc/yum.repos.d]#yum install ree

………………省略部分输出信息………………
Dependencies Resolved
===================================================================================================================================================================
 Package            Arch            Version         Repository          Size
===================================================================================================================================================================
Updating:
 ftp                x86_64          0.17-67.el7     base                61 k

Transaction Summary
===================================================================================================================================================================
Install  1 Package

Total download size: 61 k
Installed size: 96 k
Is this ok [y/d/N]: y
Downloading packages:
ftp-0.17-67.el7.x86_64.rpm 
--------------------------------------------------------------------------------
………………省略部分输出信息………………
Complete!
8-3、epel-yum源:
[root@centos73_test /etc/yum.repos.d]#vim etc/yum.repos.d/epel.repo    
[epel]
name=epel
baseurl=http://10.0.7.190/epel
enabled=1
gpgcheck=0

[root@centos73_test /etc/yum.repos.d]yum clean all
[root@centos73_test /etc/yum.repos.d]yum makecache 
[root@centos73_test /etc/yum.repos.d]#yum install epel-release

………………省略部分输出信息………………
Dependencies Resolved
===================================================================================================================================================================
 Package            Arch            Version         Repository          Size
===================================================================================================================================================================
Updating:
 epel-release       noarch          7-12            epel                15 k

Transaction Summary
===================================================================================================================================================================
Install  1 Package

Total download size: 15 k
Installed size: 24 k
Is this ok [y/d/N]: y 
Downloading packages:
epel-release-7-12.noarch.rpm
--------------------------------------------------------------------------------
………………省略部分输出信息………………
Complete!
8-4、zabbix-yum源:
[root@centos73_test /etc/yum.repos.d]#vim etc/yum.repos.d/zabbix.repo    
[zabbix]
name=zabbix
baseurl=http://10.0.7.190/zabbix
enabled=1
gpgcheck=0

[root@centos73_test /etc/yum.repos.d]yum clean all
[root@centos73_test /etc/yum.repos.d]yum makecache 
[root@centos73_test /etc/yum.repos.d]#yum install zabbix-agent

………………省略部分输出信息………………
Dependencies Resolved
===================================================================================================================================================================
 Package            Arch            Version         Repository          Size
===================================================================================================================================================================
Installing:
 zabbix-agent       x86_64          4.0.19-1.el7    zabbix              422 k
Transaction Summary
===================================================================================================================================================================
Install  1 Package
Total download size: 422 k
Installed size: 1.6 M
Is this ok [y/d/N]: y
Downloading packages:
zabbix-agent-4.0.19-1.el7.x86_64.rpm   
--------------------------------------------------------------------------------
………………省略部分输出信息………………
Complete!

总结:

yum源服务器搭建根据来源不同,有多种方式,以上述实验为例,搭建步骤如下:

  1. 规划好yum仓库存储容量(/yum/repository/,500G)
  2. 采用国内源(阿里云、华为云、各大高校镜像库等)
  3. 光盘镜像采用everything,确保rpm包尽可能完整
  4. 根据来源准备服务器端repo配置文件(repo编写方式大同小异,除epel源外其他的均需手动编写配置,若base源采用云端方式下载,则不需要手动编写)
  5. 对外提供服务可采用web(apache、nginx)、ftp(vsftp)等方式,注意服务器安全,尽可能开启防火墙,通过放行服务端口策略确保网络安全
  6. 目前只编写了epel和zabbix的更新脚本,其他yum源有需要的可根据实际情况参考编写

服务端repo:

[root@yum-server yum.repos.d]# ls
base.repo  CentOS-base.repo  CentOS-epel.repo  Centos-media.repo  CentOS-zabbix.repo  epel.repo  media.repo  repo-bk.tar.gz  zabbix.repo

客户端repo:

[root@centos73_test /etc/yum.repos.d]#ls
base.repo  epel.repo  media.repo  zabbix.repo

你可能感兴趣的:(本地yum源服务器搭建及同步阿里源)