Centos7制作本地yum仓库,共享给局域网其他设备

发布时间 2023-07-30 10:12:19作者: 青山001

环境准备:

  • 准备好安装好Centos7的虚机A(服务端)和虚机B(客户端)

  • 配置两台虚机网络使其互通,关闭selinux和firewalld等限制

  • 下载完整的ISO镜像(CentOS-7-x86_64-Everything-2207-02_3.iso):http://isoredirect.centos.org/centos/7/isos/x86_64/

  • ⭐️注意:同步完所有的软件包占用62G空间,虚机A作为离线yum仓库,需要添加一块至少70G容量的硬盘,避免磁盘空间不足

具体步骤

1、配置虚机A的yum源(本次使用的外国语的源,阿里的源因为网络问题同步太慢了,哪个网络速度快用哪个)
  • 方法一、配置阿里的源-----------具体帮助见https://developer.aliyun.com/mirror/

     curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo  #替换CentOS-Base.repo
     curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo  #替换epel.repo
     # 重建yum源缓存
     yum clean all
     yum makecache
  • 方法二、配置外国语的源----------具体帮助见https://mirrors.bfsu.edu.cn/help/centos/

 # 替换CentOS-Base.repo
 sed      -e 's|^mirrorlist=|#mirrorlist=|g' \
          -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirrors.bfsu.edu.cn/centos|g' \
          -i.bak \
          /etc/yum.repos.d/CentOS-*.repo
 # 安装epel源,替换epel.repo
 yum install -y epel-release
 sed -e 's!^metalink=!#metalink=!g' \
     -e 's!^#baseurl=!baseurl=!g' \
     -e 's!https\?://download\.fedoraproject\.org/pub/epel!https://mirrors.bfsu.edu.cn/epel!g' \
     -e 's!https\?://download\.example/pub/epel!https://mirrors.bfsu.edu.cn/epel!g' \
     -i /etc/yum.repos.d/epel*.repo
 # 重建yum源缓存
 yum clean all
 yum makecache
2、创建yum源用到的目录,安装yum仓库用的的软件包
 mkdir -p  /data/soft/centos7  /data/soft/epel   /data/soft/extras  /data/soft/updates      #创建本地yum源需要的目录
 yum install -y yum-utils createrepo              #安装软件包
3、同步yum源(epel,updates,extras),制作本地yum仓库
 reposync -r epel -p /data/soft/epel      #把yum源的软件包,依赖下载到本地yum仓库目录(同步时间比较长)
 reposync -r extras -p /data/soft/extras
 reposync -r updates -p /data/soft/updates
 createrepo -v /data/soft/epel          #创建yum仓库信息
 createrepo -v /data/soft/extras
 createrepo -v /data/soft/updates
 #上传CentOS-7-x86_64-Everything-2207-02_3.iso到虚机中
 mount CentOS-7-x86_64-Everything-2207-02_3.iso /mnt  #挂载镜像到/mnt目录
 cp -a /mnt/*   /data/soft/centos7    #复制镜像的软件包和依赖到本地
 createrepo -v /data/soft/centos7     #创建yum仓库信息
4、配置安装、配置vsftp
 yum install -y vsftpd  #安装vsftpd
 vim /etc/vsftpd/vsftpd #编辑vsftpd配置文件,设置匿名访问,设置匿名访问默认目录
 anonymous_enable=YES
 anon_root=/data/soft
 ##################################
 systemctl enable vsftpd    #设置自启
 systemctl start vsftpd     #启动服务
 systemctl stop firewalld   #临时关闭防火墙
 setenforce 0               #临时关闭selinux
5、移除虚机B的.repo配置文件,测试搭建的yum仓库是否生效
 mv /etc/yum.repos.d/  /opt  #移动目录到/opt
 # 重建本地yum源
 vi /etc/yum.repos.d/local.repo  #新建local.repo配置文件,写入以下内容
 [centos7]
 name=centos
 baseurl=ftp://10.0.0.55/centos7  #IP为虚机A的IP地址
 gpgcheck=0
 enabled=1
 [epel]
 name=epel
 baseurl=ftp://10.0.0.55/epel
 gpgcheck=0
 enabled=1
 [extras]
 name=extras
 baseurl=ftp://10.0.0.55/extras
 gpgcheck=0
 enabled=1
 [updates]
 name=updates
 baseurl=ftp://10.0.0.55/updates
 gpgcheck=0
 enabled=1
 
 #######测试一波
 yum clean all  #清除缓存
 yum makecache  #生成缓存
 yum repolist   #查看yum仓库软件数量
 yum install -y tree  vim wget bash-completion bash-completion-extras lrzsz net-tools sysstat iotop iftop htop unzip nc nmap telnet bc psmisc httpd-tools bind-utils nethogs expect cowsay sl   #安装一些常用软件包验证local.repo文件是否生效