97、配置yum源仓库服务器

时间:2023-12-20 15:06:50

(服务端(双(外,内)网卡)——客户端(内网))

YUM主要用于自动安装、升级rpm软件包,它能自动查找并解决rpm包之间的依赖关系。要成功的使用YUM工具安装更新软件或系统,

就需要有一个包含各种rpm软件包的repository(软件仓库),这个软件仓库我们习惯称为yum源。网络上有大量的yum源,

但由于受到网络环境的限制,导致软件安装耗时过长甚至失败。特别是当有大量服务器大量软件包需要安装时,缓慢的进度条令人难以忍受。

因此我们在优化系统时,都会更换国内的源。相比较而言,本地YUM源服务器最大优点是局域网的快速网络连接和稳定性。有了局域网中的

YUM源服务器,即便在Internet连接中断的情况下,也不会影响其他YUM客户端的软件安装和升级。

97.1、YUM常用命令:

1、安装:

yum localinstall package1 #从本机安装包package1

yum install package1 #安装指定的安装包package1

yum groupinsall group1 #安装程序组group1

2、 更新和升级:

yum update/upgrade #更新操作系统和内核

yum update package1 #更新指定程序包package1,不删除旧的包

yum upgrade package1 #更新指定程序包package1,并删除旧的包

yum check-update #检查可更新的程序

yum grouplist #列出程序组

yum groupupdate group1 #升级程序组group1

3、查找和显示:

yum info package1 #显示安装包信息package1

yum list #显示所有已经安装和可以安装的程序包

yum list package1 #显示指定程序包安装情况package1

yum groupinfo group1 #显示程序组group1信息

yum search package1 #查找软件包package1;

yum list installed #列出已安装的软件包

yum list extras #列出不是通过软件仓库安装的软件包

yum list *ttp* #列出标题包含ttp的软件包的安装情况

yum list recent #列出新加入仓库的软件包

4、删除程序:

yum remove/erase package1 #删除程序包package1

yum groupremove group1 #删除程序组group1

yum deplist package1 #查看程序package1依赖情况

5、清除缓存:

yum clean packages #清除缓存目录下的软件包

yum clean headers #清除缓存目录下的 headers

yum clean all #清除缓存目录下的软件包及header

yum makecache #生成yum仓库缓存,生成元数据

97.2、创建yum仓库目录:

[root@nfs01 ~]# uname -r

2.6.32-573.el6.x86_64

[root@nfs01 ~]# cat /etc/redhat-release

CentOS release 6.7 (Final)

[root@nfs01 ~]# mkdir -p /application/yum/CentOS6.7/x86_64

[root@nfs01 ~]# cd /application/yum/CentOS6.7/x86_64/

[root@nfs01 x86_64]# rz -y #上传rpm包到此目录下,此目录下面还可以包括文件夹;

rz waiting to receive.

Starting zmodem transfer. Press Ctrl+C to cancel.

Transferring wordpress-4.5.1-zh_CN.tar.gz...

100% 7989 KB 7989 KB/sec 00:00:01 0 Errors

97.3、安装createrepo软件:

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo #更新yum源仓库,会自动更新epel源仓库;

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo #更新epel源仓库;

yum clean all #清理yum元数据和缓存rpm包;

[root@nfs01 x86_64]# yum install createrepo -y

[root@nfs01 x86_64]# rpm -qa createrepo

createrepo-0.9.9-28.el6.noarch

97.4、初始化repodata索引文件:

[root@nfs01 x86_64]# createrepo -pdo /application/yum/CentOS6.7/x86_64/ /application/yum/CentOS6.7/x86_64/

Saving Primary metadata

Saving file lists metadata

Saving other metadata

Generating sqlite DBs

Sqlite DBs complete

97.5、提供yum服务:

1、用python的http模块更简单,但是太卡了网页打开速度非常慢,弃用:

[root@nfs01 x86_64]# pwd

/application/yum/CentOS6.7/x86_64 #使用python的http服务必须要在当前的目录中执行下面的命令;

