VPS-搭建一个 CentOS7 的在线yum源

环境:

  • CentOS7.x
  • ngnix
  • rsync

目的:

同步CentOS镜像站点的内容到此服务器,并且通过配置http服务器,能够向外提供yum服务

搭建 YUM 私有源有两个方法,一个是借用镜像,另一个是同步网上公开的 YUM 源

实施:

  1. 新建目录,存放镜像文件

    mkdir -p /usr/local/local_mirror/
    # 注意坑点:nginx位于第三方的yum源里面,而不在centos官方yum源里面
    # 使用epel源,也就是安装epel-release软件包:EPEL (Extra Packages for Enterprise Linux)是基于Fedora的一个项目,为“红帽系”的操作系统提供额外的软件包,适用于RHEL、CentOS等系统。
    yum install epel-release
    yum install nginx
    # Yum-utils, a collection of utilities that integrate with yum to extend its native features in several ways, thus making it more powerful and easier to use.
    yum install yum-utils
    yum install createrepo
    # rsync 是一个常用的 Linux 应用程序,用于文件同步。
    # 它可以在本地计算机与远程计算机之间,或者两个本地目录之间同步文件(但不支持两台远程计算机之间的同步)。它也可以当作文件复制工具,替代cp和mv命令。
    ### 传输的双方都必须安装 rsync。
    yum install rsync
    yum install vim
    # it dosen't know alacritty
    export TERM=xterm
    $echo TERM
  2. 修改nginx配置,并重新加载

    $ vi /etc/nginx/nginx.conf
    # look like this, exposes my repo to internet
    server {
    listen 80;
    server_name mirror.t.com;
    root /usr/local/local_mirror;
    access_log /var/log/nginx/access.log main;
    location / {
    autoindex on; #开启nginx目录浏览功能
    autoindex_exact_size off; #文件大小从KB开始显示
    autoindex_localtime on; #显示文件修改时间为服务器本地时间
    }
    }
    # You can configure SSL certificate later.
    # 查看防火墙状态
    systemctl status firewalld.service
    # 防火墙如果启用,则需开放 80 端口
    # 开放服务器 80 端口,permanent 表示永久有效
    firewall-cmd --add-service=http --permanent
    firewall-cmd --add-port=80/tcp --permanent
    # 重启防火墙
    firewall-cmd --reload
    $ nginx -s reload
    #尝试域名访问
  3. 确定镜像源,确定需要的软件包

  • 原始源:http://mirror.centos.org/centos

  • 清华源:https://mirrors.tuna.tsinghua.edu.cn/centos

  • 阿里源:http://mirrors.aliyun.com/repo/Centos-7.repo

  • 网易源:http://mirrors.163.com/.help/CentOS7-Base-163.repo

    我需要的是 CentOS 7 ,包括:

  • epel

  • centos7-base

  • centos7-updates

  • centos7-extras

  • centos7-centosplus

  • centos****7.9.2009****-base

  • centos****7.9.2009****-updates

  • centos****7.9.2009****-extras

  • centos****7.9.2009****-centosplus

# 确定源
rsync://mirrors.tuna.tsinghua.edu.cn/epel/7/
# 新建本地目录
/usr/local/local_mirror/epel/7/
# 如果你仔细查看源站中的目录结构会发现,里面很有许多你不需要的内容,尤其是安装镜像等极其庞大的文件。那么我们建立一个不同步文件与文件夹列表:
vi /usr/local/local_mirror/exclude.list
#填入内容
SRPMS
aarch64
ppc64
ppc64le
debug
repodata
EFI
LiveOS
images
isolinux
CentOS_BuildTag
EULA
GPL
RPM-GPG-KEY-CentOS-7
RPM-GPG-KEY-CentOS-Testing-7
drpms
# 方便运行,我们建一个简易的Shell脚本
vi /usr/local/local_mirror/rsync.sh
# 填入内容
#epel
rsync -avz --exclude-from=/usr/local/local_mirror/exclude.list rsync://mirrors.tuna.tsinghua.edu.cn/epel/7/ /usr/local/local_mirror/epel/7/
createrepo /usr/local/local_mirror/epel/7/
#centos7-base
rsync -avz --exclude-from=/usr/local/local_mirror/exclude.list rsync://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/ /usr/local/local_mirror/centos/7/os/x86_64/
createrepo /usr/local/local_mirror/centos/7/os/x86_64/
#centos7-updates
rsync -avz --exclude-from=/usr/local/local_mirror/exclude.list rsync://mirrors.tuna.tsinghua.edu.cn/centos/7/updates/x86_64/ /usr/local/local_mirror/centos/7/updates/x86_64/
createrepo /usr/local/local_mirror/centos/7/updates/x86_64/
#centos7-extras
rsync -avz --exclude-from=/usr/local/local_mirror/exclude.list rsync://mirrors.tuna.tsinghua.edu.cn/centos/7/extras/x86_64/ /usr/local/local_mirror/centos/7/extras/x86_64/
createrepo /usr/local/local_mirror/centos/7/extras/x86_64/
#centos7-centosplus
rsync -avz --exclude-from=/usr/local/local_mirror/exclude.list rsync://mirrors.tuna.tsinghua.edu.cn/centos/7/centosplus/x86_64/ /usr/local/local_mirror/centos/7/centosplus/x86_64/
createrepo /usr/local/local_mirror/centos/7/centosplus/x86_64/
#centos7.9.2009-base
rsync -avz --exclude-from=/usr/local/local_mirror/exclude.list rsync://mirrors.tuna.tsinghua.edu.cn/centos/7.9.2009/os/x86_64/ /usr/local/local_mirror/centos/7.9.2009/os/x86_64/
createrepo /usr/local/local_mirror/centos/7.9.2009/os/x86_64/
#centos7.9.2009-updates
rsync -avz --exclude-from=/usr/local/local_mirror/exclude.list rsync://mirrors.tuna.tsinghua.edu.cn/centos/7.9.2009/updates/x86_64/ /usr/local/local_mirror/centos/7.9.2009/updates/x86_64/
createrepo /usr/local/local_mirror/centos/7.9.2009/updates/x86_64/
#centos7.9.2009-extras
rsync -avz --exclude-from=/usr/local/local_mirror/exclude.list rsync://mirrors.tuna.tsinghua.edu.cn/centos/7.9.2009/extras/x86_64/ /usr/local/local_mirror/centos/7.9.2009/extras/x86_64/
createrepo /usr/local/local_mirror/centos/7.9.2009/extras/x86_64/
#centos7.9.2009-centosplus
rsync -avz --exclude-from=/usr/local/local_mirror/exclude.list rsync://mirrors.tuna.tsinghua.edu.cn/centos/7.9.2009/centosplus/x86_64/ /usr/local/local_mirror/centos/7.9.2009/centosplus/x86_64/
createrepo /usr/local/local_mirror/centos/7.9.2009/centosplus/x86_64/
# 赋予脚本权限
chmod +x /usr/local/local_mirror/rsync.sh
# 运行
$ /usr/local/local_mirror/rsync.sh
  1. 等待同步直到完成。。时间的长短取决于你的网络速度
  2. 我有一个基本可用的源了,现在修改客户机上的源设置
