欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

终极yum源指南:本地、网络和共享源配置全覆盖!

最编程 2024-02-14 22:05:04
...

2.1 网络yum源配置

我们通常使用 yum install 命令来在线安装 linux系统的软件, 这种方式可以自动处理依赖性关系,并且一次安装所有依赖的软件包。众所周知,从国外下载软件速度非常慢,很多情况下都无法下载。国内一些大公司做镜像同步国外的软件, 那么我们可能使用国内的下载地址,就能下载我们需要的软件。

2.1.1 yum仓库配置

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

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

yum install -y tree

发现无法安装

image-20230312150648398

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

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

cat /etc/redhat-release		

此处我的linux版本为centos7.6

image-20230312144833124

打开华为云开源镜像站

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

image-20230312145037599

image-20230312145353265

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

image-20230312145513474

或者执行下列命令

cd /etc/yum.repos.d/
vi local.repo
# 按i输入下列内容
# 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-7

2.1.2 更新yum源

#输入完成后按Esc ,然后输入:,wq,回车

执行下面指令

yum clean all # 清除系统所有的yum缓存
yum makeacache # 生成新的yum缓存
yum repolist

image-20230312150044576

image-20230312150102488

image-20230312150114706

再次尝试安装tree

yum install -y tree

image-20230312150551116

安装成功,网络yum源配置完成

2.1.3centos yum源一键配置命令

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 makecache

2.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/
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

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

mount -o loop /mnt/CentOS* /mnt/centos72

2.2.2 repo文件配置

进入etc/yum.repos.d/目录,

将之前的repoll文件进行备份

mv * ..

然后创建新的repo源,输入以下命令

mv -r * ..


echo "
[local]
name=bendiyum
baseurl=file:///mnt/centos72/
enabled=1
gpgcheck=0" > local.repo

yum clean all
yum makecache
yum repolist

配置完成

image-20230312171716960

尝试使用yum

yum install -y tree

image-20230312171758375

本地yum源配置完成

2.2.3 centos本地yum源一键配置

只需要输入下面命令即可

当服务器可以访问公网时

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目录,然后执行下面命令

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 repolist

2.3局域网共享yum源配置

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

2.3.1 httpd介绍

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

image-20230312174802894

2.3.2 安装并启动httpd

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

yum install -y httpd 

image-20230312175512891

安装后启动httpd,关闭防火墙

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

ifconfig	

image-20230312175613198

在浏览器输入本机ip,出现下面页面代表配置完成image-20230312175636829

httpd服务的目录文件在/var/www/html下面

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

image-20230312175834888

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

mv /mnt/* centosiso.iso
mount /mnt/centosiso.iso /var/www/html/centosiso

image-20230312180330154

在浏览器访问http://192.168.220.129/centosiso/

image-20230312181022929

2.3.3访问机配置yum

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

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也可以使用同样方法