Linux下创建私有CentOS7的yum源

2021年4月29日 69点热度 0人点赞 0条评论

描述

初始化服务器时往往需要安装大量的软件包,使用公网yum源可能会占用过多的网络资源,本文记录了搭建本地私有CentOS的yum源方法。

一、使用光盘搭建私有yum源

使用光盘搭建时有两种方法,一种是直接使用光盘内创建好的索引,另一种是自己创建索引,第二种方法适合增加或删除光盘中不需要的rpm软件包。

1.使用光盘自带索引

1)将光盘里面所有的文件都复制到/iso_repo文件夹内

[root@yum-server ~]# mount /dev/sr0 /mnt
mount: /dev/sr0 is write-protected, mounting read-only
[root@yum-server ~]# mkdir /iso_repo
[root@yum-server ~]# cp -r /mnt/* /iso_repo/

2)注释掉原来的yum源

[root@yum-server ~]# gzip /etc/yum.repos.d/*.repo

3)创建local_iso.repo本地光盘源配置文件

[root@yum-server ~]# vim /etc/yum.repos.d/local_iso.repo

[local_iso]
name = local iso repo
baseurl = file:///iso_repo
enabled = 1
gpgcheck = 0

4)清除并重新缓存yum源

[root@yum-server ~]# yum clean all && yum makecache

5)测试本地iso源

[root@yum-server ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                    repo name                                        status
local_iso                                  local iso repo                                   4,067
repolist: 4,067

[root@yum-server ~]# yum -y install mariadb &>/dev/null && echo $?
0

2.手动创建索引文件

1)清理刚才安装的软件包

[root@yum-server ~]# yum history
Loaded plugins: fastestmirror
ID     | Login user               | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
     4 | root <root>              | 2021-04-28 20:47 | Install        |    1   
     3 | root <root>              | 2021-04-28 20:46 | Install        |    5   
     2 | root <root>              | 2021-04-28 13:54 | Install        |   34   
     1 | System <unset>           | 2021-04-28 13:46 | Install        |  308   
history list
[root@yum-server ~]# yum history undo 4
[root@yum-server ~]# yum history undo 3

2) 删除/iso_repo内所有的文件

[root@yum-server ~]# cd /iso_repo/ && rm -rf *

3)将光盘内的所有rpm包复制到/iso_repo文件夹内

[root@yum-server /]# cp /mnt/Packages/*.rpm /iso_repo/

4)安装建立yum仓库索引的软件包

[root@yum-server /]# rpm -ivh /iso_repo/createrepo-0.9.9-28.el7.noarch.rpm 
error: Failed dependencies:
        deltarpm is needed by createrepo-0.9.9-28.el7.noarch
        libxml2-python is needed by createrepo-0.9.9-28.el7.noarch
        python-deltarpm is needed by createrepo-0.9.9-28.el7.noarch
[root@yum-server /]# rpm -ivh /iso_repo/deltarpm-3.6-3.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:deltarpm-3.6-3.el7               ################################# [100%]
[root@yum-server /]# rpm -ivh /iso_repo/libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:libxml2-python-2.9.1-6.el7_2.3   ################################# [100%]
[root@yum-server /]# rpm -ivh /iso_repo/python-deltarpm-3.6-3.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:python-deltarpm-3.6-3.el7        ################################# [100%]

[[root@yum-server /]# rpm -ivh /iso_repo/createrepo-0.9.9-28.el7.noarch.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:createrepo-0.9.9-28.el7          ################################# [100%]
[root@yum-server /]#

5)使用createrepo创建元数据

[root@yum-server /]# createrepo /iso_repo/
Spawning worker 0 with 4067 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

6) 清除并重新建立yum缓存

[root@yum-server /]# yum clean all && yum makecache
Loaded plugins: fastestmirror
Cleaning repos: local_iso
Cleaning up list of fastest mirrors
Other repos take up 83 M of disk space (use --verbose for details)
Loaded plugins: fastestmirror
Determining fastest mirrors
local_iso                                                                  | 2.9 kB  00:00:00     
(1/3): local_iso/filelists_db                                              | 3.3 MB  00:00:00     
(2/3): local_iso/other_db                                                  | 1.3 MB  00:00:00     
(3/3): local_iso/primary_db                                                | 3.2 MB  00:00:00     
Metadata Cache Created

