创建本地yum源和局域网yum源

搭建本地yum源和局域网yum源,解决内网环境离线安装的问题

1. 本地yum源

分两种,一种是来源于ISO中,另一种是自己有一堆rpm安装包。

1.1 ISO来源的

  1. 挂载CentOS-7-x86_64-Everything-1511.iso,把里面所有文件都拷贝到本地目录/yum/yum-iso

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    [root@min-base ~]# mkdir /mnt/yum-iso
    [root@min-base ~]# mount /dev/cdrom /mnt/yum-iso/
    mount: /dev/sr0 is write-protected, mounting read-only
    [root@min-base ~]# mkdir /yum/
    [root@min-base ~]# cp -a /mnt/yum-iso/ /yum/
    [root@min-base ~]# ll /yum/
    total 4
    dr-xr-xr-x. 8 root root 4096 Dec 10 2015 yum-iso
    [root@min-base ~]# ll /yum/yum-iso/
    total 640
    -r--r--r--. 1 root root 14 Dec 10 2015 CentOS_BuildTag
    dr-xr-xr-x. 3 root root 33 Dec 10 2015 EFI
    -r--r--r--. 1 root root 215 Dec 10 2015 EULA
    -r--r--r--. 1 root root 18009 Dec 10 2015 GPL
    dr-xr-xr-x. 3 root root 69 Dec 10 2015 images
    dr-xr-xr-x. 2 root root 4096 Dec 10 2015 isolinux
    dr-xr-xr-x. 2 root root 41 Dec 10 2015 LiveOS
    dr-xr-xr-x. 2 root root 483328 Dec 10 2015 Packages
    dr-xr-xr-x. 2 root root 4096 Dec 10 2015 repodata
    -r--r--r--. 1 root root 1690 Dec 10 2015 RPM-GPG-KEY-CentOS-7
    -r--r--r--. 1 root root 1690 Dec 10 2015 RPM-GPG-KEY-CentOS-Testing-7
    -r--r--r--. 1 root root 2883 Dec 10 2015 TRANS.TBL
    [root@min-base ~]#
  2. 备份、移除原来的repo

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@min-base ~]#  ll /etc/yum.repos.d/
    -rw-r--r--. 1 root root 1664 Dec 9 2015 CentOS-Base.repo
    -rw-r--r--. 1 root root 1309 Dec 9 2015 CentOS-CR.repo
    -rw-r--r--. 1 root root 649 Dec 9 2015 CentOS-Debuginfo.repo
    -rw-r--r--. 1 root root 290 Dec 9 2015 CentOS-fasttrack.repo
    -rw-r--r--. 1 root root 630 Dec 9 2015 CentOS-Media.repo
    -rw-r--r--. 1 root root 1331 Dec 9 2015 CentOS-Sources.repo
    -rw-r--r--. 1 root root 1952 Dec 9 2015 CentOS-Vault.repo
    [root@min-base ~]# mv /etc/yum.repos.d /etc/yum.repos.d-bak
    [root@min-base ~]# mkdir /etc/yum.repos.d
  3. 配置本地repo

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@min-base ~]# vim /etc/yum.repos.d/CentOS-Media.repo

    [c7-media]
    name=CentOS-$releasever - Media
    baseurl=file:///yum/yum-iso/
    gpgcheck=0
    enabled=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

    注:配置repo路径、关闭gbp校验、启用这个repo
  4. 制作cache

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@min-base yum.repos.d]# yum clean all
    Loaded plugins: fastestmirror
    Cleaning repos: c7-media
    Cleaning up everything
    Cleaning up list of fastest mirrors
    [root@min-base yum.repos.d]# yum makecache
    Loaded plugins: fastestmirror
    c7-media | 3.6 kB 00:00:00
    (1/4): c7-media/group_gz | 155 kB 00:00:00
    (2/4): c7-media/primary_db | 5.3 MB 00:00:00
    (3/4): c7-media/filelists_db | 6.2 MB 00:00:00
    (4/4): c7-media/other_db | 2.3 MB 00:00:00
    Determining fastest mirrors
    Metadata Cache Created
    [root@min-base yum.repos.d]#
  5. 找个原来没有的安装过的包测试一下

    1
    2
    3
    4
    5
    6
    7
    [root@min-base yum.repos.d]# yum install tree
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Resolving Dependencies
    --> Running transaction check
    ---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
    --> Finished Dependency Resolution

