centos9

误删python恢复方案

centos9误删了/usr/bin/python3.9,导致yum不可用

[root@localhost ~]# yum makecache
-bash: /usr/bin/yum: /usr/bin/python3.9: bad interpreter: No such file or directory
[root@localhost ~]# python3
-bash: /usr/bin/python3: No such file or directory
[root@localhost ~]# python3.9
-bash: /usr/bin/python3.9: No such file or directory

如果仅仅是删了这一个文件并没有做其他任何操作,那么恢复也很简单,此时所有的python相关包仍是已安装状态,只需要找到对应的包卸载再重装即可。

[root@localhost ~]# rpm -qa | grep python
python3-setuptools-wheel-53.0.0-12.el9.noarch
python3-pip-wheel-21.2.3-7.el9.noarch
python-unversioned-command-3.9.18-1.el9.noarch
python3-3.9.18-1.el9.x86_64
python3-libs-3.9.18-1.el9.x86_64
python3-libcomps-0.1.18-1.el9.x86_64
python3-six-1.15.0-9.el9.noarch
python3-dateutil-2.8.1-7.el9.noarch
python3-systemd-234-18.el9.x86_64
libcap-ng-python3-0.8.2-7.el9.x86_64
python3-nftables-1.0.4-11.el9.x86_64
python3-dbus-1.2.18-2.el9.x86_64
python3-gobject-base-noarch-3.40.1-6.el9.noarch
python3-gobject-base-3.40.1-6.el9.x86_64
python3-firewall-1.2.5-1.el9.noarch
python3-libdnf-0.69.0-6.el9.x86_64
python3-hawkey-0.69.0-6.el9.x86_64
python3-gpg-1.15.1-6.el9.x86_64
python3-rpm-4.16.1.3-25.el9.x86_64
python3-dnf-4.14.0-8.el9.noarch
python3-dnf-plugins-core-4.3.0-11.el9.noarch
python3-libselinux-3.5-1.el9.x86_64

显然,我们需要安装python3-3.9.18-1.el9.x86_64,先去下载rpm包,rpm包下载建议使用wget方式,浏览器直接下载,爬虫方式下载得到的rpm安装的时候文件无法识别!

wget https://mirrors.aliyun.com/centos-stream/9-stream/BaseOS/x86_64/os/Packages/python3-3.9.18-1.el9.x86_64.rpm

# 安装前会提示已安装,需要先卸载
rpm -e --nodeps python3-3.9.18-1.el9.x86_64

rpm -i python3-3.9.18-1.el9.x86_64.rpm

测试python3.9, yum命令正常。

如果不知道误删的文件对应哪个包,这里给出一种粗暴的方案。

  1. rpm获取系统所有安装的python包。

    rpm -qa | grep python > python-rpm.txt
    
  2. 卸载所有的python相关包。

    # 强制删除python已安装程序及其关联
    rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps
    # 删除残余python文件
    whereis python |xargs rm -frv
    验证删除,返回为无结果
    whereis python
    
  3. 重新全部下载并安装所有python包。使用脚本调用wget批量下载

    #!/bin/bash
    
    deps=$(sed 's/\r//g' ./python-rpm.txt)
    download_dir=./python-rpms
    for dep in $deps
    do
      rpm_name=${dep}.rpm
      wget -P $download_dir https://mirrors.aliyun.com/centos-stream/9-stream/BaseOS/x86_64/os/Packages/"$rpm_name"
      if [ ! -f "$download_dir/$rpm_name" ]
      then
        wget -P $download_dir https://mirrors.aliyun.com/centos-stream/9-stream/AppStream/x86_64/os/Packages/"$rpm_name"
      fi
      if [ ! -f "$download_dir/$rpm_name" ]
      then
        echo "$rpm_name 下载失败"
      fi
    done
    
    
  4. 安装所有

    rpm -ivh --nodeps *.rpm
    

不仅仅是centos9,7和8,redhat都可以这样操作。

centos9网络配置

和7略有不同,centos7配置网络在/etc/sysconfig/network-scripts/ifcfg-ensxxx文件中,系统安装后会自动生成文件,centos9中该目录下有个readme文件,说明了这种配置文件已被弃用,新格式的配置文件在/etc/NetworkManager/system-connections/ensxxx.nmconnection

[connection]
id=ens160
uuid=c937dff4-3319-3289-9fea-f640eb95c79e
type=ethernet
autoconnect-priority=-999
interface-name=ens160
timestamp=1699658293

[ethernet]

[ipv4]
# 手动模式
method=manual
# dns,多个使用分号隔开
dns=192.168.8.2
# 网关
gateway=192.168.8.2
# ip/掩码,多个地址分号隔开
address=192.168.8.3/24

[ipv6]
addr-gen-mode=eui64
method=auto

[proxy]

配置完成后重启NetworkManager

systemctl restart NetworkManager

centos9镜像源

centos9还比较新,网上没有找到比较全的镜像源配置,这里贴一个我自己配置的阿里源。

centos-aliyun.repo

[baseos]
name=CentOS Stream $releasever - BaseOS
# metalink=https://mirrors.centos.org/metalink?repo=centos-baseos-$stream&arch=$basearch&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/BaseOS/$basearch/os/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=1

