搭建本地yum仓库

方法一:

第一步:光盘挂载

[root@m01 ~]# mount /dev/cdrom  /mnt
mount: /dev/sr0 is write-protected, mounting read-only

第二步:编写文件

[root@m01 ~]# vim /etc/yum.repos.d/local_yum
[local_yum]
name=local yum
baseurl=file:///mnt
gpgcheck=0 #因为是光盘安装,不需要检查
enabled=1
priority=1    #优先级越低越优先

第三步:清理缓存和安装优先级

[root@m01 ~]# yum install yum-plugin-priorities.noarch   #安装优先级
[root@m01 ~]# yum clean all       #清理缓存:
[root@m01 ~]# yum repolist        #查看仓库立有多少安装包

其它设备如果想要使用,可以结合NFS使用。

第四步:如果有新的rpm包,放入并更新。

[root@m01 ~]# createrepo --update /application/yum/centos7/x86_64/

方法二:

第一步:修改yum配置文件

[root@m01 ~]# sed -i 's#keepcache=0#keepcache=1#g' /etc/yum.conf 

第二步:关闭防火墙和SELINUX

[root@m01 ~]# getenforce 
Disabled
[root@m01 ~]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

第三步:下载createrepo组件包

[root@m01 ~]# yum install -y createrepo
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                                                             | 3.6 kB  00:00:00     
epel                                                                                                                                             | 4.7 kB  00:00:00     
extras                                                                                                                                           | 3.4 kB  00:00:00     
updates                                                                                                                                          | 3.4 kB  00:00:00     
Package createrepo-0.9.9-28.el7.noarch already installed and latest version
Nothing to do

第四步:备份rpm包并更新元数据

[root@m01 ~]# find / -type f -name "*.rpm" | xargs -i cp {} /usr/share/nginx/html/yum/centos7/x86_64/
[root@m01 ~]# createrepo /usr/share/nginx/html/yum/centos7/x86_64/ #类似数据库建库建表一样,或者理解成创建inode
Spawning worker 0 with 508 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

第五步:搭建Apache或Nginx服务

 
部署配置官方nginx源
/etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

 

本例中采用的yum安装的nginx,修改相关配置文件如下

server {
  listen  80;
  server_name 10.0.0.61;
  location /  {
      root /usr/share/nginx/html/yum/centos7/x86_64;
      autoindex  on;
  }
}

修改完成后重启服务

或在要共享的目录下执行
[root@oldboy yum]# python -m SimpleHTTPServer 80 &> /dev/null &
[1] 8738
[root@oldboy yum]# lsof -i:80
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python  8725 root    3u  IPv4  57388      0t0  TCP *:http (LISTEN)
[root@oldboy yum]# curl 10.0.0.61
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
<title>Directory listing for /</title>
<body>
<h2>Directory listing for /</h2>
<hr>
<ul>
<li><a href="x86_64/">x86_64/</a>
</ul>
<hr>
</body>
</html>

第六步:修改其它设备的yum源

[root@m01 ~]# cat local_yum
[local]
name=local yum
baseurl=http://10.0.0.61  
gpgcheck=0
enabled=1

第七步:如果有新的rpm包,放入共享目录中并更新元数据。

[root@m01 ~]# createrepo --update /application/yum/centos7/x86_64/



1.搭建yum仓库,准备需要的软件
mlocate lrzsz tree vim nc nmap lrzsz wget bash-completion bash-completion-extras cowsay sl htop iotop iftop lsof net-tools sysstat unzip bc psmisc ntpdate wc telnet-server bind-utils createrepo

gcc gcc-c++ autoconf pcre pcre-devel make automake httpd-tools
还有架构中要用到的软件与依赖环境也要下载放到仓库中

服务端—m01管理机

提前下载好nginx

yum install nginx -y

把rpm包及其相关依赖全部都下载到本地,保存好。
手动在/etc/yum.repos.d/目录下配置本地仓库信息。
使用createrepo命令生成repodata信息。
使用yum repoinfo检查确认。

yum仓库本地存放rpm包的路径:/yum/

创建一个本地仓库的目录
mkdir /yum/

yum install  --downloadonly --downloaddir=/yum/ mlocate lrzsz tree vim nc nmap lrzsz wget bash-completion bash-completion-extras cowsay sl htop iotop iftop lsof net-tools sysstat unzip bc psmisc ntpdate wc telnet-server bind-utils gcc gcc-c++ autoconf pcre pcre-devel make automake httpd-tools 

只下载不安装的命令:

