linux无法ping通外网,yum无法使用,yum本地源,ifconfig无命令,centos7挂载centos7

文章目录

1. 问题描述

  • 刚到手一台CentOS7的虚拟机,虽然有root权限,但发现ifconfig提示:未找到命令,第一反应没有安装net-tools,然后yum install net-tools,报以下错误
已加载插件: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

  • 通过网上查找,大多数都说是网络配置问题,然后查看了/etc/sysconfig/network-scripts/ifcfg-ensxxx文件,发现网卡设置都没问题
  • 然后尝试了ping命令,发现ping不通外网(当然内网是可以ping通的)
  • 到此发现可能只是yum出现了问题,由于不能ping通外网,所以yum会使用本地yum源(本地yum仓库),然后去寻找yum配置文件,一般位于/etc/yum.repos.d/CentOS-Base.repo这个配置文件,打开发现该配置文件和另外一台可以正常yum的内网服务器配置文件一样。说明不是该配置文件的问题
  • 到目前为止,无法排查出到底哪里配置了yum,于是暴力将另外一台好的服务器和yum有关的文件拷贝到目前这台有问题的服务器。拷贝的文件夹有两个,一个是:/etc/yum.repos.d,另一个是/var/cache/yum/x86_64。
  • 拷贝过来还是不行,问题可能有很多,一是当前这台服务器没有配置yum本地源,二是虽然有本地yum仓库,但是某个配置文件没有指向该仓库。目前能想到的可能是这两个原因。

2. 解决方案

  • 由于无法准确定位到问题所在,故采用一种比较暴力的方法:通过CentOS-7-x86_64-Everything-2003.iso(该镜像文件是全系统文件,包含了一些yum源,可以理解为将各种rpm包封装到了里面,还包含一些内置的java之类的)挂载到当前轻量级的centos服务器下面。具体步骤如下:

    • 下载CentOS-7-x86_64-Everything-2003.iso镜像

    • 将该镜像放到当前有问题服务器的某个目录下,目前我放到了/home下

    • 挂载CentOS-7-x86_64-Everything-2003.iso:使用mount命令mount -o loop /home/CentOS-7-x86_64-Everything-2003.iso /mnt/cdron,其中/mnt/cdron是目录的意思(不是/mnt/cdron分区),如果/mnt下面没有cdron,先mkdir cdron该文件夹。该目录不是固定的,不过一般放到/mnt下。
      linux无法ping通外网,yum无法使用,yum本地源,ifconfig无命令,centos7挂载centos7

    • 挂载成功后,可以看到ls /mnt/cdron可以看到有相应的文件了。
      linux无法ping通外网,yum无法使用,yum本地源,ifconfig无命令,centos7挂载centos7

    • 将/mnt/cdron/目录下的所用文件复制到/opt/yumCache下,yumCache是我新建的文件夹,为了防止/mnt/cdron下的文件都放到/opt下显得很乱。cp -r /mnt/cdron/* /opt/yumCache
      linux无法ping通外网,yum无法使用,yum本地源,ifconfig无命令,centos7挂载centos7

    • 切换到/etc/yum.repos.d/目录下,并新建一个centos-Base文件夹(文件夹名自定义),并将/etc/yum.repos.d/所有文件都移动到centos-Base文件夹下,主要是为了进行备份。
      linux无法ping通外网,yum无法使用,yum本地源,ifconfig无命令,centos7挂载centos7

    • 在/etc/yum.repos.d/目录下vi /etc/yum.repos.d/local-yum.repo(作用是新建并编辑local-yum.repo文件),填写以下内容:

    [base-local]
    name=CentOS-local   # 仓库名称
    baseurl=file:///mnt/cdron   # 挂载地址
    enabled=1 
    gpgcheck=1 
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    
    
    • 清理缓存:yum clean all,如果提示,直接rm -rf /var/cache/yum
      linux无法ping通外网,yum无法使用,yum本地源,ifconfig无命令,centos7挂载centos7

    • 然后yum makecache
      linux无法ping通外网,yum无法使用,yum本地源,ifconfig无命令,centos7挂载centos7

    • yum install vim进行测试,发现问题终于解决了
      linux无法ping通外网,yum无法使用,yum本地源,ifconfig无命令,centos7挂载centos7

  • 下面按顺序整理了完整的代码命令:

mkdir /mnt/cdron  # 创建文件夹
mount -o loop /home/CentOS-7-x86_64-Everything-2003.iso /mnt/cdron  # 挂载镜像
ls /mnt/cdron  # 查看是否挂载成功
mkdir /opt/yumCache  # 创建yumCache文件夹
cp -r /mnt/cdron/* /opt/yumCache/
cd /etc/yum.repos.d/  # 切换目录
mkdir centos-Base  # 新建文件夹
mv ./*.repo centos-Base/
vi /etc/yum.repos.d/local-yum.repo  # 内容见上
yum clean all
yum makecache
yum install vim

3. 思考

以上我的解决方式,其实是创建自己的yum本地仓库,只不是采用的是挂载centos-everything镜像的方式,个人感觉可以不挂载centos-everything镜像,而是直接找一个yum源镜像,然后挂载,具体可以参考博客https://blog.csdn.net/xys2333/article/details/83349501该博客的方法我没有亲自尝试,但可以当做一个备选方案,有时间可以搞搞

上一篇:用createrepo命令创建自己的yum源


下一篇:3、Azure Devops之Azure Repos篇