[baseos-debuginfo]
name=CentOS Stream $releasever - BaseOS - Debug
# metalink=https://mirrors.centos.org/metalink?repo=centos-baseos-debug-$stream&arch=$basearch&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/BaseOS/$basearch/debug/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[baseos-source]
name=CentOS Stream $releasever - BaseOS - Source
# metalink=https://mirrors.centos.org/metalink?repo=centos-baseos-source-$stream&arch=source&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/BaseOS/source/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[appstream]
name=CentOS Stream $releasever - AppStream
# metalink=https://mirrors.centos.org/metalink?repo=centos-appstream-$stream&arch=$basearch&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/AppStream/$basearch/os/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=1

[appstream-debuginfo]
name=CentOS Stream $releasever - AppStream - Debug
# metalink=https://mirrors.centos.org/metalink?repo=centos-appstream-debug-$stream&arch=$basearch&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/AppStream/$basearch/debug/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[appstream-source]
name=CentOS Stream $releasever - AppStream - Source
# metalink=https://mirrors.centos.org/metalink?repo=centos-appstream-source-$stream&arch=source&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/AppStream/source/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[crb]
name=CentOS Stream $releasever - CRB
# metalink=https://mirrors.centos.org/metalink?repo=centos-crb-$stream&arch=$basearch&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/CRB/$basearch/os/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0

[crb-debuginfo]
name=CentOS Stream $releasever - CRB - Debug
# metalink=https://mirrors.centos.org/metalink?repo=centos-crb-debug-$stream&arch=$basearch&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/CRB/$basearch/debug/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[crb-source]
name=CentOS Stream $releasever - CRB - Source
# metalink=https://mirrors.centos.org/metalink?repo=centos-crb-source-$stream&arch=source&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/CRB/source/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

centos-addons-aliyun.repo

[highavailability]
name=CentOS Stream $releasever - HighAvailability
# metalink=https://mirrors.centos.org/metalink?repo=centos-highavailability-$stream&arch=$basearch&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/HighAvailability/$basearch/os/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0

[highavailability-debuginfo]
name=CentOS Stream $releasever - HighAvailability - Debug
# metalink=https://mirrors.centos.org/metalink?repo=centos-highavailability-debug-$stream&arch=$basearch&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/HighAvailability/$basearch/debug/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[highavailability-source]
name=CentOS Stream $releasever - HighAvailability - Source
# metalink=https://mirrors.centos.org/metalink?repo=centos-highavailability-source-$stream&arch=source&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/HighAvailability/source/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[nfv]
name=CentOS Stream $releasever - NFV
# metalink=https://mirrors.centos.org/metalink?repo=centos-nfv-$stream&arch=$basearch&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/NFV/$basearch/os/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0

[nfv-debuginfo]
name=CentOS Stream $releasever - NFV - Debug
# metalink=https://mirrors.centos.org/metalink?repo=centos-nfv-debug-$stream&arch=$basearch&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/NFV/$basearch/debug/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[nfv-source]
name=CentOS Stream $releasever - NFV - Source
# metalink=https://mirrors.centos.org/metalink?repo=centos-nfv-source-$stream&arch=source&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/NFV/source/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[rt]
name=CentOS Stream $releasever - RT
# metalink=https://mirrors.centos.org/metalink?repo=centos-rt-$stream&arch=$basearch&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/RT/$basearch/os/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0

[rt-debuginfo]
name=CentOS Stream $releasever - RT - Debug
# metalink=https://mirrors.centos.org/metalink?repo=centos-rt-debug-$stream&arch=$basearch&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/RT/$basearch/debug/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[rt-source]
name=CentOS Stream $releasever - RT - Source
# metalink=https://mirrors.centos.org/metalink?repo=centos-rt-source-$stream&arch=source&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/RT/source/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[resilientstorage]
name=CentOS Stream $releasever - ResilientStorage
# metalink=https://mirrors.centos.org/metalink?repo=centos-resilientstorage-$stream&arch=$basearch&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/ResilientStorage/$basearch/os/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0

[resilientstorage-debuginfo]
name=CentOS Stream $releasever - ResilientStorage - Debug
# metalink=https://mirrors.centos.org/metalink?repo=centos-resilientstorage-debug-$stream&arch=$basearch&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/ResilientStorage/$basearch/debug/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[resilientstorage-source]
name=CentOS Stream $releasever - ResilientStorage - Source
# metalink=https://mirrors.centos.org/metalink?repo=centos-resilientstorage-source-$stream&arch=source&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/ResilientStorage/source/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[extras-common]
name=CentOS Stream $releasever - Extras packages
# metalink=https://mirrors.centos.org/metalink?repo=centos-extras-sig-extras-common-$stream&arch=$basearch&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Extras-SHA512
baseurl=https://mirrors.aliyun.com/centos-stream/SIGs/$stream/extras/$basearch/extras-common/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=1

[extras-common-source]
name=CentOS Stream $releasever - Extras packages - Source
# metalink=https://mirrors.centos.org/metalink?repo=centos-extras-sig-extras-common-source-$stream&arch=source&protocol=https,http
# gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Extras-SHA512
baseurl=https://mirrors.aliyun.com/centos-stream/SIGs/$stream/extras/source/
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

Q.E.D.


一切很好,不缺烦恼。