1.2 自定义的yum源

  1. 准备rpm包,将其全部都放在一个指定的目录,例如:/yum/yum-custom/packages

  2. 创建repo

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [root@min-base ~]# createrepo -u -d  /yum/yum-custom/
    Spawning worker 0 with 1 pkgs
    Spawning worker 1 with 0 pkgs
    Workers Finished
    Saving Primary metadata
    Saving file lists metadata
    Saving other metadata
    Generating sqlite DBs
    Sqlite DBs complete
    [root@min-base ~]# ll /yum/yum-custom/
    total 4
    drwxr-xr-x. 2 root root 41 Dec 20 07:03 packages
    drwxr-xr-x. 2 root root 4096 Dec 20 07:08 repodata
    [root@min-base ~]#

    注:createrepo如果没有这个说明没安装,要去找rpm包手动安装
  3. 配置自定义repo

    1
    2
    3
    4
    5
    6
    7
    8
    [root@min-base ~]# vi /etc/yum.repos.d/CentOS-Media.repo

    [c7-media]
    name=CentOS-$releasever - Media
    baseurl=file:///yum/yum-custom/
    gpgcheck=0
    enabled=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  4. 制作cache和安装测试同上

局域网YUM源

局域网YUM源可以配成:本地YUM源 + FTP服务器

  1. 先建立本地yum源,见上述

  2. 安装vsftpd

    1
    [root@min-base ~]# yum -y install vsftpd
  3. 配置vsftpd

    1
    2
    3
    4
    5
    [root@min-base ~]# vi /etc/vsftpd/vsftpd.conf

    并增加匿名用户root目录(默认已经启用匿名访问)
    anon_root=/yum/ ---这里只想本地yum的目录

  4. 确认目录权限

    1
    2
    3
    4
    5
    6
    7
    [root@min-base ~]# ll -a /yum/
    total 8
    drwxr-xr-x. 4 root root 37 Dec 20 07:37 .
    dr-xr-xr-x. 18 root root 4096 Dec 20 06:32 ..
    drwxr-xr-x. 4 root root 36 Dec 20 07:08 yum-custom
    dr-xr-xr-x. 8 root root 4096 Dec 10 2015 yum-iso

  5. 关闭selinux

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # 临时
    [root@min-base ~]# setenforce 0

    永久:
    [root@min-base ~]# vi /etc/selinux/config
    设置

    SELINUX=disabled
    重启

    [root@min-base ~]# reboot
  6. 启用vsftp

    1
    2
    3
    4
    [root@min-base ~]# systemctl start vsftpd
    [root@min-base ~]# systemctl enable vsftpd
    Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
    [root@min-base ~]#
  7. 另一台局域网内的机器验证是否可以使用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    编辑repo
    [root@min-client ~]# vim /etc/yum.repos.d/CentOS-Media.repo

    [c7-media]
    name=CentOS-$releasever - Media
    baseurl=ftp://192.168.118.133/yum-custom
    gpgcheck=0
    enabled=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

    注:
    其中192.168.118.133为上面vsftp服务器地址

    验证:
    [root@min-client ~]# yum clean all
    Loaded plugins: fastestmirror
    Cleaning repos: c7-media
    Cleaning up everything
    [root@min-client ~]# yum makecache
    Loaded plugins: fastestmirror
    c7-media | 3.0 kB 00:00
    (1/3): c7-media/filelists_db | 880 B 00:00
    (2/3): c7-media/other_db | 1.3 kB 00:00
    (3/3): c7-media/primary_db | 1.8 kB 00:00
    Determining fastest mirrors
    Metadata Cache Created
    [root@min-base ~]# yum -y install tree
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Resolving Dependencies
    --> Running transaction check
    ---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
    --> Finished Dependency Resolution