CentOS 8 源失效修复教程


CentOS 8 源失效修复教程

适用情况:

dnf install 报错
yum install 报错
socat / curl / wget 装不上
remi-release 依赖冲突
metadata 过期
No match for argument
Failed to download metadata
nothing provides redhat-release >= 8.10

CentOS 8 已停止维护,默认源经常失效,需要切换到 CentOS Vault 归档源。


1. 查看系统版本

cat /etc/redhat-release
uname -m

如果显示类似:

CentOS Linux release 8.x.xxxx

可以继续下面的步骤。


2. 备份当前 repo 源

BACKUP_DIR="/root/yumrepo-backup-$(date +%F-%H%M%S)"
mkdir -p "$BACKUP_DIR"
cp -a /etc/yum.repos.d/*.repo "$BACKUP_DIR"/ 2>/dev/null || true
echo "repo backup saved to: $BACKUP_DIR"

3. 禁用旧源和第三方源

这一步会把原来的 .repo 文件移走,避免 remi、epel、旧 CentOS 源继续干扰。

mkdir -p /etc/yum.repos.d/disabled
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/disabled/ 2>/dev/null || true

4. 写入 CentOS 8.5 Vault 源

cat > /etc/yum.repos.d/CentOS-Vault-8.5.2111.repo <<'EOF'
[baseos]
name=CentOS Linux 8.5.2111 - BaseOS
baseurl=http://vault.centos.org/8.5.2111/BaseOS/$basearch/os/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[appstream]
name=CentOS Linux 8.5.2111 - AppStream
baseurl=http://vault.centos.org/8.5.2111/AppStream/$basearch/os/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[extras]
name=CentOS Linux 8.5.2111 - Extras
baseurl=http://vault.centos.org/8.5.2111/extras/$basearch/os/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[powertools]
name=CentOS Linux 8.5.2111 - PowerTools
baseurl=http://vault.centos.org/8.5.2111/PowerTools/$basearch/os/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
EOF

5. 如果官方 Vault 慢,换阿里云 Vault 镜像

可选。如果访问 vault.centos.org 很慢,再执行:

sed -i 's#http://vault.centos.org#https://mirrors.aliyun.com/centos-vault#g' /etc/yum.repos.d/CentOS-Vault-8.5.2111.repo

6. 清理 dnf/yum 缓存

dnf clean all
rm -rf /var/cache/dnf/*
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial 2>/dev/null || true
dnf makecache --refresh

7. 检查源是否正常

dnf repolist

正常应该能看到类似:

baseos
appstream
extras
powertools

8. 测试安装常用依赖

dnf install -y curl wget tar gzip unzip socat ca-certificates lsof net-tools bind-utils

如果能正常安装,说明 CentOS 8 源基本修好了。


9. 如果仍然提示 best candidate / 依赖冲突

可以尝试:

dnf install -y 软件包名 --nobest

例如:

dnf install -y socat --nobest

如果还有旧第三方源干扰,检查:

ls -l /etc/yum.repos.d/
dnf repolist all

确保当前启用的主要是:

baseos
appstream
extras
powertools

CentOS 8 源修复一键脚本

下面这段可以直接复制执行:

cat > /root/fix-centos8-repo.sh <<'EOF'
#!/bin/bash

set -e

echo "=============================="
echo " CentOS 8 Repo Fix Script"
echo "=============================="

if [ ! -f /etc/redhat-release ]; then
    echo "[ERR] /etc/redhat-release not found"
    exit 1
fi

echo "[INFO] System version:"
cat /etc/redhat-release

if ! grep -qi "centos" /etc/redhat-release || ! grep -q "8" /etc/redhat-release; then
    echo "[WARN] This system may not be CentOS 8."
    echo "[WARN] Continue in 5 seconds, press Ctrl+C to cancel."
    sleep 5
fi

BACKUP_DIR="/root/yumrepo-backup-$(date +%F-%H%M%S)"
mkdir -p "$BACKUP_DIR"

echo "[INFO] Backup repo files to: $BACKUP_DIR"
cp -a /etc/yum.repos.d/*.repo "$BACKUP_DIR"/ 2>/dev/null || true

echo "[INFO] Disable old repo files..."
mkdir -p /etc/yum.repos.d/disabled
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/disabled/ 2>/dev/null || true

echo "[INFO] Write CentOS 8.5.2111 Vault repo..."
cat > /etc/yum.repos.d/CentOS-Vault-8.5.2111.repo <<'REPOEOF'
[baseos]
name=CentOS Linux 8.5.2111 - BaseOS
baseurl=http://vault.centos.org/8.5.2111/BaseOS/$basearch/os/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[appstream]
name=CentOS Linux 8.5.2111 - AppStream
baseurl=http://vault.centos.org/8.5.2111/AppStream/$basearch/os/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[extras]
name=CentOS Linux 8.5.2111 - Extras
baseurl=http://vault.centos.org/8.5.2111/extras/$basearch/os/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[powertools]
name=CentOS Linux 8.5.2111 - PowerTools
baseurl=http://vault.centos.org/8.5.2111/PowerTools/$basearch/os/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
REPOEOF

echo "[INFO] Clean dnf cache..."
dnf clean all || true
rm -rf /var/cache/dnf/*

echo "[INFO] Import CentOS GPG key..."
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial 2>/dev/null || true

echo "[INFO] Build dnf cache..."
dnf makecache --refresh

echo "[INFO] Enabled repos:"
dnf repolist

echo "[INFO] Install common packages..."
dnf install -y curl wget tar gzip unzip socat ca-certificates lsof net-tools bind-utils --nobest

echo "=============================="
echo "[OK] CentOS 8 repo fixed."
echo "[OK] Backup saved to: $BACKUP_DIR"
echo "=============================="
EOF

chmod +x /root/fix-centos8-repo.sh
bash /root/fix-centos8-repo.sh

修完后测试:

dnf install -y socat curl wget --nobest
socat -V
curl -V
wget --version

文章作者: 0xdadream
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 0xdadream !
评论
  目录