搭建局域网内的 YUM 源

公司出口带宽有限?
公司研发没法上外网?
自行搭建 YUM 源服务吧

步骤

  1. 创建目录、拷贝 rpm 包
  2. 创建 repository
  3. 安装 nginx
  4. 客户端上配置 repo 文件
  5. 从知名镜像站同步 rpm 包
  6. 后续

1. 创建目录、拷贝rpm包

1.1 创建一个目录,用于存放 rpm 包

mkdir

[root@choco-01 /]# mkdir /YUM ; ll | grep YUM
drwxr-xr-x    2 root root    6 May 29 10:55 YUM

1.2 放点 ISO 中的 rpm

mount /dev/cdrom /mnt
cp /mnt/Packages/a*

[root@choco-01 /]# mount /dev/cdrom /mnt/ ; ls /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
CentOS_BuildTag  GPL       RPM-GPG-KEY-CentOS-7          images
EFI              LiveOS    RPM-GPG-KEY-CentOS-Testing-7  isolinux
EULA             Packages  TRANS.TBL                     repodata




[root@choco-01 /]# cp /mnt/Packages/a*rpm /YUM/ ;  ls /YUM/
abattis-cantarell-fonts-0.0.25-1.el7.noarch.rpm
abrt-2.1.11-48.el7.centos.x86_64.rpm
abrt-addon-ccpp-2.1.11-48.el7.centos.x86_64.rpm
abrt-addon-kerneloops-2.1.11-48.el7.centos.x86_64.rpm
abrt-addon-pstoreoops-2.1.11-48.el7.centos.x86_64.rpm
abrt-addon-python-2.1.11-48.el7.centos.x86_64.rpm
abrt-addon-vmcore-2.1.11-48.el7.centos.x86_64.rpm
.
.
.

2. 创建 repository

2.1 安装 createrepo

yum -y install createrepo

[root@choco-01 yum.repos.d]# yum list | grep createrepo
Failed to set locale, defaulting to C
createrepo.noarch                        0.9.9-28.el7                  @base    
createrepo_c.x86_64                      0.10.0-6.el7                  epel     
createrepo_c-devel.x86_64                0.10.0-6.el7                  epel     
createrepo_c-libs.x86_64                 0.10.0-6.el7                  epel     
python-createrepo_c.x86_64               0.9.0-1.el7.centos            extras   
python2-createrepo_c.x86_64              0.10.0-6.el7                  epe
[root@choco-01 yum.repos.d]# yum install -y createrepo

2.2 创建 repository

createrepo #在中产生元数据

[root@choco-01 yum.repos.d]# createrepo /YUM/
Spawning worker 0 with 137 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@choco-01 yum.repos.d]# ll /YUM/  | grep repodata
drwxr-xr-x 2 root root     4096 May 29 13:00 repodata

2.3 仓库变更后,更新 repository

如果 rpm 包有增加或减少,需要执行以下命令
createrepo --update


3. 安装nginx

3.1 安装 nginx

yum -y install epel-release

yum -y install nginx

3.2 修改 nginx 配置文件 /etc/nginx/nginx.conf

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /data/yumdata;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
                autoindex on;                      #这个必须有,为了能提供目录浏览
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

3.3 启动 nginx 服务

systemctl start nginx

3.4 iptables、DNS 等

  • 关闭或调整 iptables
    systemctl stop firewalld.service

  • 必要时将该服务器 IP 配上 DNS


4. 客户端上配置repo文件

  • 备份原有 repo 文件
[root@choco-01 yum.repos.d]# pwd
/etc/yum.repos.d
[root@choco-01 yum.repos.d]# mkdir bak
[root@choco-01 yum.repos.d]# ls
CentOS-Base.repo       CentOS-Media.repo      bak
CentOS-Base.repo.bak   CentOS-Sources.repo    epel-testing.repo
CentOS-CR.repo         CentOS-Vault.repo      epel.repo
CentOS-Debuginfo.repo  CentOS-fasttrack.repo
[root@choco-01 yum.repos.d]# mv *.repo* bak ; ls
bak
  • 新建 repo 文件
[root@choco-01 yum.repos.d]# vim my.repo

[my-repo]
name=myrepo
baseurl=http://192.168.1.101
gpgcheck=0
enabled=1

5. 从知名镜像站同步 rpm 包

[root@choco-01 yum.repos.d]# rsync -av rsync://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/ /YUM/


6. 后续

同步后,往往需要进行 repo 的更新,所以可以将同步&更新,写入同一个脚本,并加入 crontab
rsync -av rsync://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/ /YUM/ ; createrepo --update /YUM/

(The End)

你可能感兴趣的:(搭建局域网内的 YUM 源)