YUM主要用于自动安装、升级rpm软件包,它能自动查找并解决rpm包之间的依赖关系。要成功的使用YUM工具安装更新软件或系统,就需要有一个包含各种rpm软件包的repository(软件仓库),这个软件仓库我们习惯称为yum源。网络上有大量的yum源,但由于受到网络环境的限制,导致软件安装耗时过长甚至失败。特别是当有大量服务器大量软件包需要安装时,缓慢的进度条令人难以忍受。因此我们在优化系统时,都会更换国内的源。
相比较而言,本地YUM源服务器最大优点是局域网的快速网络连接和稳定性。有了局域网中的YUM源服务器,即便在Internet连接中断的情况下,也不会影响其他YUM客户端的软件安装和升级。
1、尽管有很多的免费镜像提供yum源服务,但是还是有必要建立自己的yum服务器,主要出于以下几点考虑
网络速度:访问互联网可能比较慢
节省带宽:如果有大量的服务器,架设自己的yum源可以有效节省互联网带宽
联网限制:对于有些内网服务器,不能连接到互联网
对于RHEL(Redhat Enterprise Linux),需要购买服务
便于发布自己开发的rpm包
2、找到适合你的站点的yum同步源(需支持rsync),主要是CentOS标准软件仓库、EPEL
常见的提供rsync的中国数据源:
mirrors.hust.edu.cn mirrors.ustc.edu.cn mirrors.yun-idc.com mirror.bjtu.edu.cn mirrors.tuna.tsinghua.edu.cn(推荐)
基于速度、稳定性以及源更新频率综合考虑,我们选择清华提供的源。
为自己的rpm包制作yum仓库
此机器的IP地址为:192.168.175.11
createrepo命令说明
createrepo [options] <directory>
下面是整个过程,所有的文件都放在/data/yum_file目录里面,fastdfs为一个图片管理系统,以这几个rpm包作为示例,来演示仓库的制作,假如在/data/yum_file/fastdfs/Packeges目录下面有三个rpm包,现在要为其创建repodata文件
[root@localhost ~]#yi createrepo #安装命令 [root@localhost ~]#mkdir -p /data/yum_file [root@localhost ~]#cp -r fastdfs /data/yum_file [root@localhost ~]#cd /data/yum_file/ [root@localhost /data/yum_file]#ls fastdfs [root@localhost /data/yum_file]#tree . . └── fastdfs └── Packeges ├── fastdfs-5.0.9-1.el7.centos.x86_64.rpm ├── fastdfs-server-5.0.9-1.el7.centos.x86_64.rpm └── fastdfs-tool-5.0.9-1.el7.centos.x86_64.rpm 2 directories, 3 files [root@localhost /data/yum_file]#cd fastdfs/ [root@localhost /data/yum_file/fastdfs]#ls Packeges [root@localhost /data/yum_file/fastdfs]#createrepo . #就在这个目录下面创建repodata目录, #也可以在Packeges目录下面执行这个命令,但是这样的话repodata目录和rpm包放在一起比较乱 Spawning worker 0 with 3 pkgs Workers Finished Gathering worker results Saving Primary metadata Saving file lists metadata Saving other metadata Generating sqlite DBs Sqlite DBs complete [root@localhost /data/yum_file/fastdfs]#ls Packeges repodata [root@localhost /data/yum_file/fastdfs]#cd repodata/ [root@localhost /data/yum_file/fastdfs/repodata]#ls 3c96494fcfa1afe4327e4de1b611966679f654aeb3b2c718cc2e3dd20babded5-other.xml.gz 3e1bc707e83178d6ffef29cd5f9d91e4827e470af8d09dc38a7e6d8a2f04c9da-primary.sqlite.bz2 4c6f103258bf23273d0b3e063c80559aa7fafddfa7d2246026e42c7bd86ae30e-filelists.sqlite.bz2 cebb9ffcbe8e2bb0de5ca9930efc156d3ba105ea2ef143bb249cfec8173b7cb1-other.sqlite.bz2 d6ff943cfa2db7ee44eb6472a2bc402c3d88571a129ba73180cb547ab3882287-filelists.xml.gz e7b843b1a771e3a600ff694f8559adf90ef709c4c5dba4e077f286a0a02ce16e-primary.xml.gz repomd.xml
然后用nginx搭建一个web服务器,将配置文件里面的root指到这个目录就可以
[root@localhost ~]#yi nginx [root@localhost /etc/nginx/conf.d]#vim default.conf server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /data/yum_file/; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { autoindex on; #设置可以访问目录下的所有文件 } [root@localhost ~]#service nginx restart Stopping nginx: [FAILED] Starting nginx: [ OK ]
然后就可以通过浏览器访问了,也可以将这个url添加到yum源的配置里面,就可以使用yum命令安装里面的rpm包了
机器:192.168.175.12进行安装测试
[root@localhost ~]#cat /etc/yum.repos.d/CentOS-Base.repo [base] name=CentOS-$releasever - Base #baseurl=http://mirrors.aliyun.com/centos/6.8/os/x86_64/ baseurl=http://192.168.175.10/base/centos6.8 enabled=1 gpgcheck=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 [epel] name=fedora-epel #baseurl=http://mirrors.aliyun.com/epel/6/x86_64/ baseurl=http://192.168.175.10/epel/centos6.8 enabled=1 gpgcheck=0 [fastdfs] name=fastdfs #baseurl=http://mirrors.aliyun.com/epel/6/x86_64/ baseurl=http://192.168.175.11/fastdfs #这里的仓库位置指向要指向到在这个目录下面能找到repodata目录为准 enabled=1 gpgcheck=0
执行repolist的时候可以正常显示rpm包数量3
[root@localhost ~]#yum repolist Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile repo id repo name status base CentOS-6 - Base 6,696 epel fedora-epel 12,199 fastdfs fastdfs 3 repolist: 18,898
添加新的rpm包
当有新的rpm包想要添加到这个yum仓库的时候,假如说:pcre-devel openssl-devel这两个包是新制作的rpm包,添加到Packages目录里面,怎么让repodata也更新
[root@localhost /data/yum_file/fastdfs/Packeges]#yumdownloader pcre-devel openssl-devel Loaded plugins: fastestmirror, refresh-packagekit Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.aliyun.com pcre-devel-7.8-7.el6.x86_64.rpm | 320 kB 00:00 pcre-devel-7.8-7.el6.i686.rpm | 320 kB 00:00 openssl-devel-1.0.1e-57.el6.x86_64.rpm | 1.2 MB 00:01 openssl-devel-1.0.1e-57.el6.i686.rpm | 1.2 MB 00:01 [root@localhost /data/yum_file/fastdfs/Packeges]#clear [root@localhost /data/yum_file/fastdfs/Packeges]#ls fastdfs-5.0.9-1.el7.centos.x86_64.rpm openssl-devel-1.0.1e-57.el6.x86_64.rpm fastdfs-server-5.0.9-1.el7.centos.x86_64.rpm pcre-devel-7.8-7.el6.i686.rpm fastdfs-tool-5.0.9-1.el7.centos.x86_64.rpm pcre-devel-7.8-7.el6.x86_64.rpm openssl-devel-1.0.1e-57.el6.i686.rpm
在上一级目录执行
[root@localhost /data/yum_file/fastdfs]#ls Packeges repodata [root@localhost /data/yum_file/fastdfs]#createrepo --update . #更新后,会在yum仓库里面看到7个rpm包,成功更新 Spawning worker 0 with 4 pkgs Workers Finished Gathering worker results Saving Primary metadata Saving file lists metadata Saving other metadata Generating sqlite DBs Sqlite DBs complete
机器:192.168.175.12机器测试
[root@localhost ~]#yum clean all [root@localhost ~]#yum repolist Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile repo id repo name status base CentOS-6 - Base 6,696 epel fedora-epel 12,199 fastdfs fastdfs 7 #有7个包 repolist: 18,902
命令介绍,在yum源上下载rpm包,会下载到当前的目录
yumdownloader pcre-devel openssl-devel
yum镜像同步脚本文件
因为设计:/data/samba/public目录可以通过http服务来进行访问:http://192.168.111.33/
然后:/data/samba目录可以通过samba服务来进行访问,即samba访问的范围要大,public目录是公共目录,可以通过samba服务和http服务访问到
具体samba共享目录查看samba章节
–
同步脚本如下面,写的比较简陋,将日志记录在log目录中,执行之前将对应的目录建立好
要确保对方的网站有rsyncd进程在监听,不能通过httpd协议来复制,具体的命令讲解见:rsync命令,可以将脚本放置在定时任务中,每周定时进行同步
国内的镜像站点得rsync服务都是经过修改的,用普通的方式同步镜像,容易出错,找了下面的方法,必须要进入当前目录里面
此库包含了:
centos6 base仓库,updates仓库,extras仓库,epel仓库
centos7 base仓库,updates仓库,extras仓库,epel仓库
docker-ce 仓库
[root@master /data/script]#cat rsync_repo.sh #!/bin/bash base_dir="/data/samba/public" rsync_repo_log="/var/log/rsync_repo.log" mkdir -p ${base_dir} fun_rsync(){ echo "$(date +%F_%T) start sync" mkdir -p ${base_dir}/centos/6/os/x86_64/Packages/ cd ${base_dir}/centos/6/os/x86_64/Packages/ echo "###################centos/6/os/x86_64/Packages/" rsync -avrt --delete rsync://mirrors.tuna.tsinghua.edu.cn/centos/6/os/x86_64/Packages/ . mkdir -p ${base_dir}/centos/6/os/x86_64/repodata/ cd ${base_dir}/centos/6/os/x86_64/repodata/ echo "###################centos/6/os/x86_64/repodata/" rsync -avrt --delete rsync://mirrors.tuna.tsinghua.edu.cn/centos/6/os/x86_64/repodata/ . mkdir -p ${base_dir}/centos/6/updates/x86_64/ cd ${base_dir}/centos/6/updates/x86_64/ echo "###################centos/6/updates/x86_64/" rsync -avrt --delete rsync://mirrors.tuna.tsinghua.edu.cn/centos/6/updates/x86_64/ . mkdir -p ${base_dir}/centos/6/extras/x86_64/ cd ${base_dir}/centos/6/extras/x86_64/ echo "###################centos/6/extras/x86_64/" rsync -avrt --delete rsync://mirrors.tuna.tsinghua.edu.cn/centos/6/extras/x86_64/ . mkdir -p ${base_dir}/epel/6/x86_64/ cd ${base_dir}/epel/6/x86_64/ echo "###################epel/6/x86_64/" rsync -avrt --delete --exclude=/debug/ rsync://mirrors.tuna.tsinghua.edu.cn/epel/6/x86_64/ . mkdir -p ${base_dir}/centos/7/os/x86_64/Packages/ cd ${base_dir}/centos/7/os/x86_64/Packages/ echo "###################centos/7/os/x86_64/Packages/" rsync -avrt --delete rsync://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/Packages/ . mkdir -p ${base_dir}/centos/7/os/x86_64/repodata/ cd ${base_dir}/centos/7/os/x86_64/repodata/ echo "###################centos/7/os/x86_64/repodata/" rsync -avrt --delete rsync://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/repodata/ . mkdir -p ${base_dir}/centos/7/updates/x86_64/ cd ${base_dir}/centos/7/updates/x86_64/ echo "###################centos/7/updates/x86_64/" rsync -avrt --delete rsync://mirrors.tuna.tsinghua.edu.cn/centos/7/updates/x86_64/ . mkdir -p ${base_dir}/centos/7/extras/x86_64/ cd ${base_dir}/centos/7/extras/x86_64/ echo "###################centos/7/extras/x86_64/" rsync -avrt --delete rsync://mirrors.tuna.tsinghua.edu.cn/centos/7/extras/x86_64/ . mkdir -p ${base_dir}/epel/7/x86_64/ cd ${base_dir}/epel/7/x86_64/ echo "###################epel/7/x86_64/" rsync -avrt --delete --exclude=/debug/ rsync://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/ . mkdir -p ${base_dir}/docker-ce/linux/centos/ cd ${base_dir}/docker-ce/linux/centos/ echo "###################docker-ce/linux/centos/" rsync -avrt --delete rsync://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/ . mkdir -p ${base_dir}/mysql/yum/ cd ${base_dir}/mysql/yum/ echo "###################mysql/yum/" rsync -avrt --delete rsync://mirrors.tuna.tsinghua.edu.cn/mysql/yum/ . echo "$(date +%F_%T) stop sync" echo "=========================" } fun_rsync &>> ${rsync_repo_log}
添加到定时任务中
[root@localhost ~]# vim /etc/crontab 0 0 * * 1 root /bin/bash /data/script/rsync_repo.sh
上面的源同步完成后,将key也添加到目录中供下载
[root@localhost ~]# wget https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 -P /data/samba/public/centos [root@localhost ~]# wget https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6 -P /data/samba/public/centos [root@localhost ~]# wget https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-6 -P /data/samba/public/epel [root@localhost ~]# wget https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-7 -P /data/samba/public/epel
目录结构
[root@master /data/samba/public]#ll total 32 drwxr-xr-x 4 root root 4096 Aug 2 23:28 centos -rwxr--r-- 1 root root 971 Jun 20 17:05 CentOS-6-192.168.111.33.repo -rwxr--r-- 1 root root 971 Jun 20 17:06 CentOS-7-192.168.111.33.repo drwxr-xr-x 3 root root 4096 Aug 25 17:25 docker-ce -rw-r--r-- 1 root root 383 Sep 11 15:00 docker-ce-192.168.111.33.repo drwxr-xr-x 4 root root 4096 Aug 3 00:51 epel drwxr-xr-x 10 root root 4096 Aug 3 12:03 isos drwxr-xr-x 2 root root 4096 Aug 20 14:59 software
下面是centos6的repo文件的具体内容
[root@master /data/samba/public]#cat CentOS-6-192.168.111.33.repo [base] name=CentOS-$releasever-$basearch-Base-192.168.111.33 baseurl=http://192.168.111.33/centos/$releasever/os/$basearch/ #baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/ enabled=1 gpgcheck=1 gpgkey=http://192.168.111.33/centos/RPM-GPG-KEY-CentOS-6 #gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 [updates] name=CentOS-$releasever-$basearch-Updates-192.168.111.33 baseurl=http://192.168.111.33/centos/$releasever/updates/$basearch/ #baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/ enabled=1 gpgcheck=1 gpgkey=http://192.168.111.33/centos/RPM-GPG-KEY-CentOS-6 #gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 [extras] name=CentOS-$releasever-$basearch-Extras-192.168.111.33 baseurl=http://192.168.111.33/centos/$releasever/extras/$basearch/ #baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/ enabled=1 gpgcheck=1 gpgkey=http://192.168.111.33/centos/RPM-GPG-KEY-CentOS-6 #gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 [epel] name=CentOS-$releasever-$basearch-Epel-192.168.111.33 baseurl=http://192.168.111.33/epel/$releasever/$basearch/ #baseurl=http://mirrors.aliyun.com/epel/$releasever/$basearch/ enabled=1 gpgcheck=1 gpgkey=http://192.168.111.33/epel/RPM-GPG-KEY-EPEL-6 #gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
下面是centos7的repo文件的具体内容
[root@master /data/samba/public]#cat CentOS-7-192.168.111.33.repo [base] name=CentOS-$releasever-$basearch-Base-192.168.111.33 baseurl=http://192.168.111.33/centos/$releasever/os/$basearch/ #baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/ enabled=1 gpgcheck=1 gpgkey=http://192.168.111.33/centos/RPM-GPG-KEY-CentOS-7 #gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 [updates] name=CentOS-$releasever-$basearch-Updates-192.168.111.33 baseurl=http://192.168.111.33/centos/$releasever/updates/$basearch/ #baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/ enabled=1 gpgcheck=1 gpgkey=http://192.168.111.33/centos/RPM-GPG-KEY-CentOS-7 #gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 [extras] name=CentOS-$releasever-$basearch-Extras-192.168.111.33 baseurl=http://192.168.111.33/centos/$releasever/extras/$basearch/ #baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/ enabled=1 gpgcheck=1 gpgkey=http://192.168.111.33/centos/RPM-GPG-KEY-CentOS-7 #gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 [epel] name=CentOS-$releasever-$basearch-Epel-192.168.111.33 baseurl=http://192.168.111.33/epel/$releasever/$basearch/ #baseurl=http://mirrors.aliyun.com/epel/$releasever/$basearch/ enabled=1 gpgcheck=1 gpgkey=http://192.168.111.33/epel/RPM-GPG-KEY-EPEL-7 #gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
下面是docker的repo文件的具体内容
[root@master /data/samba/public]#cat docker-ce-192.168.111.33.repo [docker-ce-stable] name=Docker CE Stable - $basearch baseurl=http://192.168.111.33/docker-ce/linux/centos/7/$basearch/stable enabled=1 gpgcheck=1 gpgkey=http://192.168.111.33/docker-ce/linux/centos/gpg [docker-ce-edge] name=Docker CE Edge - $basearch baseurl=http://192.168.111.33/docker-ce/linux/centos/7/$basearch/edge enabled=1 gpgcheck=1 gpgkey=http://192.168.111.33/docker-ce/linux/centos/gpg
下面是mysql的repo文件的具体内容
cat mysql.repo # Enable to use MySQL 5.6 [mysql56-community] name=MySQL 5.6 Community Server baseurl=http://192.168.111.33/mysql/yum/mysql56-community-el$releasever/ enabled=1 gpgcheck=0 # Enable to use MySQL 5.7 [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://192.168.111.33/mysql/yum/mysql56-community-el$releasever/ enabled=0 gpgcheck=0 # Enable to use MySQL 8.0 [mysql80-community] name=MySQL 8.0 Community Server baseurl=http://192.168.111.33/mysql/yum/mysql56-community-el$releasever/ enabled=0 gpgcheck=0
普通用户使用方法:centos6
最好将:/etc/yum.repo.d目录下面的文件修改为不以.repo结尾的名字,或者全部删除
[root@httpre /etc/yum.repos.d]#rm -rf * [root@httpre /etc/yum.repos.d]#cd [root@httpre ~]#wget http://192.168.111.33/CentOS-6-192.168.111.33.repo -P /etc/yum.repos.d/ && yum clean all && yum makecache && yum repolist
普通用户使用方法:centos7
最好将:/etc/yum.repo.d目录下面的文件修改为不以.repo结尾的名字,或者全部删除
[root@localhost ~]#wget http://192.168.111.33/CentOS-7-192.168.111.33.repo -P /etc/yum.repos.d/ && yum clean all && yum makecache && yum repolist [root@localhost ~]#wget http://192.168.111.33/docker-ce-192.168.111.33.repo -P /etc/yum.repos.d/ && yum clean all && yum makecache && yum repolist
同步日志不大,不用加到logrotate里面
–
centos历史版本的iso镜像下载地址
http://archive.kernel.org/centos-vault/
然后就可以愉快的玩耍了
–
–
–
评论前必须登录!
注册