yum 系列(二) 离线部署
自定义 yum 源 vi /etc/yum.repos.d/nexus.repo
[nexus]
name=nexus repository
baseUrl=http://192.168.139.101:8080/repository/163-yum/$releasever/os/$basearch
enabled=1
gpgcheck=0
一、下载 rpm 依赖包
(1) yum 下载 rpm 包
准备一台 全新的 CentOS7 mini 虚拟机 ,联网下载所有所需的 rpm 包和其依赖,
yum install -y --downloadonly --downloaddir=/home/yum/packages createrepo ansible ruby rubygems
# 也可以指定需要安装的 rpm 包,注意不需要 .rpm 后缀
yum install -y --downloadonly --downloaddir=/home/yum/packages yum-utils-1.1.31-45.el7.noarch
注意:
-
安装软件包 xxx 下载到 /xxx 位置,然后记得指定 --downloadonly,只下载,不安装。
-
此方法,会将主软件包和基于你现在的操作系统缺少的依赖关系包一并下载!
基于你现在的操作系统,意味着不同版本的操作系统(桌面版、mini版等)下载的软件依赖包个数会不一样!
你的操作系统已装的包越多,那么下下来的依赖会越少,所以请选择合适的基操作系统!
下载完成后,我们可以看到 /home/yum/packages 下有多个 rpm 包(主包及其依赖),接下来我们就利用这些 rpm 包搭建一个本地 yum 仓库。
(2) yum 设置自动保存下载的 rpm 包
yum 默认情况下,升级或者安装后,会删除下载的 rpm 包。不过,我们也可以如下设置升级后不删除下载的 rpm 包。
修改 /etc/yum.conf
[main]
cachedir=/home/yum/packages
keepcache=1
将 keepcache=0 修改为 keepcache=1, 安装或者升级后,在目录 /home/yum/packages 下就会有下载的 rpm 包了。
二、搭建本地 yum 仓库
(1) 拷贝 rpm 包
将拷贝下载的 rpm 包到离线目标机器(假设为 /home/yum/packages)
(2) createrepo 安装
# 先删除 deltarpm 包,注意删除 libxml2 后 yum 不可用,提示
# libxml2.so.2: cannot open shared object file: No such file or directory
# rpm -qa | grep libxml2 | xargs rpm -e --nodeps
rpm -qa | grep deltarpm| xargs rpm -e --nodeps
rpm -ivh /home/yum/packages/deltarpm-3.6-3.el7.x86_64.rpm
rpm -ivh /home/yum/packages/python-deltarpm-3.6-3.el7.x86_64.rpm
rpm -ivh /home/yum/packages/libxml2-2.9.1-6.el7_2.3.x86_64.rpm
rpm -ivh /home/yum/packages/libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm
rpm -ivh /home/yum/packages/createrepo-0.9.9-28.el7.noarch.rpm
(3) createrepo 生成 yum 仓库
createrepo /home/yum/packages # 生成 yum 仓库
createrepo --update /home/yum/packages # 更新 yum 仓库
(4) 添加 yum 配制文件
cat > /etc/yum.repos.d/yum.repo << EOF
[local]
# 名称描述
name=Local Repository
# yum 源目录,源地址,可以用 file://,ftp://,http:// 等
baseurl=file:///home/yum/packages
# 是否启用该 yum 源,0 为禁用
enabled=1
# 检查 GPG-KEY,0 为不检查,1 为检查
gpgcheck=0
EOF
注:cat > yum.repo 有则覆盖,无则生成;cat >> yum.repo 有则追加,无则生成。
(5) 更新 yum 仓库
yum clean all
yum repolist
yum makecache
完整的 shell 脚本:
# 1. 安装 createrepo
cd /home/yum/packages
# rpm -qa | grep libxml2 | xargs rpm -e --nodeps
rpm -qa | grep deltarpm | xargs rpm -e --nodeps
rpm -ivh /home/yum/packages/deltarpm-3.6-3.el7.x86_64.rpm
rpm -ivh /home/yum/packages/python-deltarpm-3.6-3.el7.x86_64.rpm
rpm -ivh /home/yum/packages/libxml2-2.9.1-6.el7_2.3.x86_64.rpm
rpm -ivh /home/yum/packages/libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm
rpm -ivh /home/yum/packages/createrepo-0.9.9-28.el7.noarch.rpm
# 2. 生成 yum 仓库
createrepo /home/yum/packages
# 3. 添加配制文件
cat > /etc/yum.repos.d/yum.repo << EOF
[local]
# 名称描述
name=Local Repository
# yum 源目录,源地址,可以用 file://,ftp://,http:// 等
baseurl=file:///home/yum/packages
# 是否启用该 yum 源,0 为禁用
enabled=1
# 检查 GPG-KEY,0 为不检查,1 为检查
gpgcheck=0
EOF
# 4. 更新 yum 源
mkdir -p /home/yum/yum.repos.d
mv /etc/yum.repos.d/Cent* /home/yum/yum.repos.d/
yum clean all
yum repolist
yum makecache
三、yum 默认源(/etc/yum.repos.d/CentOS-Base.repo)
(1) 默认源配置
[root@localhost packages]# cat /etc/yum.repos.d/CentOS-Base.repo
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
可以看到 yum 默认有 base、updates、extras 三个源。
(2) yum repolist
可以用 yum repolist 查看,其中 local 是我本地添加的源。
base/7/x86_64 CentOS-7 - Base 9,911
extras/7/x86_64 CentOS-7 - Extras 313
local Local Repository 37
updates/7/x86_64 CentOS-7 - Updates 683
(3) yum list
yum list | grep yum
结果如下:
[root@localhost packages]# yum list | grep yum
yum.noarch 3.4.3-150.el7.centos @anaconda
yum-metadata-parser.x86_64 1.1.4-10.el7 @anaconda
yum-plugin-fastestmirror.noarch 1.1.31-40.el7 @anaconda
PackageKit-yum.x86_64 1.1.5-2.el7.centos updates
PackageKit-yum-plugin.x86_64 1.1.5-2.el7.centos updates
centos-release-yum4.noarch 1-2.el7.centos extras
kabi-yum-plugins.noarch 1.0-3.el7.centos base
yum.noarch 3.4.3-158.el7.centos base
yum-NetworkManager-dispatcher.noarch 1.1.31-45.el7 base
yum-cron.noarch 3.4.3-158.el7.centos base
yum-langpacks.noarch 0.4.2-7.el7 base
yum-plugin-aliases.noarch 1.1.31-45.el7 base
第一列为 rpm 包名,第二列为 rpm 包版本,第三列为 rpm 包所在的源仓库,其中 @ 表示已安装。
每天用心记录一点点。内容也许不重要,但习惯很重要!