[root@nfs01 x86_64]# python -m SimpleHTTPServer 80 &>/dev/null &

[1] 1640

2、使用apache提供web服务:

[root@nfs01 x86_64]# yum install httpd -y

[root@nfs01 x86_64]# rpm -qa httpd

httpd-2.2.15-69.el6.centos.x86_64

[root@nfs01 x86_64]# chkconfig --list httpd

httpd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭

[root@nfs01 x86_64]# chkconfig httpd on

[root@nfs01 x86_64]# chkconfig --list httpd

httpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

[root@nfs01 x86_64]# vim /etc/httpd/conf/httpd.conf

276 ServerName 0127.0.0.1:8

292 DocumentRoot "/application/yum/CentOS6.7/x86_64/"

317 <Directory "/application/yum/CentOS6.7/x86_64/">

#删除欢迎文件,其实是无首页的显示的404内容,目的是为了输入ip就显示列表;

[root@nfs01 x86_64]# rm -vf /etc/httpd/conf.d/welcome.conf

已删除"/etc/httpd/conf.d/welcome.conf"

[root@nfs01 x86_64]# /etc/init.d/httpd start

正在启动 httpd: [确定]

在浏览器中查看:

97、配置yum源仓库服务器

97.6、添加新的rpm包:

1、yum下载rpm包到本地,不安装:

[root@nfs01 x86_64]# yumdownloader pcre-devel openssl-devel

已加载插件:fastestmirror

Loading mirror speeds from cached hostfile

* base: mirrors.aliyun.com

* extras: mirrors.cn99.com

* updates: mirrors.cn99.com

pcre-devel-7.8-7.el6.x86_64.rpm | 320 kB 00:00

pcre-devel-7.8-7.el6.i686.rpm | 320 kB 00:00

openssl-devel-1.0.1e-57.el6.x86_64.rpm | 1.2 MB 00:00

openssl-devel-1.0.1e-57.el6.i686.rpm

2、更新yum仓库repodata目录下的索引文件:

[root@nfs01 x86_64]# createrepo --update /application/yum/CentOS6.7/x86_64/

Spawning worker 0 with 4 pkgs

Workers Finished

Gathering worker results

Saving Primary metadata

Saving file lists metadata

Saving other metadata

Generating sqlite DBs

Sqlite DBs complete

每次上传新的rpm后都要执行更新命令;

3、配置yum安装缓存rpm功能:

[root@nfs01 x86_64]# vim /etc/yum.conf

[main]

#cachedir=/var/cache/yum/$basearch/$releasever

cachedir=/application/yum/CentOS6.7/x86_64/ #yum下载的rpm包的缓存目录;

keepcache=1 #rpm包缓存,默认不缓存为0;

debuglevel=2 #信息输出等级,范围是0-10,缺省值为2;

logfile=/var/log/yum.log #yum安装日志文件;

exactarch=1 #yum只会安装和系统架构匹配的软件包,默认是1;

obsoletes=1 #update参数,允许更新旧的rpm包;

gpgcheck=1 #检查安装rpm包的安全性;

plugins=1 #启用插件,默认是0不启用;

installonly_limit=5 #保留多少个内核包;

bugtracker_url=http://bugs.centos.org/set_project.php?project_id=19&ref=http://bugs.centos.org/bug_report_page.php?category=yum

distroverpkg=centos-release #根据软件包名判断rpm包的发行版本,默认是centos-release;

备注:如果以后更改网上的yum源时不能使用clean all 清除之前yum源留下的缓存,这样会把自己缓存的rpm包给删掉,最好是使用yum clean metadata命令

清除缓存的yum源信息;

96.8、客户端配置:

[root@backup x86_64]# cd /etc/yum.repos.d/

1、yum源临时生效配置:

(1)配置文件配置:

[root@backup yum.repos.d]# vim CentOS-6.7-x86_64.repo

[CentOS-6.7-x86_64]

name=CentOS-6.7

