1、分别在CentOS6和CentOS7分别搭建网络yum源
a、CentOS 7中
首先安装并启用vsftpd服务
[root@node1 ~]# mount /dev/cdrom /mnt/yum/ [root@node1 ~]# vim /etc/yum.repos.d/local.repo [local] name=local baseurl=file:///mnt/yum gpgcheck=0 [root@node1 ~]# yum clean all 此时本地的yum源可以使用 [root@node1 ftp]# yum -y install vsftpd ##安装 vsftpd服务包 [root@node1 ftp]# systemctl start vsftpd ##启动vsftpd服务 [root@node1 ftp]# systemctl enable vsftpd ##开机启动vsftpd服务 [root@node1 ftp]# iptables -F ##清空当前防火墙规则 [root@node1 ftp]# systemctl disable firewalld.service ##禁止防火墙开机启动 [root@node1 ftp]# systemctl stop firewalld ##停止防火墙 [root@node1 ~]# cd /var/ftp/pub/ && mkdir app ##创建一个app目录用于存放rpm [root@node1 app]# yum -y install createrepo ##安装createrepo命令程序 cp /mnt/yum/Packages/screen-4.1.0-0.21.20120314git3c2946.el7.x86_64.rpm /var/ftp/pub/app/ ##复制一个rpm 到ftp服务器rpm包的存放目录中 [root@node1 app]# createrepo /var/ftp/pub/app/ ##创建rpm元数据存储库 Spawning worker 0 with 2 pkgs Workers Finished Saving Primary metadata Saving file lists metadata Saving other metadata Generating sqlite DBs Sqlite DBs complete
此时访问你的ftp址时就可以看到以下结果了
在另一台机器中建立一个刚刚搭建ftp网络的repo配置文件
[root@test~]#cd /etc/yum.repos.d/ [root@testyum.repos.d]#ls ren.repo [root@testyum.repos.d]#vim ren.repo [myrepo] name=myrepo baseurl=ftp://192.168.99.36/pub/app gpgcheck=0 [root@testyum.repos.d]#yum clean all [root@testyum.repos.d]#yum repolist Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile repo id repo name status myrepo myrepo 2 repolist: 2 [root@testyum.repos.d]#yum list all | grep myrepo screen.x86_64 4.1.0-0.21.20120314git3c2946.el7 myrepo tree.x86_64 1.6.0-10.el7 myrepo [root@testyum.repos.d]#yum -y install screen tree [root@testyum.repos.d]#screen 此时就可以用yum安装使用这两个所测试的rpm包了
b、CentOS6中
同样,首先安装并启用vsftpd服务
[root@rj ~]# mount /dev/sr0 /mnt/yum/ mount: block device /dev/sr0 is write-protected, mounting read-only [root@rj ~]# vim /etc/yum.repos.d/jin.repo ##先配置好一个本地的yum源,用来安装vsftpd [base] name=base baseurl=file:///mnt/yum gpgcheck=0 [root@rj ~]# yum -y install vsftpd ##安装vsftpd服务 [root@rj ~]# service vsftpd start ##启动vsftpd服务 Starting vsftpd for vsftpd: [ OK ] [root@rj ~]# chkconfig vsftpd on ##添加vsftpd至开机自启动 [root@rj ~]# chkconfig --list | grep vsftpd ##查看vsftpd服务在哪个级别开机自启 vsftpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@rj ~]# iptables -F ##清空防火墙规则 [root@rj ~]# chkconfig iptables off ##不将防火墙开机自启 [root@rj ~]# chkconfig --list | grep iptables ##查看防火墙是否关闭自启 iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@rj ~]# cd /var/ftp/pub/ && mkdir app ##ftp服务器的目录并在其中创建一个文件 [root@rj pub]# yum -y install createrepo ##安装createrepo工具 [root@rj pub]# cp /mnt/yum/Packages/screen-4.0.3-18.el6.x86_64.rpm /var/ftp/pub/app/ [root@rj pub]# cp /mnt/yum/Packages/tree-1.5.3-3.el6.x86_64.rpm /var/ftp/pub/app/ ##复制两个测试的rpm包到创建的目录中 [root@rj pub]# createrepo /var/ftp/pub/app/ ##创建rpm元数据存储库 Spawning worker 0 with 2 pkgs Workers Finished Gathering worker results Saving Primary metadata Saving file lists metadata Saving other metadata Generating sqlite DBs Sqlite DBs complete
此时访问你CentOS6的ftp址时就可以看到以下结果了
在CentOS6的环境中,测试自己搭的ftp网络rpm源能否使用
[root@rj pub]# vim /etc/yum.repos.d/create.repo [create] name=create baseurl=ftp://192.168.99.55/pub/app gpgcheck=0 [root@rj pub]# yum clean all [root@rj yum.repos.d]# yum list | grep "create" createrepo.noarch 0.9.9-22.el6 @base screen.x86_64 4.0.3-18.el6 create tree.x86_64 1.5.3-3.el6 create [root@rj yum.repos.d]# yum -y install tree screen ##测试效果 [root@rj ~]# tree -L 1 / [root@rj ~]# screen
2、用一个bash脚本来实现以上功能
[root@rj ~]# vim createrepo2.sh #!/bin/bash #Author:jin.ren #Date 20170419 #Version 3.0 #Can create ftp repo read -p "Please enter your operating system environment 6|7 : " OS read -p "Please input your $OS IP : " IP if echo $IP | grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" &> /dev/null ; then if [ $OS == "7" ] ;then [ -d /etc/yum.repos.d/bak ] || mkdir /etc/yum.repos.d/bak mv /etc/yum.repos.d/*repo /etc/yum.repos.d/bak &> /dev/null [ -d /mnt/yum ] || mkdir /mnt/yum if mount -r /dev/cdrom /mnt/yum &> /dev/null ; then cat <<EOF > /etc/yum.repos.d/local.repo [local] name=local baseurl=file:///mnt/yum gpgcheck=0 enabled=1 EOF yum clean all &> /dev/null sleep 10 yum -y install vsftpd &> /dev/null sleep 10 systemctl start vsftpd &> /dev/null systemctl enable vsftpd &> /dev/null iptables -F systemctl disable firewalld &> /dev/null systemctl stop firewalld &> /dev/null mkdir /var/ftp/pub/app yum -y install createrepo &> /dev/null cp /mnt/yum/Packages/screen* /var/ftp/pub/app cp /mnt/yum/Packages/tree* /var/ftp/pub/app createrepo /var/ftp/pub/app &> /dev/null mv /etc/yum.repos.d/*repo /etc/yum.repos.d/bak &> /dev/null cat <<EOF >> /etc/yum.repos.d/ren.repo [myrepo] name=myrepo baseurl=ftp://$IP/pub/app gpgcheck=0 EOF yum clean all & > /dev/null sleep 10 yum list | grep myrepo else echo "Please insert cdrom" exit 2 fi elif [ $OS == "6" ];then [ -d /etc/yum.repos.d/bak ] || mkdir /etc/yum.repos.d/bak mv /etc/yum.repos.d/*repo /etc/yum.repos.d/bak &> /dev/null [ -d /mnt/yum ] || mkdir /mnt/yum if mount -r /dev/cdrom /mnt/yum &> /dev/null ; then cat <<EOF > /etc/yum.repos.d/local.repo [local] name=local baseurl=file:///mnt/yum gpgcheck=0 enabled=1 EOF yum clean all &> /dev/null sleep 3 yum -y install vsftpd &> /dev/null sleep 3 chkconfig vsftpd on &> /dev/null service vsftpd start &> /dev/null iptables -F chkconfig iptables off &> /dev/null service iptables off &> /dev/null mkdir /var/ftp/pub/app yum -y install createrepo &> /dev/null cp /mnt/yum/Packages/screen* /var/ftp/pub/app cp /mnt/yum/Packages/tree* /var/ftp/pub/app createrepo /var/ftp/pub/app &> /dev/null mv /etc/yum.repos.d/*repo /etc/yum.repos.d/bak &> /dev/null cat <<EOF >> /etc/yum.repos.d/ren.repo [myrepo] name=myrepo baseurl=ftp://$IP/pub/app gpgcheck=0 EOF yum clean all & > /dev/null sleep 10 yum list | grep myrepo else echo "Please insert cdrom" exit 2 fi else exit 6 fi else echo "Please output the correct format " exit 1 fi
原创文章,作者:renjin,如若转载,请注明出处:http://www.178linux.com/79322