Centos7配置本地网络yum源,并定期自动同步阿里云yum源

Centos7配置本地网络yum源,并定期自动同步阿里云yum源

总体思路:

通过yum配置阿里云yum源,将下载的rpm包保留到本地(即同步下载rpm包),创建本地yum仓库,通过网站服务器(nginx或者apache,本文将采用apapche作为网站服务器)将本地yum源配置成内网可访问yum源,最后写个定时执行的脚本,自动同步阿里云yum源并下载最新的rpm软件包。

Tips:基本环境:centos7 关闭firewalld 、关闭Selinux(由于是内网的缘故,不太需要繁琐的配置防火墙和selinux)createrepo

第一步:首先我们需要准备一台可以连接外网的Centos7服务器。配置阿里yum源,安装阿里云yunm源,检查其可用性。

备份原有的yum文件:

mv /etc/yum.repos.d/* /root/bak/yum

安装阿里云yum源文件

wget -o /etc/yum.repos.d/CentOSBase.repo https://mirrors.aliyun.com/repo/Centos-7.repo

通过阿里云的yum源安装epel源

yum install -y epel-release

清除原有的yum缓存,建立新的yum源的缓存,列出可用repo

yum clean all && yum makecache && yum repolist

第二步:安装createrepo,安装apache网站服务器(httpd),准备rpm包存放路径

安装相关依赖包

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

准备数据存储路径,我们要准备一个大一点的分区,阿里的yum源体积还是挺大的,而内网源就是把云yum源同步到本地。

mkdir -p /data/repo

更改目录权限

chown -R apache:apache /data/repo && chmod -R 755 /data/repo

tips:参数-n指定下载最新软件包,-p指定目录,指定本地的源–repoid(如果不指定就同步本地服务器所有的源),下载过程比较久
reposync -n --repoid=extras --repoid=updates --repoid=base --repoid=epel -p /data/repo

等待下载完成 查看下载后的文件大小

du -sh /data/repo

第三步:创建仓库索引

创建本地软件源索引

createrepo -po /data/repo/base/ /data/repo/base/
createrepo -po /data/repo/extras/ /data/repo/extras/
createrepo -po /data/repo/updates/ /data/repo/updates/
createrepo -po /date/repo/epel/ /data/repo/epel/

第四步:更新数据源

当公网yum源更新的时候,我们需要给本地的软件仓库中的软件包也一起更新

createrepo --update /data/repo/base/
createrepo --update /data/repo/extras/
createrepo --update /data/repo/updates/
createrepo --update /data/repo/epel/

这里我们可以写一个脚本结合计划任务,让服务器自己更新软件包
脚本内容:

#!/bin/bash
echo "更新软件包"
yum update
echo "开始同步centos7的yum源"
reposync -n --repoid=docker-ce-stable --repoid=extras --repoid=updates --repoid=base --repoid=epel --repoid=nginx -p /data
echo "同步centos7和epel、nginx的yum源完成!!!"

#更新元数据
createrepo --update /data/base/
createrepo --update /data/nginx/
createrepo --update /data/extras/
createrepo --update /data/updates/
createrepo --update /data/epel/
createrepo --update /data/docker-ce-stable/

第五步:创建计划任务

使用crontab创建计划任务
可以使用crontab -e命令,创建计划任务,定时执行yum软件包的更新任务

* */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org

第六步:启动并配置web服务器

启动httpd服务并设置开机自动启动

systemctl enable --now httpd

编辑httpd配置文件:

vim /etc/httpd/conf.d/yum.conf

<VirtualHost *:80>
  DocumentRoot /data/repo/
  ServerName repo.example.org
  ErrorLog logs/repo.example.org-error_log
  CustomLog logs/repo.example.org-access_log common
</VirtualHost>

### 创建yum默认首页index.html
cat << EOF > /data/repo/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>mv /etc/yum.repos.d/* /opt/yum/</myHero>
    <br>
        <h2>2、下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/ </h2>
        <myHero>curl -o /etc/yum.repos.d/CentOS-Base.repo http://repo.example.org/CentOS-Base.repo</myHero>
    <br>
        <h2>3、运行 yum makecache 生成缓存</h2>
    <br>
        <h2>4、运行 yum repolist   查看已经生成缓存</h2>
    <br>
    <br>

</body>
</html>
EOF

编辑好配置文件后,重启一下httpd服务

systemctl restart httpd

第七步:编写yum本地源客户端的yum配置文件

配置文件代码如下:

cat << EOF > /data/repo/CentOS-Base.repo
[base]
name=CentOS-$releasever - Base - repo.example.org
failovermethod=priority
baseurl=http://repo.example.org/base/
enable=1
gpgcheck=0
 
#released updates 
[updates]
name=CentOS-$releasever - Updates - repo.example.org
failovermethod=priority
baseurl=http://repo.example.org/updates/
enable=1
gpgcheck=0
 
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - repo.example.org
failovermethod=priority
baseurl=http://repo.example.org/extras/
enable=1
gpgcheck=0
 
#additional packages that may be useful
[epel]
name=CentOS-$releasever - Epel - repo.example.org
failovermethod=priority
baseurl=http://repo.example.org/epel/
enable=1
gpgcheck=0
EOF

第八步:客户端配置一下dns解析,配置客户端yum源

编写一下主机的hosts文件,将repo.example.org 跟你配置的yum本地源的服务器IP解析配置一下
在这里插入图片描述
配置yum客户端

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

到此,会自动同步阿里云yum源的内网网络yum源就配置好了。

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

河越柯衍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值