Centos7.1搭建本地Yum仓库(Centos6&7&epel)

1.系统环境准备

[root@zon ~]# cat /etc/redhat-release && uname -r
CentOS Linux release 7.1.1503 (Core) 
3.10.0-229.el7.x86_64
[root@zon ~]# getenforce && systemctl status firewalld;hostname -I
Disabled
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled)
   Active: inactive (dead)

192.168.56.12 

2.安装createrepo

[root@zon ~]# yum install createrepo -y
================================================================================================
 Package                     Arch               Version                  Repository        Size
================================================================================================
Installing:
 createrepo                  noarch             0.9.9-28.el7             base              94 k
Installing for dependencies:
 deltarpm                    x86_64             3.6-3.el7                base              82 k
 python-deltarpm             x86_64             3.6-3.el7                base              31 k

Transaction Summary
================================================================================================
Install  1 Package (+2 Dependent packages)

3.创建相关目录及更改配置

#创建本地yum仓库repo发布目录(rpm包存放目录)
[root@zon ~]# mkdir /data/local_yum_data -p
#创建本地yum仓库同步目录
[root@zon Packages]# mkdir /data/local_yum_data/centos/6/os/x86_64/ -p
[root@zon Packages]# mkdir /data/local_yum_data/centos/6/extras/x86_64/ -p
[root@zon Packages]# mkdir /data/local_yum_data/centos/6/updates/x86_64/ -p
[root@zon Packages]# mkdir /data/local_yum_data/epel/6/x86_64/ -p

4.提供yum服务(Http:80)

#可以用Apache或nginx提供web服务,适用于内网环境
##基于HTTP的yum源配置
#安装apache软件
    yum install -y httpd
    vim /etc/httpd/conf/httpd.conf
