CentOS7中yum命令无法使用且resolv.conf重启后失效

本文讲述了在新装CentOS 7系统中遇到的vim安装问题,由于链接404,经排查发现是网卡配置不正确。解决办法包括修改ifcfg-ens33配置或锁定resolv.conf文件。最终,通过设置静态IP和DNS,成功解决了vim安装问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

新安装的CentOS7系统,之后进行 yum install -y vim 安装vim命令时发现,所有的链接都是404状态导致vim无法正常安装,错误如下:

One of the configured repositories failed (Unknown),
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. Disable the repository, 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>

     4. 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:

问题解决

查找一圈问题后发现是网卡配置的问题,我们可以查看 cat /etc/resolv.conf 文件发现只有一个宿主机ip
在这里插入图片描述
此时我们添加一个 nameserver 8.8.8.8 进去后重新加载网卡 service network restart 应该就可以了,但是偏偏重启后发现还是一样的错误,我们再次查看 /etc/resolv.conf 文件时发现这个文件又初始化了。
至此,根源问题找到了,因为 /etc/resolv.conf 这个文件是由 /etc/sysconfig/network-scripts/ifcfg-ens33 生成的,所以每次重启的时候会重新生成一个文件。根据以上我们解决方式有两种:
方法一: 修改/etc/sysconfig/network-scripts/ifcfg-ens33文件

DEVICE=ens33
BOOTPROTO=none                 //DHCP自动或者手动获取ip
HWADDR=00:0c:29:f4:63:83
IPV6INIT=yes
NM_CONTROLLED=yes
ONBOOT=yes
TYPE=Ethernet
UUID="17348826-0ae4-40e1-9b43-4ddc3fd5dae4"
IPADDR=192.168.216.128         //ip
NETMASK=255.255.255.0            
GATEWAY=192.168.216.2         //网关
DNS1=192.168.216.2           //①修改DNS
USERCTL=no
PEERDNS=no                  //②默认为yes,修改为no之后则不会在重启之后更新resolv

方法二: 直接修改后锁定文件/etc/resolv.conf

  1. 编辑文件 resolv.conf 添加 一行 nameserver 8.8.8.8
  2. 锁定文件
    chattr +i /etc/resolv.conf //resolv被锁定,即使reroot也不能修改
  3. 如果要修改锁定后的文件可执行
    chattr -i /etc/resolv.conf

图省事儿用的方法二,目前没什么其他问题。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值