Skip to content Skip to main navigation Skip to footer

CentOS 7/RHEL 7: 如何创建本地YUM软件仓库源

本文中的“本地YUM源”包括三种类型:一是直接使用CentOS光盘作为本地yum源,优点是简单便捷,缺点是光盘软件包可能不完整(centos 7 Everything 总共才6.57G);二是同步CentOS官方源到本地(相当于创建了一个公共镜像),优点是最靠谱,缺点是占空间费流量;三是创建完全自定义的本地源,优点是灵活性最大,缺点是只是作为其他源的补充。本文将会从下面三个方法来讲述如何搭建centos 7系统的yum 软件源仓库
一、使用CentOS光盘作为本地yum源
(1) 实体机上直接放入光盘
(2) VMware Workstation 虚拟机:虚拟机–>可移动设备–>CD/DVD–>连接

mkdir /mnt/cdrom #创建目录
mount /dev/cdrom /mnt/cdrom #挂载
umount /mnt/cdrom #卸载
 

若不想每次都要放光盘,那就复制光盘文件到本地硬盘吧

cp -avf /mnt/cdrom /yum # -a:该选项保留链接、文件属性,并递归地复制目录
 

创建repo文件

    cat >>/etc/yum.repos.d/CentOS-Local.repo<<-EOF
    &#91;Local&#93;
    name=Local Yum
    baseurl=file:///yum/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    enabled=1
 &#91;/code&#93;
问:为什么连createrepo软件都不用安装?
createrepo 的作用是创建索引文件的(位于repodata文件夹),光盘中连GPG-KEY都有了。
<strong>二、如何为CentOS创建公共镜像</strong>
详细说明见官方中文文档:http://wiki.centos.org/zh/HowTos/CreatePublicMirrors
提供一个SHELL脚本,然后cron自动运行~~
http://wangyan.org/blog/centos-local-yum-repo.html
脚本稍作修改,换成中科大的镜像源。可用镜像列表见:http://www.centos.org/download/mirrors/
 
    localpath="/mirrors/CentOS"
    mirror="mirrors.hustunique.com::centos"
    rsync="rsync -avzH --delete"
    verlist="7"
    baselist="centosplus extras fasttrack os updates"
    archlist="x86_64"
    for ver in $verlist
    do
        for base in $baselist
        do
            for arch in $archlist
            do
                remote=$mirror/$ver/$base/$arch/
                mkdir -pv $localpath/$ver/$base/$arch/
                $rsync $remote $localpath/$ver/$base/$arch/
            done
        done
    done
 

三、创建完全自定义的本地源
把平时收藏的一些RPM软件包放在一个目录里,就变成一个自定义源了,可以用YUM来管理,灵活性相当大。
1、创建本地yum仓库

    mkdir -p /yum/local #可以有N级目录
    cp ... #复制RPM包到上述目录
 

2、创建repo文件

    cat >>/etc/yum.repos.d/CentOS-Local.repo<<-EOF
    &#91;Local&#93;
    name=Local Yum
    baseurl=file:///yum/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    enabled=1
 &#91;/code&#93;
<strong>3、安装createrepo</strong>
 
    yum install createrepo #使用yum安装createrepo
    rpm -ivh http://mirror.centos.org/centos/7/os/x86_64/Packages/createrepo-0.9.9-23.el7.noarch.rpm
 

4、创建索引

    createrepo /yum
    yum makecache #更新缓存
 

四、添加yum源

     [1] Add RPMforge Repository for yum because that has many useful packages.
    [root@dlp ~]#
    wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
    [root@dlp ~]#
    rpm -Uvh rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
    Preparing… ###################################### [100%] 1:rpmforge-release ###################################### [100%]
    [root@dlp ~]#
    sed -i -e "s/enabled = 1/enabled = 0/g" /etc/yum.repos.d/rpmforge.repo
    [2] Add repository EPEL that is provided from Fedora project.
    [root@dlp ~]#
    wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
    [root@dlp ~]#
    rpm -Uvh epel-release-6-8.noarch.rpm
    Preparing… ###################################### [100%] 1:epel-release ###################################### [100%]
    [root@dlp ~]#
    sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/epel.repo
 

五、参考
http://m.oschina.net/blog/88071
http://jingyan.baidu.com/article/e9fb46e1aed8207521f7662c.html
http://blog.chinaunix.net/uid-28283843-id-3930474.html
http://www.ibm.com/developerworks/cn/linux/l-lpic1-v3-102-5/

0 Comments

There are no comments yet

Leave a comment

Your email address will not be published.