7)测试yum仓库

[root@yum-server /]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                    repo name                                        status
local_iso                                  local iso repo                                   4,067
repolist: 4,067

[root@yum-server /]# yum install tree -y &>/dev/null && echo $?
0

 

二、从互联网的公共仓库下载rpm软件包

1)创建存放目录和下载脚本

[root@yum-server ~]# mkdir /tuna_repo
[root@yum-server ~]# cat get_tuna_c7.sh 
#!/usr/bin/bash


# 获取软件包名称
web_site="https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/Packages/"
pkg_name=$(curl -s ${web_site} | grep "<a" | awk -F '"' '{print $4}')


# 遍历包名称,依次赋予给i变量;
for i in ${pkg_name}
do
        wget -O /tuna_repo/${i} ${web_site}/${i}
done

2)使用脚本下载互联网上的rpm包

3)下载完成后,使用createrepo创建仓库索引

[root@yum-server ~]# createrepo /tuna_repo/
Spawning worker 0 with 10071 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

4)创建新的repo文件

[root@yum-server yum.repos.d]# cd /etc/yum.repos.d/

[root@yum-server yum.repos.d]# gzip local_iso.repo

[root@yum-server yum.repos.d]# cat local_tuna.repo 
[local_tuna]
name = local tuna repo
baseurl = file:///tuna_repo
enabled = 1
gpgcheck = 0

5)清除并创建yum缓存

[root@yum-server ~]# yum clean all && yum makecache
Loaded plugins: fastestmirror
Cleaning repos: local_tuna
Cleaning up list of fastest mirrors
Other repos take up 259 M of disk space (use --verbose for details)
Loaded plugins: fastestmirror
Determining fastest mirrors
local_tuna                                              | 2.9 kB     00:00     
(1/3): local_tuna/filelists_db                            | 7.2 MB   00:00     
(2/3): local_tuna/other_db                                | 2.6 MB   00:00     
(3/3): local_tuna/primary_db                              | 6.1 MB   00:00     
Metadata Cache Created

6)查看和验证local_tuna源

[root@yum-server ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                           repo name                              status
local_tuna                        local tuna repo                        10,071
repolist: 10,071

[root@yum-server ~]# yum -y install unzip &>/dev/null && echo $?
0

三、将yum仓库使用ftp协议共享到内网

yum仓库可以是ftp、http、file链接,我们使用ftp的方式向内网的机器提供yum仓库服务

1)安装vsftpd

[root@yum-server ~]# yum -y install vsftpd

[root@yum-server ~]# cp -r /tuna_repo /var/ftp/tuna_repo

[root@yum-server ~]# systemctl enable vsftpd --now
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.

[root@yum-server ~]# setenforce 0 && systemctl stop firewalld

2)在客户端配置yum源文件

[root@client ~]# cd /etc/yum.repos.d/

[root@client yum.repos.d]# vim local-tuna.repo
[local_tuna]
name = local tuna repo
baseurl = ftp://172.20.1.192/tuna_repo
enabled = 1
gpgcheck = 0

3)清除并缓存yum

[root@client ~]# yum clean all && yum makecache
Loaded plugins: fastestmirror
Cleaning repos: local_tuna
Cleaning up list of fastest mirrors
Other repos take up 83 M of disk space (use --verbose for details)
Loaded plugins: fastestmirror
Determining fastest mirrors
local_tuna                                              | 2.9 kB     00:00     
(1/3): local_tuna/filelists_db                            | 7.2 MB   00:00     
(2/3): local_tuna/primary_db                              | 6.1 MB   00:00     
(3/3): local_tuna/other_db                                | 2.6 MB   00:00     
Metadata Cache Created

测试yum源

[root@client ~]# yum -y install httpd &> /dev/null && echo $?
0

小李

学无止境,勇攀高峰。