背景
CentOS 7 已于2024年6月30日停止维护,在停止维护后我们之前配置的国内镜像源大多都是空目录了,即在线国内镜像源不可用,就像下边这样提示:
[root@bogon yum.repos.d]# yum install vim
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
One of the configured repositories failed (未知),
and yum doesn't have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work "fix" this:
1. Contact the upstream for the repository and get them to fix the problem.
2. Reconfigure the baseurl/etc. for the repository, to point to a working
upstream. This is most often useful if you are using a newer
distribution release than is supported by the repository (and the
packages for the previous distribution release still work).
3. Run the command with the repository temporarily disabled
yum --disablerepo=<repoid> ...
4. Disable the repository permanently, so yum won't use it by default. Yum
will then just ignore the repository until you permanently enable it
again or use --enablerepo for temporary usage:
yum-config-manager --disable <repoid>
or
subscription-manager repos --disable=<repoid>
5. Configure the failing repository to be skipped, if it is unavailable.
Note that yum will try to contact the repo. when it runs most commands,
so will have to try and fail each time (and thus. yum will be be much
slower). If it is a very temporary problem though, this is often a nice
compromise:
yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true
Cannot find a valid baseurl for repo: base/7/x86_64
那么有没有办法可以继续使用在线镜像源呢?
有的,还不只一种,强烈推荐后两种,我们一起看看。
解决方法
方法1、本地搭建镜像源
作为内网环境使用yum源的方法备用。
1、下载centos7 ISO镜像,下载地址 https://mirrors.huaweicloud.com/centos-vault/7.9.2009/isos/x86_64/ ,推荐选择Everything版本。
2、上传镜像到服务器,以/root目录举例
3、root用户执行命令挂载创建本地镜像源,镜像源目录以/mnt/repo举例
#备份
mkdir /etc/yum.repo.d/bak
mv /etc/yum.repo.d/*.repo /etc/yum.repo.d/bak/
cp /etc/fstab /etc/fstab.bak
#创建挂载目标目录
mkdir /mnt/repo
#临时挂载,ISO只能只读挂载
mount -o loop /root/CentOS-7-x86_64-Everything-2009.iso /mnt/repo
#创建本地镜像源配置
cat > /etc/yum.repo.d/local.repo<<EOF
[centos-test]
name=centos-test
baseurl=file:///mnt/repo
enable=1
gpgcheck=0
EOF
#更新yum缓存,验证安装
yum clean all && yum makecache
yum install -y vim
#开机自动挂载
echo "/root/CentOS-7-x86_64-Everything-2009.iso /mnt/repo iso9660 loop,defaults 0 0" >> /etc/fstab
方法2、使用阿里镜像源【推荐】
不得不说,阿里的确汇集了很多国内运维人才,目前只看到它家可以镜像源不变就能继续用。
阿里镜像源地址:https://mirrors.aliyun.com/centos/7.9.2009/
#备份
mkdir /etc/yum.repo.d/bak
mv /etc/yum.repo.d/*.repo /etc/yum.repo.d/bak/
#下载镜像源配置
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
#更新yum缓存,验证安装
yum clean all && yum makecache
yum install -y vim
方法3、使用centos-valut镜像【推荐】
valut有保险的意思,旧版本都会归到这个镜像源中,这里推荐阿里和华为的镜像源,示例以华为镜像源演示(不为别的,就为下载快!)
华为centos-valut镜像源:https://mirrors.huaweicloud.com/centos-vault/
#备份
mkdir /etc/yum.repo.d/bak
mv /etc/yum.repo.d/*.repo /etc/yum.repo.d/bak/
#下载镜像源配置
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.huaweicloud.com/artifactory/os-conf/centos/centos-7.repo
#更新yum缓存,验证安装
yum clean all && yum makecache
yum install -y vim
全文完,如有帮助麻烦点个赞,我是hellxz,下文见。