#url 支持的协议有 http://、 ftp:// 、file:// 三种。baseurl 后可以跟多个url,但baseurl 只能有一个。

baseurl=http://172.16.1.31

enabled=1 #启动此yum源,默认是1;

gpgcheck=0 #关闭检查rpm的安全性,默认是1开启,如果开启还需要在下面配置一行参数为gpgkey=http://192.16.1.31/RPM-GPG-KEY-CentOS-6

(2)清除以前的yum源缓存:

[root@backup yum.repos.d]# yum clean all #清理yum元数据和缓存rpm包

已加载插件:fastestmirror, security

Repository CentOS-6.7-x86_64 is listed more than once in the configuration

Cleaning repos: CentOS-6.7-x86_64 epel

清理一切

Cleaning up list of fastest mirrors

(3)测试:列出仓库中当前系统没有安装的软件;

[root@backup yum.repos.d]# yum --enablerepo=CentOS-6.7-x86_64 --disablerepo=base,extras,updates,epel list available

已加载插件:fastestmirror, security

Repository base is listed more than once in the configuration

Repository updates is listed more than once in the configuration

Repository extras is listed more than once in the configuration

Repository centosplus is listed more than once in the configuration

Repository contrib is listed more than once in the configuration

Loading mirror speeds from cached hostfile

可安装的软件包

openssl-devel.i686 1.0.1e-57.el6 CentOS-6.7-x86_64

openssl-devel.x86_64 1.0.1e-57.el6 CentOS-6.7-x86_64

pcre-devel.i686 7.8-7.el6 CentOS-6.7-x86_64

pcre-devel.x86_64 7.8-7.el6 CentOS-6.7-x86_64

2、yum源永久生效配置:

(1)配置文件配置:

[root@backup yum.repos.d]# vim Centos-Base.repo

[base]

…………

enabled=0

[updates]

…………

enabled=0

[extras]

…………

enabled=0

[CentOS-6.7-x86_64]

name=CentOS-6.7

baseurl=http://172.16.1.31

enabled=1

gpgcheck=0

[root@nfs01 yum.repos.d]# vim epel.repo

[epel]

enabled=0

(2)清除以前的yum源缓存:

[root@backup yum.repos.d]# yum clean all #清理yum元数据和缓存rpm包

已加载插件:fastestmirror, security

Repository CentOS-6.7-x86_64 is listed more than once in the configuration

Cleaning repos: CentOS-6.7-x86_64

清理一切

(3)测试:

[root@backup yum.repos.d]# yum install pcre-devel -y

已加载插件:fastestmirror, security

设置安装进程

Repository CentOS-6.7-x86_64 is listed more than once in the configuration

Loading mirror speeds from cached hostfile

解决依赖关系

--> 执行事务检查

---> Package pcre-devel.x86_64 0:7.8-7.el6 will be 安装

--> 完成依赖关系计算

96.9、镜像同步公网yum源配置:

服务端和客户端都可上网或服务端是双网卡(连接内外网),客户端是内网服务;

上面只是将自己制作的rpm包放入yum源,但还有一种企业需求,说的更具体一点,平时学生上课yum安装软件都是从公网下载的,占用带宽,

因此在学校里搭建一个内网yum服务器,但又考虑到学生回家也要使用yum安装软件,如果yum软件的数据库文件repodata不一样,就会有问题,

因此解决方法就是直接使用公网yum源的repodata。

1、安装httpd服务,使httpd网站根目录指向/data/yum_data/:

mkdir /data/yum_data/ -p

2、创建相应的yum源目录:

(1)创建base源目录:

mkdir -p /data/yum_data/centos/6/os/x86_64/

(2)创建extras源目录:

mkdir /data/yum_data/centos/6/extras/x86_64/

(3)创建updates源目录:

mkdir /data/yum_data/centos/6/updates/x86_64/

(4)创建epel源目录:

mkdir /data/yum_data/epel/6/x86_64/

3、同步yum源和epel源:

