CentOS 搭建内部Yum源同步阿里Yum源

参考文档: https://www.cnblogs.com/zoulongbin/p/5773330.html

背景:

有这样的需求,测试环境网段不能上网,但是测试环境centos安装软件包每一台都配置本地yum源很麻烦,也缺少第三方yum源,特此在内网搭建一台yum源服务器满足这一需求,同时防火墙层面只允许此yum源服务器每周日晚限时联网同步阿里云的yum源.


一.yum源服务器的系统环境,最小化安装

[root@yumserver ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

   yum源服务器地址  10.8.98.222  

放开系统防火墙,关闭selinux

[root@yumserver ~]# firewall-cmd --add-port=80/tcp --permanent 
success
[root@yumserver ~]# firewall-cmd --reload
success
[root@yumserver ~]# cat /etc/selinux/config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted


二.添加阿里云yum源并检查

备份好原系统的repo

[root@yumserver yum.repos.d]# pwd
/etc/yum.repos.d
[root@yumserver yum.repos.d]# mkdir backup
[root@yumserver yum.repos.d]# mv *.repo  backup/
curl -o /etc/yum.repos.d/CentOS-Base.repo https:999999//mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo        http:999999//mirrors.aliyun.com/repo/epel-7.repo
yum makecache
yum repolist

去掉999999

 

三.安装相关软件

yum install -y wget make cmake gcc gcc-c++ pcre-devel zlib-devel openssl openssl-devel httpd yum-utils createrepo

标注:

yum-utils:reposync同步工具

createrepo:编辑yum库工具

httpd:通过Apache软件提供web服务,也可以使用nginx


四、同步阿里云yum源软件包到本地服务器指定目录/mirror(自定义目录)

mkdir -p /mirror
chown -R apache:apache /mirror
chmod -R 755 /mirror
###参数-n指下载最新软件包,-p指定目录,指定本地的源--repoid(如果不指定就同步本地服务器所有的源),下载过程比较久
reposync -n --repoid=extras --repoid=updates --repoid=base --repoid=epel -p /mirror
[root@yumserver ~]# du -sh /mirror/*
9.0G    /mirror/base
16G     /mirror/epel
321M    /mirror/extras
2.2G    /mirror/updates


、创建仓库索引

createrepo -po /mirror/base/ /mirror/base/
createrepo -po /mirror/extras/ /mirror/extras/
createrepo -po /mirror/updates/ /mirror/updates/
createrepo -po /mirror/epel/ /mirror/epel/


更新数据源

createrepo --update /mirror/base/
createrepo --update /mirror/extras/
createrepo --update /mirror/updates/
createrepo --update /mirror/epel/


七、启动并配置Apache服务

systemctl start httpd
systemctl enable httpd
systemctl status httpd
[root@yumserver ~]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/mirror/"

    Options Indexes FollowSymLinks
    AllowOverride  None
    Order allow,deny
    Allow from all
    Require all granted


###修改Apache默认首页index.html,直接复制粘贴执行

cat << EOF > /usr/share/httpd/noindex/index.html




CentOS 7 镜像

 


    

简介

    
    

CentOS,是基于 Red Hat Linux 提供的可自由使用源代码的企业级 Linux 发行版本。

    
    
    
        

CentOS 7 配置内部YUM源

    
        

1、备份

        mkdir /etc/yum.repos.d/backup  mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/     
        

2、下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/ 

        curl -o /etc/yum.repos.d/CentOS-Base.repo xxxx://x.x.x.x/repo/CentOS-Base.repo              
        

3、运行 yum makecache 生成缓存

    
        

4、运行 yum repolist   查看已经生成缓存

    
    
EOF

注意:上面

xxxx://x.x.x.x/

更改为自己对应的

我这里为http  :  //  10.8.98.222  / 




八、编写yum源客户端配置文件

[root@yumserver repo]# cat /mirror/repo/CentOS-Base.repo 
[base]
name=CentOS- - Base - 10.8.98.222
failovermethod=priority
baseurl=http://*******/base/
enable=1
gpgcheck=0
 
#released updates 
[updates]
name=CentOS- - Updates - 10.8.98.222
failovermethod=priority
baseurl=http://*******/updates/
enable=1
gpgcheck=0
 
#additional packages that may be useful
[extras]
name=CentOS- - Extras - 10.8.98.222
failovermethod=priority
baseurl=http://*******/extras/
enable=1
gpgcheck=0
 
#additional packages that may be useful
[epel]
name=CentOS- - Epel - 10.8.98.222
failovermethod=priority
baseurl=http://*******/epel/
enable=1
gpgcheck=0
[root@yumserver repo]#

*******此处为自己的ip


九、客户端配置yum源

用户可以浏览器访问10.8.98.222查看配置客户端yum源的方法

curl -o /etc/yum.repos.d/CentOS-Base.repo http:/ /10.8.98.222/repo/CentOS-Base.repo
yum makecache


、设置定时同步yum源的任务

[root@yumserver ~]# cat  /mirror/script/centos_yum_update.sh

#!/bin/bash
echo 'Updating Aliyum Source'
DATETIME=`date +%F_%T`
exec > /var/log/aliyumrepo_$DATETIME.log
reposync -np /mirror
if [ $? -eq 0 ];then
 createrepo --update /mirror/base
  createrepo --update /mirror/extras
   createrepo --update /mirror/updates
   createrepo --update /mirror/epel
    echo "SUCESS: $DATETIME aliyum_yum update successful"
	else
	 echo "ERROR: $DATETIME aliyum_yum update failed"
	fi


添加定时任务,每周一凌晨1点执行

[root@yumserver ~]# crontab -l
0 1 * * 1 /bin/bash /mirror/script/centos_yum_update.sh




请使用浏览器的分享功能分享到微信等