#将默认的www目录指向本地创建的yum仓库目录
    [root@zon data]# grep -v "#" /etc/httpd/conf/httpd.conf
    ServerRoot "/etc/httpd"
    Listen 80
    Include conf.modules.d/*.conf
    User apache
    Group apache
    ServerAdmin root@localhost
    ServerName 192.168.56.12:80
    <Directory />
        AllowOverride none
        Require all denied
    </Directory>
    DocumentRoot "/data/local_yum_data/"
    <Directory "/data/local_yum_data/">
        AllowOverride None
        Require all granted
    </Directory>
    <Directory "/data/local_yum_data/">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
 #修改/usr/local/yumrepo目录的属主和属组为apache
    chown -R apache.apache /data/yum_data/
#删除默认主页
    rm /etc/httpd/conf.d/welcome.conf
#启动服务
    #service httpd restart

浏览器输入http本机IP,显示出列表文件
Centos7.1搭建本地Yum仓库(Centos6&7&epel)_第1张图片

利用python快速搭建http服务,提供yum服务

    cd /data/local_yum_data/ && nohup python -m SimpleHTTPServer 80 &>/dev/null &

浏览器输入http本机IP,显示出列表文件
Centos7.1搭建本地Yum仓库(Centos6&7&epel)_第2张图片

5.同步官方rpm包文件

5.1 Centos-6同步官方rpm包文件

    #官方源(centos6)
    rsync -av rsync://mirrors.ustc.edu.cn/centos/6/os/x86_64/ /data/local_yum_data/centos/6/os/x86_64/
    rsync -av rsync://mirrors.ustc.edu.cn/centos/6/extras/x86_64/ /data/local_yum_data/centos/6/extras/x86_64/
    rsync -av rsync://mirrors.ustc.edu.cn/centos/6/updates/x86_64/ /data/local_yum_data/centos/6/updates/x86_64/
    rsync -av --exclude=debug rsync://mirrors.ustc.edu.cn/epel/6/x86_64/ /data/local_yum_data/epel/6/x86_64/

5.2 Centos-7同步官方rpm包文件

    #官方源(centos6)
    rsync -av rsync://mirrors.ustc.edu.cn/centos/7/os/x86_64/ /data/local_yum_data/centos/7/os/x86_64/
    rsync -av rsync://mirrors.ustc.edu.cn/centos/7/extras/x86_64/ /data/local_yum_data/centos/7/extras/x86_64/
    rsync -av rsync://mirrors.ustc.edu.cn/centos/7/updates/x86_64/ /data/local_yum_data/centos/7/updates/x86_64/
    rsync -av --exclude=debug rsync://mirrors.ustc.edu.cn/epel/7/x86_64/ /data/local_yum_data/epel/7/x86_64/

5.3 初始化repodata索引文件

    #初始化repodata索引文件
    [root@zon ~]# createrepo -h
    [root@zon ~]# createrepo -pdo /data/local_yum_data/ /data/local_yum_data/
    Saving Primary metadata
    Saving file lists metadata
    Saving other metadata
    Generating sqlite DBs
    Sqlite DBs complete

6.客户端yum源指定

6.1 Centos-6客户端yum源指定

    cat>> /etc/yum.repos.d/local_yum.repo <<EOF
    [local_yum]
    name=centos6
    baseurl=http://192.168.56.12/centos/6/os/x86_64/
    enable=1
    gpgcheck=0
    
    [local_extra]
    name=extra
    baseurl=http://192.168.56.12/centos/6/extras/x86_64/
    enable=1
    gpgcheck=0
    
    [local_epel]
    name=epel
    baseurl=http://192.168.56.12/epel/6/x86_64/
    enable=1
    gpgcheck=0
    EOF
    #在每个标签下面将enabled=1改为0禁用,没有的添加上
    vim /etc/yum.repos.d/CentOS-Base.repo
    [base]
    name=CentOS-$releasever - Base
    mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    
    [updates]
    name=CentOS-$releasever - Updates
    mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    
    [extras]
    name=CentOS-$releasever - Extras
    mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    
    [centosplus]
    name=CentOS-$releasever - Plus
    mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    
    [contrib]
    name=CentOS-$releasever - Contrib
    mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib&infra=$infra
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

6.2 Centos-7客户端yum源指定

    cat>> /etc/yum.repos.d/local_yum.repo <<EOF
    [local_yum]
    name=centos6
    baseurl=http://192.168.56.12/centos/7/os/x86_64/
    enable=1
    gpgcheck=0
        
    [local_extra]
    name=extra
    baseurl=http://192.168.56.12/centos/7/extras/x86_64/
    enable=1
    gpgcheck=0
    
    [local_epel]
    name=epel
    baseurl=http://192.168.56.12/epel/7/x86_64/
    enable=1
    gpgcheck=0
    EOF
    #在每个标签下面将enabled=1改为0禁用,没有的添加上
    vim /etc/yum.repos.d/CentOS-Base.repo
    [base]
    name=CentOS-$releasever - Base
    mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    
    [updates]
    name=CentOS-$releasever - Updates
    mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    
    [extras]
    name=CentOS-$releasever - Extras
    mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    
    [centosplus]
    name=CentOS-$releasever - Plus
    mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

7.测试(Centos6)

    [root@oldboy ~]# ping 223.5.5.5
    connect: Network is unreachable
    [root@oldboy ~]# ping www.baidu.com
    ping: unknown host www.baidu.com
    [root@oldboy ~]# yum install tree
    Loaded plugins: fastestmirror, security
    Setting up Install Process
    Loading mirror speeds from cached hostfile
    local_extra                                                            | 3.4 kB     00:00     
    local_yum                                                              | 3.7 kB     00:00     
    Resolving Dependencies
    --> Running transaction check
    ---> Package tree.x86_64 0:1.5.3-3.el6 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ==============================================================================================
     Package           Arch                Version                   Repository              Size
    ==============================================================================================
    Installing:
     tree              x86_64              1.5.3-3.el6               local_yum               36 k
    
    Transaction Summary
    ==============================================================================================
    Install       1 Package(s)
    
    Total download size: 36 k
    Installed size: 65 k
    Is this ok [y/N]: 

你可能感兴趣的:(Linux运维)