马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
一、问题背景
在 CentOS 上安装 gcc、nginx 等软件前,执行 yum 时常遇到 Could not resolve host: mirrorlist.centos.org 这类报错。它通常不是单一原因,而是网络、镜像源、DNS 三类问题叠加,下面逐项排查。
二、网络问题排查
虚拟机先确认网卡模式与状态。NAT 模式一般默认可用,但要确保启用了 DHCP;主机(Host-only)模式则需手动配置好对应的 IP 段。
cat /etc/sysconfig/network-scripts/ifcfg-ens33
确认网络后,按需重启网络服务(三选一,按你实际使用的管理方式):
sudo systemctl restart network
sudo nmcli networking on
sudo systemctl restart NetworkManager
三、镜像源配置
(一)先备份原有仓库配置,避免改坏后无法恢复:
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
(二)确认 CentOS 版本,再选对应的源:
cat /etc/centos-release
hostnamectl
(三)下载对应版本的 CentOS-Base.repo 到 /etc/yum.repos.d/。CentOS 7 可用阿里云源:
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
CentOS 8 需指向 vault(官方已停止维护):
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
CentOS 6 同样指向 vault:
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo
(四)手动更新缓存:
sudo yum clean all
sudo yum makecache
四、DNS 配置
若镜像源地址本身能连通、但仍报 resolve host,多半是 DNS 解析问题。检查 /etc/resolv.conf:
cat /etc/resolv.conf
正常应看到类似内容:
nameserver 8.8.8.8
nameserver 8.8.4.4
若缺失或不正确,用 vim / nano 编辑该文件,补上可用的 nameserver 即可:
vim /etc/resolv.conf
qidao123.com企服评测-应用市场 编辑提示:CentOS 8 / 6 官方仓库已 EOL,直接 yum 会报 resolve 错误,必须改用 vault 或第三方镜像源;生产环境建议对接内部统一的 yum 源,避免每台机器都直连公网镜像。 |