YUM篇--centos7搭建本地机器yum源、局域网yum源服务器、网络yum源案例步骤
一、centos7.8系统本地搭建yum源
1.1 环境描述
vmware 中安装centos7.8的系统,名为mini-local。最小化安装;网络选择net模式;
把iso镜像挂载到本地机器的目录上,yum源指向iso里面的镜像文件。
1.2 模拟内网环境
主机名:mini-local ip地址:192.168.10.201 未配置网关地址:192.168.10.254 可以ping通网关,无法ping通外网,模拟内网环境;
1.3搭建本地内网yum源
[root@mini-local ~]# yum install -y vim #未搭建时,yum去安装其他命令会报错
1.3.1 把centos7.8的iso镜像,上传至/opt/centos78/目录下
[root@mini-local ~]# mkdir -p /opt/centos78/ #新建/opt/centos78/目录
把iso镜像,上传到/opt/centos78/目录下。
1.3.2 在本地创建一个挂载目录/mnt/centos78,把/opt/centos78/下的iso镜像,挂载到/mnt/centos78/ 目录上
[root@mini-local centos78]# mkdir -p /mnt/centos78/ #创建/mnt/centos78/目录
[root@mini-local centos78]# mount -o loop /opt/centos78/CentOS-7-x86_64-DVD-2003.iso /mnt/centos78 #只读挂载,但是reboot服务器后,挂载会消失,需要写入开机自动挂载
在rc.local中加入开启自启动
chmod +x /etc/rc.d/rc.local
vi /etc/rc.d/rc.local
mount -o loop /opt/centos78/CentOS-7-x86_64-DVD-2003.iso /mnt/centos78 #把这一行加入到最后
1.3.3 修改本地的yum配置文件
[root@mini-local centos78]# cd /etc/yum.repos.d
[root@mini-local yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak
[root@mini-local yum.repos.d]# vi CentOS-Media.repo #修改配置文件
点击查看CentOS-Media.repo代码
[c7-media]
Name=CentOS-$releasever - Media
baseurl=file:///mnt/centos78/
epgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[root@mini-local yum.repos.d]# yum clean all #清除
[root@mini-local yum.repos.d]# yum makecache #创建缓存
1.3.4 测试,再次yum一下
[root@mini-local yum.repos.d]# yum install -y vim #此时安装可以进行下去
本地机器上,以iso镜像文件,yum源配置成功
二、本地局域网搭建内网yum源服务器案例
2.1环境准备
2台centos7.8系统。yum-server和yum-client
yum-server:ip:192.168.10.202
yum-client:ip:192.168.10.203
两台机器都没有配置网关,即模拟局域网环境。
2.2 在yum-server机器上搭建局域网的yum源(2个,7.8和6.9)
2.2.1 在yum-server机器上,创建/opt/centos-iso目录,用来存放iso镜像
[root@yum-server ~]# mkdir -p /opt/centos-iso/
[root@yum-server ~]# mv CentOS-6.9-x86_64-bin-DVD1.iso /opt/centos-iso/ #6.9的镜像
[root@yum-server centos78]# mv CentOS-7-x86_64-DVD-2003.iso /opt/centos-iso/ #7.8的镜像
[root@yum-server ~]# cd /opt/centos-iso/ && ll #镜像都在这里面
2.2.2 在yum-server机器上,创建相应的挂载目录,把iso挂载上去
[root@yum-server centos-iso]# mkdir -p /mnt/centos78/ #创建7.8镜像的挂载目录
[root@yum-server centos-iso]# mkdir -p /mnt/centos69/ #创建6.9镜像的挂载目录
[root@yum-server centos-iso]# mount -o loop /opt/centos-iso/CentOS-7-x86_64-DVD-2003.iso /mnt/centos78/ #把镜像挂载到目录上
[root@yum-server centos-iso]# mount -o loop /opt/centos-iso/CentOS-6.9-x86_64-bin-DVD1.iso /mnt/centos69/
但是上面挂载,reboot服务器后,挂载会消失,需要写入开机自动挂载
[root@yum-server ~]# chmod +x /etc/rc.d/rc.local #加执行权限
[root@yum-server ~]# vi /etc/rc.d/rc.local #修改配置文件
点击查看rc.loal文件,加入下面两行代码
mount -o loop /opt/centos-iso/CentOS-7-x86_64-DVD-2003.iso /mnt/centos78/
mount -o loop /opt/centos-iso/CentOS-6.9-x86_64-bin-DVD1.iso /mnt/centos69/
2.2.3 备份yum-server机器上的原repo文件,新建repo文件
[root@yum-server ~]# cd /etc/yum.repos.d/
[root@yum-server yum.repos.d]# mkdir -p /etc/yum.repos.d/bak/ #创建bak目录
[root@yum-server yum.repos.d]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/ #把所有的以repo结尾的文件,移动到bak目录下
[root@yum-server yum.repos.d]# vi base.repo #新建文件,并写入如下内容
点击查看代码
[base]
name=centos78
baseurl=file:///mnt/centos78/
enabled=1
gpgcheck=0
gpgkey=file:///mnt/centos78/rpm-gpg/RPM-GPG-KEY-CentOS-7
[root@yum-server yum.repos.d]# yum clean all
[root@yum-server yum.repos.d]# yum makecache
[root@yum-server yum.repos.d]# yum install -y vim #此时测试,yum安装成功
2.2.4 在yum-server中,安装nginx服务,浏览器验证
[root@yum-server yum.repos.d]# yum install -y httpd
[root@yum-server yum.repos.d]# systemctl start httpd #启动httpd服务
[root@yum-server html]# systemctl stop firewalld && systemctl disable firewalld #注意本机的防火墙关闭
[root@yum-server html]# cd /var/www/html/
[root@yum-server html]# vim index.html #简单写个测试页面
[root@yum-server html]# systemctl restart httpd #重启服务
浏览器输入本机ip:http://192.168.10.202 成功;
2.2.5创建软连接,把centos78的yum源开放出去,浏览器就可以访问
[root@yum-server html]# ln -s /mnt/centos78/ /var/www/html/centos78
[root@yum-server html]# ln -s /mnt/centos69/ /var/www/html/centos69
然后浏览器访问:http://192.168.10.202/centos78/ 成功
2.3 在yum-client机器上修改repo源,获取内网中yum-server提供的两个源(centos78、centos69)
[root@yum-client ~]# systemctl stop firewalld && systemctl disable firewalld #先关闭防火墙
[root@yum-client ~]# cd /etc/yum.repos.d/
[root@yum-client yum.repos.d]# mkdir -p /etc/yum.repos.d/bak/
[root@yum-client yum.repos.d]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
[root@yum-client yum.repos.d]# vi base.repo
点击查看代码
[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
baseurl=http://192.168.10.202/centos78
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
baseurl=http://192.168.10.202/centos78
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
baseurl=http://192.168.10.202/centos78
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
baseurl=http://192.168.10.202/centos78
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[root@yum-client yum.repos.d]# yum clean all
[root@yum-client yum.repos.d]# yum makecache
[root@yum-client yum.repos.d]# yum install -y vim #此时可以yum成功
三、外网下,把centos7的本地源换成其他源(阿里的yum源)
3.1 检查环境并配置
把上一步骤的yum-client机器配置的repo源删除。原来的repo移动到之前的位置。配置上网关,DNS。可以ping通外网即可。
环境:centos7.9
[root@yum-client yum.repos.d]# yum install -y wget #先用本地源,下载wget
[root@yum-client ~]# cd /etc/yum.repos.d
[root@yum-client yum.repos.d]# mv ./CentOS-Base.repo ./CentOS-Base.repo.bak
[root@yum-client yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo #获取阿里的centos-7的repo源
[root@yum-client yum.repos.d]# wget http://mirrors.aliyun.com/repo/epel-7.repo
[root@yum-client yum.repos.d]# yum clean all
[root@yum-client yum.repos.d]# yum makecache