Centos误删yum重装方法

发布于 2022-09-13  1481 次阅读


AI 摘要

本文介绍了在Centos系统中误删yum后重装的方法。首先,作者提到了一种潜在危险的代码行,当误删了python包后导致yum无法正常运行时,可能会让人感到慌乱。但是作者建议保持冷静,并提供了一系列重装yum的步骤。先卸载已损坏的python和yum,然后检查系统版本,安装相应版本的python作为yum的依赖,下载并安装对应的python rpm包,最后安装yum。作者还提供了一个更简便的方法,即通过阿里云镜像直接安装所需的依赖包。整个过程详细介绍了如何重新安装python和yum,以修复误删后的问题。

部署项目遇到python版本过低,需要安装更高版本时,很多网站会留下这样一行神秘代码:

 rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps

如果你不假思索地将它复制粘贴到命令行窗口并敲下回车键,某件非常致命的事就在这一瞬间发生了,你很可能还毫不知情

不久之后,你端着咖啡翘着大腿,与旁边的同学谈笑风生,却在执行yum install 命令时,突然看到这样的报错提示

-bash: /usr/bin/yum: No such file or directory

yum命令怎么突然用不了了!!!!!!!!

没错,那行命运般的代码被执行时,一切python相关的文件(当然也包括依赖python运行的yum),已经化为了一缕青烟(雾)

这个时候,强装镇定的你心里很可能已经开始盘算重装系统的事(慌了慌了)

且慢!

伟大的文学家、思想家鲁迅曾说过

「我知道你很急,但你先别急」

不愧是鲁迅,早在近百年前就提醒我们遇事沉着冷静不要慌,实乃真知灼见!

其实事情还有挽回的余地

咳咳,那么,下面介绍一种让yum重振雄风的方法


1.卸载python和yum

治疗之前首先是将已经坏死的组织(python yum)割掉

# 删除python

rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps

whereis python |xargs rm -frv

whereis python 

# 删除yum

rpm -qa|grep yum|xargs rpm -ev --allmatches --nodeps 

whereis yum |xargs rm -frv

whereis yum

使用上述代码确保之前安装的python和yum已被完全移除,whereis python和whereis yum应无任何返回结果


2.查看系统版本

cat /etc/redhat-release

查看Centos版本,此步骤非常重要,后续下载内容务必按照当前系统版本


3.安装python

安装系统版本对应python,作为yum的依赖(以python2.6为例)

wget http://www.python.org/ftp/python/2.6/Python-2.6.tgz #下载2.6版本python

tar xzf Python-2.6.tgz #解压python

cd Python-2.6 #进入安装目录

./configure --prefix=/usr/local/python2.6

make && make install  #编译安装python

ln -sf  /usr/local/python2.6/bin/python2.6  /usr/bin/python  #创建链接

python  -V #检查python版本

如果最后一步顺利返回python2.6版本号,恭喜你,python安装成功!


4.下载python rpm包

在下载网址http://vault.centos.org/中找到对应版本的rpm包下载,rpm在对应版本os/x86_64/Packages文件夹下

需要下载的文件包如下

  • python-2.6.6-66.el6_8.x86_64.rpm
  • python-devel-2.6.6-66.el6_8.x86_64.rpm
  • python-iniparse-0.3.1-2.1.el6.noarch.rpm
  • python-libs-2.6.6-66.el6_8.x86_64.rpm
  • python-pycurl-7.19.0-9.el6.x86_64.rpm
  • python-urlgrabber-3.9.1-11.el6.noarch.rpm
  • rpm-python-4.8.0-55.el6.x86_64.rpm
  • yum-3.2.29-81.el6.centos.noarch.rpm
  • yum-metadata-parser-1.1.2-16.el6.x86_64.rpm
  • yum-plugin-fastestmirror-1.1.30-40.el6.noarch.rpm

文件名中的数字与系统版本有关,需要严格按照自己系统版本进行下载


5.上传并安装rpm包

在自己喜欢的地方建立一个python文件夹,如local的src下

mkdir /usr/local/src/python

将刚刚下载的文件上传至python文件夹中并安装

#这里建议先安装 python-*的文件,再安装rpm-python-*的文件,最后安装yum-*的文件

rpm -ivh python-*

rpm -ivh rpm-python-*

rpm -ivh yum-*


6.安装yum

wget http://yum.baseurl.org/download/3.4/yum-3.4.3.tar.gz


tar zxvf yum-3.4.3.tar.gz


cd yum-3.4.3


./yummain.py install yum

大功告成!!!


此外,还有一种更加简便的方法

以笔者使用的Centos7.9.2009为例,直接使用阿里云镜像安装依赖

wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/lvm2-python-libs-2.02.187-6.el7.x86_64.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/libxml2-python-2.9.1-6.el7.5.x86_64.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages//python-libs-2.7.5-89.el7.x86_64.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-ipaddress-1.0.16-2.el7.noarch.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-backports-1.0-8.el7.x86_64.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-2.7.5-89.el7.x86_64.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-iniparse-0.4-9.el7.noarch.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-pycurl-7.19.0-19.el7.x86_64.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-urlgrabber-3.10-10.el7.noarch.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-setuptools-0.9.8-7.el7.noarch.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-kitchen-1.1.1-5.el7.noarch.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-chardet-2.2.1-3.el7.noarch.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/rpm-python-4.11.3-45.el7.x86_64.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/yum-utils-1.1.31-54.el7_8.noarch.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/yum-3.4.3-168.el7.centos.noarch.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/yum-plugin-aliases-1.1.31-54.el7_8.noarch.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/yum-plugin-protectbase-1.1.31-54.el7_8.noarch.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm


rpm -Uvh --replacepkgs lvm2-python-libs*.rpm --nodeps --force
rpm -Uvh --replacepkgs libxml2-python*.rpm --nodeps --force
rpm -Uvh --replacepkgs python*.rpm --nodeps --force
rpm -Uvh --replacepkgs rpm-python*.rpm yum*.rpm --nodeps --force

执行完毕后,使用python -V和yum --version 就能在终端中看到对应版本信息


yum安装完毕后,使用yum install命令时会出现下面的错误信息

已加载插件:fastestmirror, langpacks
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
To enable custom repositories:
yum-config-manager --enable

这是因为repo配置文件也被删除了,重新建立repo的方法见CentOS镜像使用帮助

参考文献

  1. centos7.9.2009重装python和yum
  2. linux误删yum、python以后重装
  3. CentOS镜像使用帮助