Centos构建本地局域网源(基于HTTP实现)附带同步外网YUM

柳三千

文章最后更新时间:2022年12月06日已超过724天没有更新。

挂载光盘本地源

添加光盘文件

图片.png

#创建挂载目录
[root@controller ~]# mkdir -p /media/centos

#挂载
[root@controller ~]# mount /dev/cdrom /media/centos/

[root@controller ~]# ll /media/centos/
total 696
-rw-r--r--  3 root root     14 Oct 30  2020 CentOS_BuildTag
drwxr-xr-x  3 root root   2048 Oct 27  2020 EFI
-rw-rw-r-- 21 root root    227 Aug 30  2017 EULA
-rw-rw-r-- 21 root root  18009 Dec 10  2015 GPL
drwxr-xr-x  3 root root   2048 Oct 27  2020 images
drwxr-xr-x  2 root root   2048 Nov  3  2020 isolinux
drwxr-xr-x  2 root root   2048 Oct 27  2020 LiveOS
drwxr-xr-x  2 root root 673792 Nov  4  2020 Packages
drwxr-xr-x  2 root root   4096 Nov  4  2020 repodata
-rw-rw-r-- 21 root root   1690 Dec 10  2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r-- 21 root root   1690 Dec 10  2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r--  1 root root   2883 Nov  4  2020 TRANS.TBL

#备份repo文件
[root@controller ~]# mv /etc/yum.repos.d/*.repo /bak/

#新建repo文件
[root@controller ~]# vim /etc/yum.repos.d/Media.repo
[cdrom]
name=media
baseurl=file:///media/centos
gpgcheck=0
enabled=1

