@toc

1. 引言1.1 什么是yum源yum(全称为 Yellow dog Updater, Modified)是一个在 Fedora ,centos,BClinux,RedHat 以及 CentOS 中的 Shell 前端软件包管理器。基于 RPM 包管理 , 能够从指定的服务器自动下载 RPM 包并且安装 , 可以自动处理依赖性关系 , 并且一次安装所有依赖的软件包 , 无须繁琐地一次次下载、安装。

我们可以使用 yum 工具通过 http,ftp 等协议去下载软件包 ,yum 提供了查找、安装、删除某一个、一组甚至全部软件包的命令,让你很方便地使用命令直接管理软件包 ( 安装 , 卸载 , 更新 rpm 包 ), 甚至是 kernel 的更新。也可以将其理解为红旗环境下的 apt 管理工具。

image-202303121431044231.2 为什么要搭建内网yum源在生产和工作中,出于安全性考虑,很多服务器并不能直接访问公网,当使用rpm安装包安装一些软件时,经常出现缺少依赖而不能安装的情况,此时我们需要配置本地yum源,配置本机yum源相对容易,一般只需要将系统镜像上传到主机后进行挂载,但是当内网内多台机器需要配置yum源时,再一台一台的配置单机yum源就十分麻烦和耽误时间,下面将分别介绍如何搭建网络yum源,本机单机yum源和内网共享yum源。

1.3 准备工作在搭建yum源之前,我们需要至少准备一台服务器配置网络yum源,只需要一台服务器

配置单机本地yum源,只需要一台服务器

配置本地共享yum源,至少需要两台可以ping通的服务器

要升级的yum源iso镜像,当搭建本地yum源时,需要提前下载好要配置的yum镜像掌握mount命令2. yum源搭建2.1 网络yum源配置我们通常使用 yum install 命令来在线安装 linux系统的软件, 这种方式可以自动处理依赖性关系,并且一次安装所有依赖的软件包。众所周知,从国外下载软件速度非常慢,很多情况下都无法下载。国内一些大公司做镜像同步国外的软件, 那么我们可能使用国内的下载地址,就能下载我们需要的软件。

2.1.1 yum仓库配置yum 的配置文件在 /etc/yum.repos.d 目录下, 其中有多个配置文件,每一个配置文件中都可以配置一个或多个repository, 但是最终会被合并为一个交给系统,所以多个文件只是为了方便管理。

当我们登录我们的纯净服务器后,尝试命令行

代码语言:shell复制yum install -y tree发现无法安装

image-20230312150648398这时,我们需要配置网络yum源,国内有很多公司提供了yum源,我们只需要选择其中一个即可,这里以华为云yum源为例。

首先输入指令查看本地操作系统版本

代码语言:shell复制cat /etc/redhat-release 此处我的linux版本为centos7.6

image-20230312144833124打开华为云开源镜像站

https://mirrors.huaweicloud.com/home,找到我们对应的操作系统版本:https://repo.huaweicloud.com/repository/conf/

image-20230312145037599image-20230312145353265找到对应的.repo文件,下载后上传到服务器对应的/etc/yum.repos.d/目录,

image-20230312145513474或者执行下列命令

代码语言:shell复制cd /etc/yum.repos.d/

vi local.repo

# 按i输入下列内容代码语言:shell复制# CentOS-Base.repo

#

# The mirror system uses the connecting IP address of the client and the

# update status of each mirror to pick mirrors that are updated to and

# geographically close to the client. You should use this for CentOS updates

# unless you are manually picking other mirrors.

#

# If the mirrorlist= does not work for you, as a fall back you can try the

# remarked out baseurl= line instead.

#

#

[base]

name=CentOS7

baseurl=https://repo.huaweicloud.com/centos/$releasever/os/$basearch/

gpgcheck=0

#released updates

[updates]

name=CentOS-$releasever - Updates - repo.huaweicloud.com

baseurl=https://repo.huaweicloud.com/centos/$releasever/updates/$basearch/

#mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates

gpgcheck=1

gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful

[extras]

name=CentOS-$releasever - Extras - repo.huaweicloud.com

baseurl=https://repo.huaweicloud.com/centos/$releasever/extras/$basearch/

#mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras

gpgcheck=1

gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages

[centosplus]

name=CentOS-$releasever - Plus - repo.huaweicloud.com

baseurl=https://repo.huaweicloud.com/centos/$releasever/centosplus/$basearch/

#mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus

gpgcheck=1

enabled=0

gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-72.1.2 更新yum源#输入完成后按Esc ,然后输入:,wq,回车

执行下面指令

代码语言:shell复制yum clean all # 清除系统所有的yum缓存

yum makeacache # 生成新的yum缓存

yum repolistimage-20230312150044576image-20230312150102488image-20230312150114706再次尝试安装tree

代码语言:shell复制yum install -y treeimage-20230312150551116安装成功,网络yum源配置完成

