CentOS 搭建内部局域网Yum源同步阿里Yum源

背景

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

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

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

yum源服务器地址:192.168.10.15

放开系统防火墙,关闭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://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo        http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache
yum repolist

三、安装相关软件

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/"
<Directory "/mirror/">
    Options Indexes FollowSymLinks
    AllowOverride  None
    Order allow,deny
    Allow from all
    Require all granted
</Directory>

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

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

注意:上面

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

更改为自己对应的

我这里为http://192.168.10.15

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

[root@yumserver ~]# mkdir -p /mirror/repo/
###直接复制粘贴执行
cat << EOF > /mirror/repo/CentOS-Base.repo
[base]
name=CentOS-$releasever - Base - 192.168.10.15
failovermethod=priority
baseurl=
enable=1
gpgcheck=0
  
#released updates 
[updates]
name=CentOS-$releasever - Updates - 192.168.10.15
failovermethod=priority
baseurl=
enable=1
gpgcheck=0
  
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - 192.168.10.15
failovermethod=priority
baseurl=
enable=1
gpgcheck=0
  
#additional packages that may be useful
[epel]
name=CentOS-$releasever - Epel - 192.168.10.15
failovermethod=priority
baseurl=
enable=1
gpgcheck=0
EOF

九、客户端配置yum源

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

curl -o /etc/yum.repos.d/CentOS-Base.repo http://192.168.10.15/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

版权声明:
作者:maxiushun
链接:https://www.uucico.top/16.html
来源:LoveShare
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>