[root@controller ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: cdrom
Cleaning up list of fastest mirrors
Other repos take up 523 M of disk space (use --verbose for details)
[root@controller ~]# yum makecache
Loaded plugins: fastestmirror
Determining fastest mirrors
cdrom                                                                      | 3.6 kB  00:00:00     
(1/4): cdrom/group_gz                                                      | 153 kB  00:00:00     
(2/4): cdrom/primary_db                                                    | 3.3 MB  00:00:00     
(3/4): cdrom/other_db                                                      | 1.3 MB  00:00:00     
(4/4): cdrom/filelists_db                                                  | 3.3 MB  00:00:00     
Metadata Cache Created

#查看多少软件包
[root@controller ~]#  yum list | wc -l
4134


搭建本地局域网源

源码编译安装Nginx

#安装依赖包
[root@controller ~]# yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

#下载Nginx安装包
[root@controller ~]# cd /usr/local/src/
[root@controller src]# wget https://nginx.org/download/nginx-1.20.2.tar.gz
--2022-12-02 14:51:33--  https://nginx.org/download/nginx-1.20.2.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5704::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1062124 (1.0M) [application/octet-stream]
Saving to: ‘nginx-1.20.2.tar.gz’

100%[========================================================>] 1,062,124    882KB/s   in 1.2s   

2022-12-02 14:51:36 (882 KB/s) - ‘nginx-1.20.2.tar.gz’ saved [1062124/1062124]


#解压编译安装
[root@controller src]# tar zxvf nginx-1.20.2.tar.gz
[root@controller src]# cd nginx-1.20.2
[root@controller nginx-1.20.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@controller nginx-1.20.2]# make && make install

#建立快捷方式
[root@controller nginx-1.20.2]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

[root@controller nginx-1.20.2]# nginx -version
nginx version: nginx/1.20.2

#新建nginx系统用户
[root@controller nginx-1.20.2]# useradd -s /sbin/nologin nginx

#修改配置文件
[root@controller nginx]# vim /usr/local/nginx/conf/nginx.conf
user  nginx;
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
       #主要修改location
        location / {
            autoindex on;
            root   html/centos/;
        }


#测试配置文件
[root@controller nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

#开启nginx
[root@controller nginx]# nginx


创建YUM源端

[root@controller nginx]# yum install createrepo* -y

[root@controller nginx]# mkdir /usr/local/nginx/html/centos
[root@controller nginx]# cd /mnt/centos/
[root@controller centos]# cp Packages/* /usr/local/nginx/html/centos/

[root@controller media]# cd /usr/local/nginx/html/
#使用 Createrepo 创建本地源
[root@controller html]# createrepo centos/
Spawning worker 0 with 2035 pkgs
Spawning worker 1 with 2035 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

[root@controller html]# cd centos/repodata/
[root@controller repodata]# ls
0fb14ce7d89e3053447ac8442fd3472451ea84bda1d5c9d38328193613f0a84b-primary.sqlite.bz2
4a1c8221285db1b9d5361d4567baa883e60ee29c32d8d0a1b7777d7d7761664d-filelists.xml.gz
60aa4673bf5fa63d1a7a640ef407b8e48fe5b3337720d844a29a53fec130d6da-filelists.sqlite.bz2
7d37fe033f19d2c678fd43b307f3f9ac7e6bfb032238a4a330b0229c1f32f1e2-primary.xml.gz
9d6292fa12fd9ca472fafa8c0b348c3e1f139681aa066d41dc53b3563bcd1f53-other.sqlite.bz2
b157d8b012febabf1c72d579b5155b8519a362a64bbd37c40acb22b6cc544ccf-other.xml.gz
repomd.xml

图片.png


其他主机测试

[root@localhost ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:57:68:0f brd ff:ff:ff:ff:ff:ff
    inet 192.168.139.136/24 brd 192.168.139.255 scope global noprefixroute dynamic ens33
       valid_lft 1684sec preferred_lft 1684sec
    inet6 fe80::aa12:63d5:a536:90cc/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
       
[root@localhost ~]# rm -rf /etc/yum.repos.d/*.repo
[root@localhost ~]# ls /etc/yum.repos.d/
bak

[root@localhost ~]# vi /etc/yum.repos.d/centos_http.repo
[root@localhost yum.repos.d]# cat centos_http.repo 
[nginx_yum]
name=host_nginx
baseurl=http://192.168.139.135/
enabled=1
gpgcheck=0

[updates]
name=host_nginx
baseurl=http://192.168.139.135/
enabled=1
gpgcheck=0
[root@localhost yum.repos.d]# yum makecache
Loaded plugins: fastestmirror
Determining fastest mirrors
nginx_yum                                                                  | 2.9 kB  00:00:00     
updates                                                                    | 2.9 kB  00:00:00     
(1/6): nginx_yum/filelists_db                                              | 3.3 MB  00:00:00     
(2/6): nginx_yum/primary_db                                                | 3.2 MB  00:00:00     
(3/6): nginx_yum/other_db                                                  | 1.3 MB  00:00:00     
(4/6): updates/primary_db                                                  | 3.2 MB  00:00:00     
(5/6): updates/filelists_db                                                | 3.3 MB  00:00:00     
(6/6): updates/other_db                                                    | 1.3 MB  00:00:00     
Metadata Cache Created

[root@localhost yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                      repo name                                      status
nginx_yum                                    host_nginx                                     4,070
updates                                      host_nginx                                     4,070
repolist: 8,140


YUM 源端软件包扩展

1、把下载 的软件包添加至centos目录

2、Createrepo 命令更新软件包即可

createrepo --update centos/


同步外网 YUM 源

(1) 下载 CentOS7 REPO 文件(阿里云或者163的都行)至/etc/yum.repos.d/,并安装 reposync 命令工具:

[root@controller ~]# curl -O https://mirrors.aliyun.com/repo/Centos-7.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2523  100  2523    0     0   5931      0 --:--:-- --:--:-- --:--:--  5936


[root@controller ~]# mv Centos-7.repo /etc/yum.repos.d/centos.repo
yum clean all
yum install yum-utils createrepo –y
yum repolist


(2) 通过 reposync 命令工具获取外网 YUM 源所有软件包

-r 指定 repolist id,

默认不加-r 表示获取外网所有 YUM 软件包

-p参数表示指定下载软件的路径,

reposync -r base -p /usr/local/nginx/html/centos/
reposync -r updates -p /usr/local/nginx/html/centos/


(3) 通过 reposync 工具下载完所有的软件包之后,需要执行 createrepo 更新本地
YUM 仓库:

createrepo /usr/local/nginx/html/centos/


文章版权声明:除非注明,否则均为柳三千运维录原创文章,转载或复制请以超链接形式并注明出处。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码