Linuxword Global
当前位置: Linux技术 > CentOS7.9 搭建内部yum源服务器同步阿里yum源

1、企业内网需求:企业内部许多服务器是不能连接互联网,但每台centos主机安装软件包时都配置本地yum源很麻烦,也缺少第三方yum源,因此在内网搭建一台yum源服务器满足这一需求,同时网络层面策略只允许此yum源服务器每周日晚限时联网同步阿里云的yum源。

 

2、准备两台测试主机,一台用作yum源服务器(能连互联网),一台用作客户端。

3、系统版本:CentOS 7.9

 

 

实操步骤:

一、添加阿里云yum源

备份原系统的repo

 

 

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

添加阿里云yum源并缓存(Centos-7.repo是软件仓库配置文件,epel-7.repo是扩展源、提供额外的软件包)

 

 

  • 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:yum-utils是yum的工具包集合,reposync一个同步工具。

createrepo:createrepo是一个对rpm文件进行索引建立的工具,就是对指定目录下的rpm文件进行检索,把每个rpm文件的信息存储到指定的索引文件中,这样方便远程yum命令在安装更新时进行检索。

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

 

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

创建存放软件包目录/mirror,(chown设置文件所有者和文件关联组,chmod控制用户对文件的权限)

 

  • mkdir -p /mirror
  • chown -R apache:apache /mirror
  • chmod -R 755 /mirror

同步阿里云yum源软件包

 

###参数-n指下载最新软件包,-p指定目录,指定本地的源--repoid(如果不指定就同步本地服务器所有的源),下载过程比较久(10个小时左右)

 

  • reposync -n --repoid=extras --repoid=updates --repoid=base --repoid=epel -p /mirror
  • du -sh /mirror/*
  •  

9.0G    /mirror/base

16G     /mirror/epel

323M    /mirror/extras

3.3G    /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服务

 

 

  • yum install -y httpd

6.1、启动Apache(httpd)服务

 

 

  • systemctl start httpd
  • systemctl enable httpd
  • systemctl status httpd

6.2、系统防火墙放行80端口(apache服务使用的是80端口)

 

 

  • firewall-cmd --permanent --zone=public --add-port=80/tcp

success

 

 

  • firewall-cmd --reload

success

 

备注:必要时可以运行 systemctl restart httpd 重启一下服务。

6.3、关闭selinux(SELINUX=disabled)

225725_z6zqZZ

6.4、配置httpd.conf文件

文件路径/etc/httpd/conf/httpd.conf

  • DocumentRoot "/mirror/"
  • <Directory "/mirror/">
  • Options Indexes FollowSymLinks
  • AllowOverride None
  • Order allow,deny
  • Allow from all
  • Require all granted
  • </Directory>

注意:上面配置的httpd.conf文件,需要找到如下图DocumentRoot字样的行进行更改,而不是直接复制粘贴插入。

225725_8REdEE

6.5、修改web界面的 index.html 文件

###修改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 http://xx.xx.xx.xx/repo/CentOS-Base.repo</myHero>
  • <br>
  • <h2>3、运行 yum makecache 生成缓存</h2>
  • <br>
  • <h2>4、运行 yum repolist 查看已经生成缓存</h2>
  • <br>
  • <br>
  •  
  • </body>
  • </html>
  • EOF

注意: 上文中的 http://xx.xx.xx.xx 填写自身yum源服务器的ip地址。

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

创建repo文件夹

  • mkdir -p /mirror/repo/

###复制粘贴执行,注意:xx.xx.xx.xx需要更改为服务器ip地址。

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

八、客户端配置yum源 

登录客户端操作系统执行以下命令(注意:xx.xx.xx.xx为yum源服务器的ip地址)

  • mkdir /etc/yum.repos.d/backup/
  • mv /etc/yum.repos.d/* /etc/yum.repos.d/backup/
  • curl -o /etc/yum.repos.d/CentOS-Base.repo http://xx.xx.xx.xx/repo/CentOS-Base.repo
  • yum makecache

用户也可以用浏览器访问 http://xx.xx.xx.xx 查看配置客户端yum源的方法

225725_SlCCZo

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

编辑脚本

  • 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点执行 

  • crontab -l
  • 0 1 * * 1 /bin/bash /mirror/script/centos_yum_update.sh

至此,结束。。。

问题:有时服务器端的yum源仓库索引没有更新成功。

手动更新:

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

「梦想一旦被付诸行动,就会变得神圣,如果觉得我的文章对您有用,请帮助本站成长」

赞(0) 打赏
一分也是爱

支付宝扫一扫打赏

微信扫一扫打赏

上一篇:

下一篇:

相关推荐

博客简介

本站CDN采用VmShell免费提供离中国大陆最近的香港CMI高速网络做支撑,ToToTel打造全球最快速的边沿网络支撑服务,具体详情请见 :https://vmshell.com/ 以及 https://tototel.com/,网站所有的文件和内容禁止大陆网站搬迁复制,谢谢,VPS营销投稿邮箱: admin@linuxxword.com,我们免费帮大家发布,不收取任何费用,请提供完整测试文稿!

精彩评论

友情链接

他们同样是一群网虫,却不是每天泡在网上游走在淘宝和网游之间、刷着本来就快要透支的信用卡。他们或许没有踏出国门一步,但同学却不局限在一国一校,而是遍及全球!申请交换友链

站点统计

  • 文章总数: 2534 篇
  • 草稿数目: 19 篇
  • 分类数目: 6 个
  • 独立页面: 0 个
  • 评论总数: 2 条
  • 链接总数: 0 个
  • 标签总数: 10493 个
  • 注册用户: 139 人
  • 访问总量: 8,664,195 次
  • 最近更新: 2024年9月15日