CentOS服务器能ping通却无法yum install:指定镜像源解决

文章目录


前言

今天有一个项目现场要在一个远程centos服务器上部署产品服务,发现能ping通百度,但是无法yum install 安装基础软件包,开始以为DNS服务器的问题,结果配置了也不行,最后通过指定镜像源的方式解决,解决过程记录下,方便后面遇到相同的问题快速解决。


一、问题记录

问题如下,能ping通百度,但是无法yum install :

c 复制代码
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (223.109.82.16) 56(84) bytes of data.
64 bytes from 223.109.82.16 (223.109.82.16): icmp_seq=1 ttl=52 time=13.6 ms
64 bytes from 223.109.82.16 (223.109.82.16): icmp_seq=2 ttl=52 time=13.6 ms
64 bytes from 223.109.82.16 (223.109.82.16): icmp_seq=3 ttl=52 time=13.0 ms
64 bytes from 223.109.82.16 (223.109.82.16): icmp_seq=4 ttl=52 time=15.5 ms
^C
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 13.046/13.991/15.574/0.956 ms
[root@localhost ~]# yum install net-tools -y
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的错误"


 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.修改DNS无效

vi /etc/resolv.conf:

添加公共 DNS 服务器地址:

nameserver 8.8.8.8

c 复制代码
[root@localhost ~]# vi /etc/resolv.conf 
[root@localhost ~]# ping mirrorlist.centos.org
ping: mirrorlist.centos.org: 未知的名称或服务
[root@localhost ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
#nameserver 192.168.1.1
nameserver 8.8.8.8
[root@localhost ~]# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=53 time=46.0 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=53 time=46.2 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=53 time=45.9 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=53 time=46.1 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=53 time=48.9 ms
64 bytes from 8.8.8.8: icmp_seq=6 ttl=53 time=46.1 ms
^C
--- 8.8.8.8 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5005ms
rtt min/avg/max/mdev = 45.925/46.569/48.998/1.125 ms
[root@localhost ~]# nslookup mirrorlist.centos.org 8.8.8.8
-bash: nslookup: 未找到命令
[root@localhost ~]# yum install bind-utils
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的错误"

可以看到在该环境下添加公共 DNS 服务器地址,虽然能ping通8.8.8.8,但是仍然解决不了在线yum install 安装工具的问题。

2.指定镜像源

由于无法解析 mirrorlist.centos.org,可以直接指定一个可用的镜像源。先备份原来的 CentOS-Base.repo 文件:

c 复制代码
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

接着从阿里云下载新的 CentOS-Base.repo 文件:

c 复制代码
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

此处如果简单的用wget 命令也会无法执行:

c 复制代码
[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
-bash: wget: 未找到命令
[root@localhost yum.repos.d]# yum install wget
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
There are no enabled repos.
 Run "yum repolist all" to see the repos you have.
 To enable Red Hat Subscription Management repositories:
     subscription-manager repos --enable <repo>
 To enable custom repositories:
     yum-config-manager --enable <repo>
[root@localhost yum.repos.d]# ls

直接从windows机器上下载了一个Centos-7.repo,然后放到/etc/yum.repos.d/目录下

c 复制代码
[root@localhost yum.repos.d]# ls -l
总用量 36
-rw-r--r--. 1 root root 2523 4月  14 16:28 Centos-7.repo
-rw-r--r--. 1 root root 1664 11月 23 2018 CentOS-Base.repo.backup
-rw-r--r--. 1 root root 1309 11月 23 2018 CentOS-CR.repo
-rw-r--r--. 1 root root  649 11月 23 2018 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 11月 23 2018 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 11月 23 2018 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 11月 23 2018 CentOS-Sources.repo
-rw-r--r--. 1 root root 5701 11月 23 2018 CentOS-Vault.repo

更新缓存:

c 复制代码
yum clean all
yum makecache

再次尝试安装:

c 复制代码
[root@localhost yum.repos.d]# yum install net-tools -y
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 net-tools.x86_64.0.2.0-0.25.20131004git.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

==============================================================================================================================================================================================================================================================================
 Package                                                         架构                                                         版本                                                                           源                                                          大小
==============================================================================================================================================================================================================================================================================
正在安装:
 net-tools                                                       x86_64                                                       2.0-0.25.20131004git.el7                                                       base                                                       306 k

事务概要
==============================================================================================================================================================================================================================================================================
安装  1 软件包

总下载量:306 k
安装大小:917 k
Downloading packages:
警告:/var/cache/yum/x86_64/7/base/packages/net-tools-2.0-0.25.20131004git.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY                                                                                               ]  0.0 B/s |    0 B  --:--:-- ETA 
net-tools-2.0-0.25.20131004git.el7.x86_64.rpm 的公钥尚未安装
net-tools-2.0-0.25.20131004git.el7.x86_64.rpm                                                                                                                                                                                                          | 306 kB  00:00:00     
从 http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 检索密钥
导入 GPG key 0xF4A80EB5:
 用户ID     : "CentOS-7 Key (CentOS 7 Official Signing Key) <[email protected]>"
 指纹       : 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 来自       : http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                                                                                                                                                                 1/1 
  验证中      : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                                                                                                                                                                 1/1 

已安装:
  net-tools.x86_64 0:2.0-0.25.20131004git.el7                                                                                                                                                                                                                                 

完毕!

问题解决!


总结

这就是今天的一次问题解决,虽然是小问题,但是每次记录下,供下次出现问题时参考,能省不少时间,以前解决过很多的问题,当时觉得很清楚,觉得没什么好记录的,但时间长了又忘了,所以不管问题大小,及时总结记录是一个很好的习惯,集少成多。

相关推荐
新辞旧梦4 小时前
企业微信自建消息推送应用
服务器·python·企业微信
虎头金猫4 小时前
如何解决 403 错误:请求被拒绝,无法连接到服务器
运维·服务器·python·ubuntu·chatgpt·centos·bug
muxue1785 小时前
关于almalinux分区配置:
linux·运维·数据库
独行soc7 小时前
2025年渗透测试面试题总结-某服面试经验分享(附回答)(题目+回答)
linux·运维·服务器·网络安全·面试·职场和发展·渗透测试
sunshineine8 小时前
jupyter notebook运行简单程序
linux·windows·python
月月大王8 小时前
easyexcel导出动态写入标题和数据
java·服务器·前端
O。o.尊都假都8 小时前
UDP协议
linux·服务器·网络·网络协议·udp
天夏已微凉9 小时前
1.3.1 Linux音频框架alsa详细介绍
linux·音视频
惜.己9 小时前
linux中的常用命令(一)
linux·运维·服务器
国际云,接待10 小时前
云计算的基础概论
服务器·人工智能·阿里云·云原生·云计算·腾讯云·aws