上游yum源必须要支持rsync协议,否则不能使用rsync进行同步,这里同步的是中科大的yum、epel源;

(1)同步base源:

/usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/os/x86_64/ /data/yum_data/centos/6/os/x86_64/

(2)同步extras源:

/usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/extras/x86_64/ /data/yum_data/centos/6/extras/x86_64/

(3)同步updates源:

/usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/updates/x86_64/ /data/yum_data/centos/6/updates/x86_64/

(4)同步epel源:

/usr/bin/rsync -av --exclude=debug rsync://mirrors.ustc.edu.cn/epel/6/x86_64/ /data/yum_data/epel/6/x86_64/

4、客户端配置:

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo #更新yum源仓库;

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo #更新epel源仓库;

yum clean all #清理yum元数据和缓存rpm包;

如果有条件可以搭建一个dns服务器,如果没有可以可以使用hosts解析,echo '192.16.1.31 mirrors.aliyun.com' >>/etc/hosts

5、补充:

上面使用的是CentOS-6的源,如果同步完大约是21G左右,如果使用的是CentOS-7的源,则修改相应的目录就可以可了;

96.10、补充:

centos的镜像中自带了很多的rpm包,大约有3G左右,在机器不联网的情况下可以配置本地yum源,方便进行yum方式软件的安装,解决软件包之间的依赖关系:

1、挂载镜像:

[root@backup ~]# mount /dev/cdrom /mnt/

mount: block device /dev/sr0 is write-protected, mounting read-only

[root@backup ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda2 57G 1.9G 53G 4% /

tmpfs 296M 12K 296M 1% /dev/shm

/dev/sda1 190M 67M 114M 37% /boot

/dev/sr0 3.7G 3.7G 0 100% /mnt

2、yum源文件参数配置:

[root@backup yum.repos.d]# vim Centos-Base.repo

[base]

…………

enabled=0

[updates]

…………

enabled=0

[extras]

…………

enabled=0

[CentOS-6.7-x86_64]

name=CentOS-6.7

baseurl=file:///mnt/

enabled=1

gpgcheck=0

[root@nfs01 yum.repos.d]# vim epel.repo

[epel]

enabled=0

3、清除以前的yum源缓存:

[root@backup yum.repos.d]# yum clean all #清理yum元数据和缓存rpm包

已加载插件:fastestmirror, security

Repository base is listed more than once in the configuration

Repository updates is listed more than once in the configuration

Repository extras is listed more than once in the configuration

Repository centosplus is listed more than once in the configuration

Repository contrib is listed more than once in the configuration

Cleaning repos: CentOS-6.7-x86_64

清理一切

4、测试:

[root@backup yum.repos.d]# yum install zlib-devel

已加载插件:fastestmirror, security

设置安装进程

Repository base is listed more than once in the configuration

Repository updates is listed more than once in the configuration

Repository extras is listed more than once in the configuration

Repository centosplus is listed more than once in the configuration

Repository contrib is listed more than once in the configuration

Loading mirror speeds from cached hostfile

解决依赖关系

--> 执行事务检查

---> Package zlib-devel.x86_64 0:1.2.3-29.el6 will be 安装

--> 完成依赖关系计算

96.11、常用rpm包地址:

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo #阿里云yum源(yum=Yellow dog Updater, Modified=更新修改程序);

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo #阿里云epel源 (epel=Extra Packages for Enterprise Linux=企业Linux的额外包)

https://mirrors.aliyun.com/centos/6/os/i386/Packages/ #阿里centos6 base rpm包 32位

https://mirrors.aliyun.com/centos/6/os/x86_64/Packages/ #阿里centos6 base rpm包 64位

https://www.centos.org/download/ #centos官方镜像下载 alternative downloads ;

96.12、总结:

1、yum源仓库添加新的rpm包时要更新repodata索引,客户端要清理以前的yum源缓存;

2、客户端更新yum源仓库地址时也要清除以前的yum源缓存;

3、要合理的使用yum clean all 和yum clean metadata 命令,不然会导致rpm包的丢失;