linux更改yum源
[TOC]
Centos7更改yum源为阿里云yum源
- 1.首先确认是否有配置dns服务器,若没有建议配置阿里云的公共DNS地址
cat /etc/resolv.conf
echo "nameserver 223.5.5.5" >> /etc/resolv.conf
echo "nameserver 223.6.6.6" >> /etc/resolv.conf
- 2.确认yum源(可拷贝)
cat /etc/yum.repos.d/CentOS-Base.repo
- 3.进入到/etc/yum.repos.d/目录下,备份之前的CentOS-Base.repo地址。
cd /etc/yum.repos.d/
mv CentOS-Base.repo CentOS-Base.repo.$(date +%Y%m%d%H%M%S)
- 4.下载阿里云yum源
#.centot5:curl -sL http://mirrors.aliyun.com/repo/Centos-5.repo -o /etc/yum.repos.d/CentOS-Base.repo
#.centos6:curl -sL http://mirrors.aliyun.com/repo/Centos-6.repo -o /etc/yum.repos.d/CentOS-Base.repo
#.centot7:curl -sL http://mirrors.aliyun.com/repo/Centos-7.repo -o /etc/yum.repos.d/CentOS-Base.repo
curl -sL http://mirrors.aliyun.com/repo/Centos-7.repo -o /etc/yum.repos.d/CentOS-Base.repo
- 5.将服务器上的软件包信息缓存到本地,以提高搜索安装软件的速度
yum makecache
附:如果执行yum makecache
报错:Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
,则检查 /etc/yum.repos.d/
下是否有 epel.repo
文件,如果有,参考下面的命令将其重命名为 epel.repo_bak
,千万不能以 .repo
格式备份,然后再执行一次上面的命令即可!
# mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo_$(date +%Y%m%d%H%M%S)
Centos7更改yum源为华为云yum源
故障场景
- 2020.12.30.一台centos7的阿里云ecs克隆到华为云之后,执行yum下载的时候报错:
Loaded plugins: fastestmirror
http://mirrors.cloud.aliyuncs.com/centos/7/os/x86_64/repodata/repomd.xml: [Errno 14] curl#6 - "Could not resolve host: mirrors.cloud.aliyuncs.com; Unknown error"
Trying other mirror.
Error downloading packages:
wget-1.14-10.el7_0.1.x86_64: [Errno 256] No more mirrors to try.
解决方案
- 1.首先确认是否有配置dns服务器,若没有建议配置华为云的内网DNS地址
cat /etc/resolv.conf
echo "nameserver 100.125.1.250" >> /etc/resolv.conf
echo "nameserver 100.125.17.29" >> /etc/resolv.conf
- 2.执行下面三条命令更改yum源并清除yum缓存
mv /etc/yum.repos.d/* /tmp/
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.myhuaweicloud.com/repo/CentOS-Base-7.repo
yum clean all && yum repolist
Centos7使用iso镜像做本地yum源
- 1.检查是否有本地yum源
yum list | grep ntp
- 2.创建iso存放目录和挂载目录
mkdir -p /mnt/{iso,cdrom}
- 3.上传iso镜像文件到iso存放目录/mnt/iso下
cd /mnt/iso
wget -c http://iso.sqlfans.cn/os/CentOS-7.9-x86_64-DVD-2009.iso
- 4.挂载iso镜像到挂载目录/mnt/cdrom下
mount -o loop /mnt/iso/CentOS-7.9-x86_64-DVD-2009.iso /mnt/cdrom
df -Th
- 5.创建repo文件并放到/etc/yum.repos.d/目录
cp /etc/yum.repos.d/local.repo /etc/yum.repos.d/local.repo.$(date +%Y%m%d%H%M%S)
cat >/etc/yum.repos.d/local.repo<<EOF
[local]
name=local
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-CentOS-7
EOF
- 6.禁用默认的yum 网络源,否则会先在网络源中寻找适合的包
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.$(date +%Y%m%d%H%M%S)
- 7.测试yum安装
yum clean all
yum makecache
yum list | grep ntp
yum repolist
yum install -y ntp
附:2022.05.30.执行 yum makecache
报错:14: curl#6 - "Could not resolve host: mirrors.elrepo.org; 未知的错误"
mv /etc/yum.repos.d/elrepo.repo /etc/yum.repos.d/elrepo.repo.$(date +%Y%m%d%H%M%S)
yum clean all
yum makecache
附:2022.05.31.执行 yum makecache
报错:file:///mnt/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mnt/repodata/repomd.xml"
mv /etc/yum.repos.d/mnt.repo /etc/yum.repos.d/mnt.repo.$(date +%Y%m%d%H%M%S)
yum clean all
yum makecache
Centos6使用iso镜像做本地yum源
- 1.检查是否有本地yum源
yum list | grep ntp
- 2.创建iso存放目录和挂载目录
mkdir -p /mnt/{iso,cdrom}
- 3.上传iso镜像文件到iso存放目录/mnt/iso下
cd /mnt/iso
wget -c http://iso.sqlfans.cn/os/CentOS-6.7-x86_64-bin-DVD.iso
- 4.挂载iso镜像到挂载目录/mnt/cdrom下
mount -o loop /mnt/iso/CentOS-6.7-x86_64-bin-DVD.iso /mnt/cdrom
df -Th
- 5.创建repo文件并放到/etc/yum.repos.d/目录
cp /etc/yum.repos.d/local.repo /etc/yum.repos.d/local.repo.$(date +%Y%m%d%H%M%S)
cat >/etc/yum.repos.d/local.repo<<EOF
[local]
name=local
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-CentOS-6
EOF
- 6.禁用默认的yum 网络源,否则会先在网络源中寻找适合的包
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.$(date +%Y%m%d%H%M%S)
- 7.测试yum安装
yum clean all
yum makecache
yum list | grep ntp
yum repolist
yum install -y ntp
附:取消iso镜像作为本地yum源
umount /mnt/cdrom
rm -f /etc/yum.repos.d/local.repo
Kylin v10使用iso镜像做本地yum源
- 1.检查是否有本地yum源
yum list | grep ntp
- 2.创建iso存放目录和挂载目录
mkdir -p /mnt/{iso,cdrom}
- 3.上传iso镜像文件到iso存放目录/mnt/iso下
cd /mnt/iso
wget -c http://iso.sqlfans.cn/os/Kylin-Server-10-SP2-x86-Release-Build09-20210524.iso
- 4.挂载iso镜像到挂载目录/mnt/cdrom下
mount -o loop /mnt/iso/Kylin-Server-10-SP2-x86-Release-Build09-20210524.iso /mnt/cdrom
df -Th
- 5.创建repo文件并放到/etc/yum.repos.d/目录
mv /etc/yum.repos.d/kylin_x86_64.repo /etc/yum.repos.d/kylin_x86_64.repo.$(date +%Y%m%d%H%M%S)
cat >/etc/yum.repos.d/kylin_x86_64.repo<<EOF
[ks10-adv-os]
name=Kylin Linux Advanced Server 10 - os
baseurl=file:///mnt/cdrom
gpgcheck=0
enabled=1
EOF
- 6.测试yum安装
yum clean all
yum makecache
yum list | grep ntp
yum repolist
yum install -y ntp
redhat 8.2更改yum源为阿里云yum源
cat /etc/resolv.conf | grep nameserver | grep "223.5.5.5" || echo "nameserver 223.5.5.5" >> /etc/resolv.conf
mv /etc/yum.repos.d/redhat.repo /etc/yum.repos.d/redhat.repo.$(date +%Y%m%d%H%M%S)
curl -sL http://mirrors.aliyun.com/repo/Centos-8.repo -o /etc/yum.repos.d/CentOS-Base.repo
yum makecache