自建yum仓库
2020-03-15 本文已影响0人
早_wsm
前言
节省时间就是抢救生命!在工作中我们总会去重复做一件事,比如下载,有些东西总是要用到要下载,受外界因素影响每次下载都会耗费大量时间,为节省时间何不在自己家建个小仓库,把之前用到下载过的东西都整理放好,用的时候直接拿过来就好!
一、创建yum仓库目录
mkdir /yum/
先把常用的rpm包和命令下载到我们刚刚创建的本地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包的下载路径
二、修改yum配置文件,打开本地缓存
[root@m01 ~]# cat /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=1 ###此处修改为1
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release
keepcache=1 #1为开启,0为不开启
三、安装createrepo,并生成索引文件
yum install createrepo -y
createrepo /yum/
四、提供yum服务
可以用Apache或nginx、python提供web服务,这里使用nginx
先安装nginx
yum install -y nginx
修改配置文件,使用http协议访问
vim /etc/nginx/conf.d/yum_ck.conf
server {
listen 8888;
server_name 10.0.0.61; #你的yum服务器地址
root /yum;
index index.html;
location / {
root /yum; #仓库目录
autoindex on;
}
}
配置文件写入完成使用nginx -t
检查语法,如果之前已启动过nginx 就重启nginx -s reload
,如果没有就启动nginx,systemctl start nginx
五、创建本地源
cd /etc/yum.repos.d
[root@m01]# vim /etc/yum.repos.d/yum-Media.repo
[yum_ck] #本地仓库名
name=yum_ck
baseurl=http://10.0.0.61:8888/ #http协议,rpm包存放路径
gpgcheck=0 #关闭安全验证
enabled=1 #启用本仓库
priority=1 #优先级为1,最高
清除yum缓存
yum clean all
六、优先级设置
因为我们服务器上还存在其他源,很难保证我们每次下载自动先从我们自建的yum仓库内下载,所以需要做一下优先级设置
- 1.下载一个第三方插件
yum install yum-plugin-priorities.noarch
- 2.检查是否启动服务(默认是开启的)
cat /etc/yum/pluginconf.d/priorities.conf
[main]
enabled = 1
- 3.修改源的优先级
在每个[模块]底部增加priority=2,这样就降低了他们的优先级,本地仓库在刚配置时就已设置为1了,所以,yum仓库优先于其他源
[root@m01 ~]# vim /etc/yum.repos.d/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
七、测试
每次新加入rpm包后都要对本地仓库进行更新
createrepo --update /yum/
查看软件包总数
yum repoinfo yum_ck | grep pkgs
把bc
命令加入到yum仓库中
yum install --downloadonly --downloaddir=/yum/ bc
再次查看软件包总数
yum repoinfo yum_ck | grep pkgs
无变化
更新本地仓库
createrepo --update /yum/
清除所有缓存
yum clean all
查看新的软件包总数
yum repoinfo yum_ck | grep pkgs
如果软件包的数量增加,说明仓库更新成功。
此时使用yum install -y bc
,下载后使用yum provides bc
可以看到:
[root@m01 ~]# yum provides bc
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
224 packages excluded due to repository priority protections
bc-1.06.95-13.el7.x86_64 : GNU's bc (a numeric processing language) and dc (a calculator)
Repo : yum_ck
bc-1.06.95-13.el7.x86_64 : GNU's bc (a numeric processing language) and dc (a calculator)
Repo : @yum_ck
至此,我们便获得了一个可爱的本地yum仓库,以后每次下载之前仓库内存在的镜像或者包都会优先在yum仓库内直接下载,大大提高了效率节省了时间