# 原始源备份
cp -r /etc/yum.repos.d /root/
# 删除源内容
rm -f /etc/yum.repos.d/*
# 新建home repo
vi /etc/yum.repos.d/home.repo
# 填入
[epel-home]
name=name=Terence homebase mirror
baseurl=http://mirror.t.com/epel/7/x86_64/
enabled=1
gpgcheck=0
[centos7-base]
name=name=Terence homebase mirror
baseurl=http://mirror.t.com/centos/7/os/x86_64/
enabled=1
gpgcheck=0
[centos7-updates]
name=name=Terence homebase mirror
baseurl=http://mirror.t.com/centos/7/updates/x86_64/
enabled=1
gpgcheck=0
[centos7.9.2009-base]
name=name=Terence homebase epel mirror
baseurl=http://mirror.t.com/centos/7.9.2009/os/x86_64/
enabled=1
gpgcheck=0
[centos7.9.2009-updates]
name=name=Terence homebase epel mirror
baseurl=http://mirror.t.com/centos/7.9.2009/updates/x86_64/
enabled=1
gpgcheck=0
# 完成后清除原缓存
yum clean all
# 重建缓存
yum makecache
# 查询一个rpm安装包做验证吧
yum info rxjava
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
可安装的软件包
名称 :rxjava
架构 :noarch
版本 :1.0.13
发布 :2.el7
大小 :698 k
源 :epel-home
简介 : Reactive Extensions for the JVM
网址 :https://github.com/ReactiveX/RxJava
协议 : ASL 2.0
描述 : RxJava a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

附:

  1. 所同步的源,文件大小在32GiB左右。请预留出足够的空间用于存储文件。另外在同步简易shell脚本中的 createrepo 命令可能会导致高CPU使用率,第一次建立repo数据库后可以使用:
createrepo --update
这个命令来更新数据库,而不需要重新建立。也可考虑做成定时执行文件,定期更新。
  1. 同步完的源是否存在包损坏?如何验证呢?

    网上类似文章虽多,但基本很少有对镜像和源进行校验,所以还是同样做个备忘。

    先把下载好的 iso 镜像做个校验(非必要,但建议),sha256sum 可在当前版本下的 sha256sum.txt 查看

    查看镜像的 sha256sum:

    # macOS
    openssl dgst -sha256 ~/Downloads/CentOS-7-x86_64-Everything-1908.iso
    # Linux
    sha256sum CentOS-7-x86_64-Everything-1908.iso
    # Windows
    certutil -hashfile ./CentOS-7-x86_64-Everything-1908.iso SHA256
  2. 如需单独的安装包,建议手动下载上传到指定文件夹即可。

参考:

使用rsync搭建适用于centos的源镜像

搭建CentOS在线yum源镜像服务器

同步官方源的可参考:

6. 搭建同步官方的yum源服务器 — Poppy 1.0.0 documentation

先外网再内网,脚本定期同步外网源(可参考脚本):

CentOS 搭建内部Yum源同步阿里Yum源_ITPUB博客

可参考yum服务的数量检查:

搭建不需要外网的局域网内yum源 - HongxuWang's Blog

httpd , iso和rpm两种方式:

基于Linux系统的本地Yum源搭建与配置(ISO方式、RPM方式) - 墨天轮


如需搭建内网YUM源,可参考:

使用 CentOS 搭建内网 YUM 私有源:镜像篇 | 长风

在局域网内搭建Yum私源

Linux 系统搭建离线软件源

Linux从配置本地yum源到局域网内yum源共享 - FreeBuf网络安全行业门户

All rights reserved
Except where otherwise noted, content on this page is copyrighted.