--downloadonly 只下载不安装
--downloaddir 指定rpm包的下载路径

1.修改yum配置文件,打开本地缓存,1为开启,0为不开启

这里要把keepcache打开
从外网下载的rpm包可以到缓存目录/var/cache/yum/下查找,可以将本地缓存下面需要的包都移动到yum仓库中。

[root@m01 /etc/yum.repos.d]# cat /etc/yum.conf 
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=1
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
...

2.安装createrepo

#创建一个本地仓库的目录
mkdir /yum/

#安装
yum install createrepo -y

#生成索引文件
createrepo /yum/

3.每次下载了新的rpm包需要执行此命令

createrepo --update /yum/

--update 如果元数据已经存在,且软件仓库中只有部分软件发生了改变或增减,则可用update参数直接对原有元数据进行升级,效率比重新分析rpm包依赖并生成新的元数据要高很多。

4.通过http协议访问

创建一个本地源

[root@m01]# vim /etc/yum.repos.d/yum-Media.repo
[yum_ck] #本地仓库名
name=CentOS- - 仓库 #注释信息
baseurl=http://10.0.0.61:8888/  #http协议,rpm包存放路径
gpgcheck=0  #关闭安全验证
enabled=1   #启用本仓库
priority=1  #优先级为1,最高

通过http协议访问

配置.conf文件
不要与ngin.conf文件冲突

vim /etc/nginx/conf.d/yum_ck.conf

server {
    listen 8888;
    server_name 10.0.0.61;
    root /yum;
    index index.html;
    location / {
      root /yum;
      autoindex on;
    }
}

重启nginx

nginx -t

systemctl start nginx

5.清除yum缓存

yum clean all

6.下载一款软件放到/yum/下测试

yum install  --downloadonly --downloaddir=/yum nfs-utils

# 执行此命令检查是否是从本地源找的包
yum provides nfs-utils

#显示结果为本地仓库名代表成功
Repo        : @yum_ck

7.优先级设置

1.下载第三方插件

yum install yum-plugin-priorities.noarch

2.检查是否启动服务(默认是开启的)

cat /etc/yum/pluginconf.d/priorities.conf
[main]
enabled = 1

3.修改源的优先级(新建的本地源在创建时已添加优先级为1)
vim /etc/yum.repos.d/CentOS-Base.repo
代码中已注释“#添加优先级为2”

[root@m01 /etc/yum.repos.d]# vim CentOS-Base.repo
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
priority=2  #添加优先级为2
#released updates 
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
priority=2  #添加优先级为2
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
priority=2  #添加优先级为2
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
priority=2  #添加优先级为2
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
priority=2  #添加优先级为2

8.对本地仓库进行更新

下载一个新的rpm软件包到本地仓库,此时我们使用’yum repoinfo yum_ck ‘查看会发现软件包的数量并没有增加,我们安装新增的软件包也会提示,找不到此软件包的现象,可以按照下述步骤,更新仓库信息。

yum_ck === 本地仓库的名称


查看旧的软件包总数 yum repoinfo yum_ck | grep pkgs
更新本地仓库 createrepo --update /yum/
清除所有缓存 yum clean all
查看新的软件包总数 yum repoinfo yum_ck | grep pkgs
如果软件包的数量增加,说明仓库更新成功。

9.将配置推送到所有的客户端

yum -y install yum-plugin-priorities.noarch

/etc/yum.repos/yum-Media.repo

/etc/yum.repos.d/CentOS-Base.repo

用ansible剧本实现,在本地配置好

这里的/etc/ansible/file路径是根据自己的环境创建的,只是为了推送这个本地源的配置文件,推送到客户端的路径不要写错就好。

[root@m01 /etc/ansible]# cat /etc/ansible/yum_ck.yml 
#本地yum仓库搭建
---
  - hosts: all
    tasks:

    - name: cp yum-Media.repo
      copy: 
        src: /etc/ansible/file/yum-Media.repo
        dest: /etc/yum.repos.d/
        force: yes

    - name: copy .repo
      copy:
        src: /etc/ansible/file/CentOS-Base.repo
        dest: /etc/yum.repos.d/CentOS-Base.repo
        force: yes

    - name: yum-plugin-priorities.noarch
      yum:
        name: yum-plugin-priorities.noarch
        state: present  


作者:Linux丶晨星
链接:https://www.jianshu.com/p/172e2a76d024
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @ 2019-11-06 23:34  xiaoxiaoren520  阅读(514)  评论(0编辑  收藏  举报