一、环境准备

你需要准备一台干净的CentOS7的环境,且可以ping的通外网~尤其是mirrors.aliyun.com。
简单测试一下,如果通的话,再执行以下步骤完成设置。

ping mirrors.aliyun.com

二、手动配置阿里云源

  1. 备份官方的原yum源的配置
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
  1. 下载Centos-7.repo文件
    最小化安装,如果没有wget的命令,可使用curl代替wget,执行如下语句代替:
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  1. 清除及生成缓存。
# 清除yum缓存
yum clean all
# 缓存阿里云源
yum makecache
# 测试阿里云源 
yum list
# 升级
yum update

三、脚本配置本地源

  1. 找个目录,创建脚本文件:
    本人以/root/目录为例
vi /root/auto_aliyun.sh
  1. 编写如下内容:
#!/bin/bash
# 备份官方的原yum源的配置
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# 下载Centos-7.repo文件,或者curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo,取决于你是否有wget的命令
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 清除yum缓存
yum clean all
# 缓存本地yum源
yum makecache
  1. 按ESC。输入:wq保存,完成镜像阿里云源的脚本文件的编写
  2. 赋予脚本可执行的权限
chmod +x /root/auto_aliyun.sh
  1. 执行脚本文件,即可
/root/auto_aliyun.sh