2.1.3centos yum源一键配置命令代码语言:shell复制mv /etc/yum.repos.d/* *.bak

echo "[base]

name=CentOS7

baseurl="https://repo.huaweicloud.com/centos/\$releasever/os/\$basearch/"

enabled=1

gpgcheck=0" > local.repo

yum clean all

yum makecache2.1.4 其他yum源参考附centos7yum源地址

1、清华大学yum源

https://mirrors.tuna.tsinghua.edu.cn/help/centos/

2、网易yum源

http://mirrors.163.com/.help/centos.html

3、阿里云yum源

http://mirrors.aliyun.com/repo/Centos-7.repo

4、中科大yum源

https://lug.ustc.edu.cn/wiki/_export/code/mirrors/help/centos?codeblock=3

5、北京理工大学

http://mirror.bit.edu.cn

6、兰州大学

http://mirror.lzu.edu.cn

7、厦门大学

http://mirrors.xmu.edu.cn

8、东北大学

http://mirror.neu.edu.cn

2.2 本机单机yum源配置当服务器无法访问公网或者所需要依赖无法在公网获取,我们可以通过

使用挂载本地iso镜像来获取我们需要的rpm包和依赖

2.2.1 iso镜像获取并上传到服务器由于的的服务器为centos7.6,所以我准备下获取centos7.6的镜像

一下为部分centos下载地址,随意选择一个就可以,切记选择来源安全可靠的镜像下载

http://man.linuxde.net/download/CentOS

http://mirrors.btte.net/centos/7/isos/x86_64/

http://mirrors.cn99.com/centos/7/isos/x86_64/

http://mirrors.sohu.com/centos/7/isos/x86_64/

[[http://mirrors.aliyun.com/centos/7/isos/x86_64/\](http://mirrors.aliyun.com/centos/7/isos/x86_64/)

http://centos.ustc.edu.cn/centos/7/isos/x86_64/](%5Bhttp://mirrors.aliyun.com/centos/7/isos/x86_64/%5D%28http://mirrors.aliyun.com/centos/7/isos/x86_64/%29%20http://centos.ustc.edu.cn/centos/7/isos/x86_64/)

http://mirrors.neusoft.edu.cn/centos/7/isos/x86_64/

http://mirror.lzu.edu.cn/centos/7/isos/x86_64/

http://mirrors.163.com/centos/7/isos/x86_64/

http://ftp.sjtu.edu.cn/centos/7/isos/x86_64/

将下载好后的镜像上传到/mnt中

image-20230312162616912在/mnt目录下创建文件夹 centos72

然后输入下面命令进行挂载

代码语言:shell复制mount -o loop /mnt/CentOS* /mnt/centos722.2.2 repo文件配置进入etc/yum.repos.d/目录,

将之前的repoll文件进行备份

代码语言:shell复制cd etc/yum.repos.d/

mv * *.bak然后创建新的repo源,输入以下命令

代码语言:shell复制echo "

[local]

name=bendiyum

baseurl=file:///mnt/centos72/

enabled=1

gpgcheck=0" > local.repo

yum clean all

yum makecache

yum repolist配置完成

image-20230312171716960尝试使用yum

代码语言:shell复制yum install -y treeimage-20230312171758375本地yum源配置完成

2.2.3 centos本地yum源一键配置只需要输入下面命令即可

当服务器可以访问公网时

代码语言:shell复制wget -p /mnt/ -o centos.iso http://mirrors.aliyun.com/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-2009.iso

mkdir /mnt/centos

mount -o loop /mnt/centos.iso /mnt/centos

cd /mnt/yum.repos.d/

mv -r * ..

echo "

[local]

name=bendiyum

baseurl=file:///mnt/centos72/

enabled=1

gpgcheck=0" > local.repo

yum clean all

yum makecache

yum repolist当服务器不可以访问公网时

需要将待挂载的镜像上传到服务器/mnt目录,然后执行下面命令

代码语言:shell复制mv /mnt/* centos.iso

mkdir /mnt/centos

mount -o loop /mnt/centos.iso /mnt/centos

cd /mnt/yum.repos.d/

mv -r * ..

echo "

[local]

name=bendiyum

baseurl=file:///mnt/centos72/

enabled=1

gpgcheck=0" > local.repo

yum clean all

yum makecache

yum repolist2.3局域网共享yum源配置在内网时,当多台机器需要配置本地同一yum源时,我们不需要在每台服务器上单独配置并挂载镜像,只需要在一台服务器上配置好yum源,并通过httpd服务让其他服务器能够正常访问即可

2.3.1 httpd介绍httpd是Apache超文本传输协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程,它会建立一个处理请求的子进程或线程的池。

image-202303121748028942.3.2 安装并启动httpd选择一台服务器作为配置节点,通过yum配置httpd,若无法使用yum,可以下载httpd的rpm包上传后安装。

代码语言:shell复制yum install -y httpd image-20230312175512891安装后启动httpd,关闭防火墙

代码语言:shell复制systemctl start httpd # 启动httpd

systemctl stop firewalld #临时关闭防火墙

systemctl status httpd #查看httpd是否启动外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A4KlmddQ-1678618188009)(https://typorabc.obs.cn-east-3.myhuaweicloud.com/tuchuang//image-20230312175524714.png)

httpd启动成功

使用下面命令查看本机ip

代码语言:shell复制ifconfig image-20230312175613198在浏览器输入本机ip,出现下面页面代表配置完成image-20230312175636829httpd服务的目录文件在/var/www/html下面

进入httpd服务目录,创建文件夹centosiso

image-20230312175834888将要挂载的镜像放在/mnt下面,

代码语言:shell复制mv /mnt/* centosiso.iso

mount /mnt/centosiso.iso /var/www/html/centosisoimage-20230312180330154在浏览器访问http://192.168.220.129/centosiso/

image-202303121810229292.3.3访问机配置yum当在上一台机器已经配置好httpd后,我们选择同一网段内网其他机器配置yum源去访问此台服务器

代码语言:shell复制cd /mnt/yum.repos.d/

mv -r * ..

echo "

[local]

name=net_bendiyum

baseurl=http://192.168.220.129/centosiso/

enabled=1

gpgcheck=0" > local.repo

yum clean all

yum makecache

yum repolist上述命令中的192.168.220.129此处ip地址为挂载镜像的服务器ip

image-20230312181354020配置完成,此时当内网内其他机器需要使用内网yum源可以使用此方法,当然,挂载其他共享yum也可以使用同样方法