背景:
有这样的需求,测试环境网段不能上网,但是测试环境centos安装软件包每一台都配置本地yum源很麻烦,也缺少第三方yum源,特此在内网搭建一台yum源服务器满足这一需求,同时防火墙层面只允许此yum源服务器每周日晚限时联网同步阿里云的yum源.
一.yum源服务器的系统环境,最小化安装
1 2 | [root@yumserver ~] # cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core) |
yum源服务器地址 10.8.98.222
放开系统防火墙,关闭selinux
1 2 3 4 | [root@yumserver ~] # firewall-cmd --add-port=80/tcp --permanent success [root@yumserver ~] # firewall-cmd --reload success |
1 2 3 4 5 6 7 8 9 10 11 12 | [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
1 2 3 4 | [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/ |
1 2 3 4 | 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
三.安装相关软件
1 | 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(自定义目录)
1 2 3 4 5 6 7 8 9 10 | 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 |
五 、创建仓库索引
1 2 3 4 | createrepo -po /mirror/base/ /mirror/base/ createrepo -po /mirror/extras/ /mirror/extras/ createrepo -po /mirror/updates/ /mirror/updates/ createrepo -po /mirror/epel/ /mirror/epel/ |
六 、更新数据源
1 2 3 4 | createrepo --update /mirror/base/ createrepo --update /mirror/extras/ createrepo --update /mirror/updates/ createrepo --update /mirror/epel/ |
七、启动并配置Apache服务
1 2 3 4 5 6 7 8 9 10 11 12 | systemctl start httpd systemctl enable httpd systemctl status httpd [root@yumserver ~] # vim /etc/httpd/conf/httpd.conf DocumentRoot "/mirror/" <Directory "/mirror/" > Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all Require all granted < /Directory > |
###修改Apache默认首页index.html,直接复制粘贴执行
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 41 | cat << EOF > /usr/share/httpd/noindex/index.html <!DOCTYPE html> <html> <head> <meta charset= "utf-8" > <title>CentOS 7 镜像</title> <script>document.createElement( "myHero" )</script> <style> myHero { display: block; background-color: #ddd; padding: 10px; font-size: 20px; } </style> </head> <body> <h1>简介</h1> <hr> <p>CentOS,是基于 Red Hat Linux 提供的可自由使用源代码的企业级 Linux 发行版本。</p> <hr> <br> <br> <h1>CentOS 7 配置内部YUM源</h1> <br> <h2>1、备份</h2> <myHero>mkdir /etc/yum.repos.d/backup </myHero> <myHero>mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/</myHero> <br> <h2>2、下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/ </h2> <myHero>curl -o /etc/yum.repos.d/CentOS-Base.repo xxxx://x.x.x.x/repo/CentOS-Base.repo</myHero> <br> <h2>3、运行 yum makecache 生成缓存</h2> <br> <h2>4、运行 yum repolist 查看已经生成缓存</h2> <br> <br> </body> </html> EOF |
注意:上面
更改为自己对应的
我这里为http : // 10.8.98.222 /
八、编写yum源客户端配置文件
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 | [root@yumserver ~] # mkdir -p /mirror/repo/ ###直接复制粘贴执行 cat << EOF > /mirror/repo/CentOS-Base .repo [base] name=CentOS-$releasever - Base - 10.8.98.222 failovermethod=priority baseurl= enable =1 gpgcheck=0 #released updates [updates] name=CentOS-$releasever - Updates - 10.8.98.222 failovermethod=priority baseurl= enable =1 gpgcheck=0 #additional packages that may be useful [extras] name=CentOS-$releasever - Extras - 10.8.98.222 failovermethod=priority baseurl= enable =1 gpgcheck=0 #additional packages that may be useful [epel] name=CentOS-$releasever - Epel - 10.8.98.222 failovermethod=priority baseurl= enable =1 gpgcheck=0 EOF |
九、客户端配置yum源
用户可以浏览器访问10.8.98.222查看配置客户端yum源的方法
1 2 | curl -o /etc/yum .repos.d /CentOS-Base .repo yum makecache |
十 、设置定时同步yum源的任务
[root@yumserver ~]# cat /mirror/script/centos_yum_update.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #!/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点执行
1 2 | [root@yumserver ~] # crontab -l 0 1 * * 1 /bin/bash /mirror/script/centos_yum_update .sh |