Centos7部署本地局域网yum源

由于ISO镜像中的软件包太少、太老,所以我的实验使用的yum源是阿里云镜像源;但是阿里云镜像源必须要连接外网才可以使用,做实验时倒无所谓;但是在公司部署大量服务器时会占用出口带宽。另外有些特殊的镜像源架设在国外,使用起来很不稳定。所以本地yum源仓库时必要的

下面是我在部署公司本地yum仓库时的具体部署步骤(以epel-7源为例):

1、部署环境:

  • 系统:Centos7 x86_64
  • 应用服务:httpd、createrepo、reposync
  • 镜像源:https://mirrors.aliyun.com/repo/epel-7.repo

2、现在阿里云镜像源repo文件到本地:

[root@localhost ~] wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

3、安装httpd服务,用于提供存放yum源:

[root@localhost ~] yum install httpd -y

  使用默认配置即可,如果有特殊需求可以修改配置文件/etc/httpd/conf/httpd.conf

  http默认家目录/var/www/html

4、安装yum-utils提供reporsync服务:

[root@localhost ~] yum install yum-utils -y

5、选择指定仓库标识作为本地yum源:

    执行yum repolist命令查看yum仓库标识

[root@localhost ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * epel: mirrors.aliyun.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
repo id                                                                                               repo name                                                                                                                            status
base/7/x86_64                                                                                         CentOS-7 - Base                                                                                                                       9,363
epel/x86_64                                                                                           Extra Packages for Enterprise Linux 7 - x86_64                                                                                       11,681
extras/7/x86_64                                                                                       CentOS-7 - Extras                                                                                                                       380
updates/7/x86_64                                                                                      CentOS-7 - Updates                                                                                                                    1,838
repolist: 23,262

  我使用epel作为本地yum源,用/var/www/html作为yum仓库根目录

[root@localhost ~]# reposync -r epel -p /var/www/html/

  命令执行完毕后,会将阿里云中的epel源同步到本地/var/www/html中;在/var/www/html中自动创建epel目录用于存放rpm包;第一次同步是时间可能较长,大概1W多个rpm包。

                                                                                                                                                                        |  19 kB  00:00:00     
(11680/11681): zvbi-fonts-0.2.35-1.el7.noarch.rpm                                                                                                                                                                         | 116 kB  00:00:00     
(11681/11681): xonotic-data-0.8.2-1.el7.noarch.rpm                                                                                                                                                                        | 873 MB  00:14:38     
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# ll /var/www/html/
total 4
drwxr-xr-x. 31 root root 4096 May 27 23:50 epel

6、createrepo 命令创对/var/www/html/epel下的 rpm 包 创建为本地的 YUM 仓库,目的是为生成repodata目录并自动创建索引信息

[root@localhost ~]# createrepo -pdo /var/www/html/epel/ /var/www/html/epel/  #第一个目录是repodata存放目录,第二个目录是需要生成索引信息yum源仓库目录
Spawning worker 0 with 5841 pkgs
Spawning worker 1 with 5840 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@localhost ~]# ll /var/www/html/epel/repodata/
total 34824
-rw-r--r--. 1 root root 2950038 May 28 02:09 458c248cc2fc1f9be754a7fd20f576c4acd231e32a49dfe26ba441d9ef1e8196-other.sqlite.bz2
-rw-r--r--. 1 root root 9980489 May 28 02:09 691ac5899b99b6a7d6de2c03f8706ab40a4104ffa95169837cd53860be9bd08c-filelists.sqlite.bz2
-rw-r--r--. 1 root root 9029887 May 28 02:09 9efa5c655cbaa93cfe74fa88f1521a00cdd7575a1ee537b3285c2f739470cd78-filelists.xml.gz
-rw-r--r--. 1 root root 6186568 May 28 02:09 b833e13dd3f617ae5c2f228991fcaecaf5b2602844da466fae2b6eef2578e64e-primary.sqlite.bz2
-rw-r--r--. 1 root root 3369194 May 28 02:09 f5dc4532b7ee4bc249b397e441842f684dc9df3b6b37cfb709080802c4fe9fce-primary.xml.gz
-rw-r--r--. 1 root root 2101964 May 28 02:09 f9f0522ce0356e3e95b0849e59ebfe35870c4359a8d1c6447b1d017a23ececbe-other.xml.gz
-rw-r--r--. 1 root root 3012 May 28 02:09 repomd.xml

7、验证本地yum源是否能正常使用:

  登录其他内网其他服务器,要能与yum源服务器通信;编写repo文件:

vim /etc/yum.repos.d/epel-7.repo内容如下

[epel]
name=local epel
baseurl=http://yum源服务器IP/epel
enabled=1
gpgcheck=0

  备份其他repo文件至/etc/yum.repos.d/repobak目录下,以防影响测试:

[root@localhost yum.repos.d]# ll /etc/yum.repos.d/
total 8
-rw-r--r--. 1 root root   79 May 28 17:26 epel.repo
drwxr-xr-x. 2 root root 4096 May 28 17:25 repobak

  执行yum clean all、yum makecache:

[root@localhost yum.repos.d]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: epel
Cleaning up everything
[root@localhost yum.repos.d]# yum makecache
Loaded plugins: fastestmirror
epel                                                                                                                                                                                                                      | 2.9 kB  00:00:00     
(1/3): epel/primary_db                                                                                                                                                                                                    | 5.9 MB  00:00:00     
(2/3): epel/filelists_db                                                                                                                                                                                                  | 9.5 MB  00:00:00     
(3/3): epel/other_db                                                                                                                                                                                                      | 2.8 MB  00:00:00     
Determining fastest mirrors
Metadata Cache Created
#上面信息表明本地epel源被正常缓存;表明配置成功。

7、为保证本地yum源能和阿里云镜像源同步,可以通过脚本定时任务实现:

vim /root/yum-update.sh
#!/bin/bash

datetime=`date +"%Y-%m-%d"`
exec > /var/log/epel.log  #同步日志输出
reposync -d -r epel -p /var/www/html/    #同步镜像源
if [ $? -eq 0 ];then
    createrepo --update  /var/www/html/epel   #每次添加新的rpm时,必须更新epel索引信息
    echo "SUCESS: $datetime epel update successful"else
    echo "ERROR: $datetime epel update failed"fi
#定时任务:每周六凌晨三点同步yum源
crontab -e
0 3 * * 6 /bin/bash /root/yum-update.sh

 

 

  

  

 

  

posted @ 2017-05-27 18:40  chengd  阅读(8892)  评论(1编辑  收藏  举报