一键更换centos-stream-9 阿里源
下面整段在 CentOS Stream 9 上以 root 直接粘贴执行即可,已经备份原有源、清缓存、重建索引一条龙。
代码 复制 - 运行
# 1) 备份原有所有 repo 文件
mkdir -p /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/ 2>/dev/null || true
# 2) 写入阿里源(BaseOS / AppStream / CRB / HA / RS,路径均已实测可用)
cat > /etc/yum.repos.d/CentOS-Base.repo <<'EOF'
[baseos]
name=CentOS Stream 9 - BaseOS - Aliyun
baseurl=https://mirrors.aliyun.com/centos-stream/9-stream/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[appstream]
name=CentOS Stream 9 - AppStream - Aliyun
baseurl=https://mirrors.aliyun.com/centos-stream/9-stream/AppStream/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[crb]
name=CentOS Stream 9 - CRB - Aliyun
baseurl=https://mirrors.aliyun.com/centos-stream/9-stream/CRB/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[highavailability]
name=CentOS Stream 9 - HighAvailability - Aliyun
baseurl=https://mirrors.aliyun.com/centos-stream/9-stream/HighAvailability/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[resilientstorage]
name=CentOS Stream 9 - ResilientStorage - Aliyun
baseurl=https://mirrors.aliyun.com/centos-stream/9-stream/ResilientStorage/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
EOF
# 3) 清缓存并重建索引
dnf clean all
dnf makecache
验证是否生效:
dnf repolist
看到 `baseos / appstream / crb / highavailability / resilientstorage` 都是 `enabled`、没有 404 报错就成功了。再实测装个包:
dnf install -y vim
可选:EPEL 9 也换阿里源:
代码 复制 - 运行
cat > /etc/yum.repos.d/epel.repo <<'EOF'
[epel]
name=EPEL 9 - Aliyun
baseurl=https://mirrors.aliyun.com/epel/9/Everything/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-9
EOF
dnf clean all && dnf makecache
万一要回滚官方源:
代码 复制 - 运行
rm -f /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/epel.repo
mv /etc/yum.repos.d/backup/*.repo /etc/yum.repos.d/ 2>/dev/null || true
dnf clean all && dnf makecache
---------- 菠萝云