在不通外网,或没有内网yum源的情况下,怎么用yum装软件?
yum管理了rpm包的依赖关系。
我们用yum安装软件时,其实它是为我们下载了一堆rpm包,并按序安装。
所以,我们可以拷贝这一堆rpm包到其它一个没有网络的机器上,手工按序一个个安装它们,或者更方便的作法是再次借助yum管理这一堆rpm的依赖关系,帮我们自动安装。
一, 收集yum安装软件时所下载的rpm包
找一台可连接外网或者内网yum源的机器,用yum在线安装软件(以安装firefox为例)
修改/etc/yum.conf
以让yum安装完软件后不删除rpm包(keepcache=1
):
(默认情况下rpm会在安装完软件后自动删除所下载的rpm包)
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=1
...
删除yum以前缓存的文件(/var/cache/yum/$basearch/$releasever
):
(此举是为接下来我们要安装的软件提供一个干净的初始缓存)
[root@localhost 7]# pwd
/var/cache/yum/x86_64/7
[root@localhost 7]# ll
total 12
drwxr-xr-x 4 root root 256 Nov 28 16:12 base
drwxr-xr-x 4 root root 4096 Nov 28 11:12 epel
drwxr-xr-x 4 root root 161 Nov 28 11:12 extras
drwxr-xr-x 4 root root 137 Nov 28 11:12 mongodb-org-3.4
-rw-r--r-- 1 root root 129 Nov 28 11:16 timedhosts
-rw-r--r-- 1 root root 1673 Nov 27 14:27 timedhosts.txt
drwxr-xr-x 4 root root 161 Nov 28 11:12 updates
[root@localhost 7]# rm -rf *
通过yum安装firefox:
yum -y install firefox
安装完成后,到缓存目录里查找下载的所有rpm包:
[root@localhost 7]# cd /var/cache/yum/x86_64/7/
[root@localhost 7]# ll
total 12
drwxr-xr-x 7 root root 149 Nov 28 11:16 .
drwxr-xr-x 3 root root 15 Aug 16 2017 ..
drwxr-xr-x 4 root root 256 Nov 28 16:12 base
drwxr-xr-x 4 root root 4096 Nov 28 11:12 epel
drwxr-xr-x 4 root root 161 Nov 28 11:12 extras
-rw-r--r-- 1 root root 0 Aug 16 2017 .gpgkeyschecked.yum
drwxr-xr-x 4 root root 137 Nov 28 11:12 mongodb-org-3.4
-rw-r--r-- 1 root root 129 Nov 28 11:16 timedhosts
-rw-r--r-- 1 root root 1673 Nov 27 14:27 timedhosts.txt
drwxr-xr-x 4 root root 161 Nov 28 11:12 updates
[root@localhost 7]# find . -type f -name "*.rpm"
./base/packages/quagga-0.99.22.4-5.el7_4.x86_64.rpm
./base/packages/libXaw-1.0.13-4.el7.x86_64.rpm
./base/packages/libXcursor-1.1.14-8.el7.x86_64.rpm
./base/packages/libXfixes-5.0.3-1.el7.x86_64.rpm
./base/packages/libXft-2.3.2-2.el7.x86_64.rpm
./base/packages/libXmu-1.1.2-2.el7.x86_64.rpm
./base/packages/libXpm-3.5.12-1.el7.x86_64.rpm
./base/packages/libXt-1.1.5-3.el7.x86_64.rpm
./base/packages/libXxf86vm-1.1.4-1.el7.x86_64.rpm
./base/packages/libxkbfile-1.0.9-3.el7.x86_64.rpm
./base/packages/xorg-x11-apps-7.7-7.el7.x86_64.rpm
./base/packages/adwaita-cursor-theme-3.26.0-1.el7.noarch.rpm
./base/packages/adwaita-icon-theme-3.26.0-1.el7.noarch.rpm
./base/packages/at-spi2-atk-2.22.0-2.el7.x86_64.rpm
./base/packages/at-spi2-core-2.22.0-1.el7.x86_64.rpm
./base/packages/atk-2.22.0-3.el7.x86_64.rpm
./base/packages/cairo-1.14.8-2.el7.x86_64.rpm
./base/packages/cairo-gobject-1.14.8-2.el7.x86_64.rpm
./base/packages/centos-indexhtml-7-9.el7.centos.noarch.rpm
./base/packages/colord-libs-1.3.4-1.el7.x86_64.rpm
./base/packages/dbus-1.10.24-7.el7.x86_64.rpm
./base/packages/dbus-libs-1.10.24-7.el7.x86_64.rpm
./base/packages/gdk-pixbuf2-2.36.5-1.el7.x86_64.rpm
./base/packages/glib2-2.54.2-2.el7.x86_64.rpm
./base/packages/graphite2-1.3.10-1.el7_3.x86_64.rpm
./base/packages/gtk2-2.24.31-1.el7.x86_64.rpm
./base/packages/harfbuzz-1.3.2-1.el7.x86_64.rpm
./base/packages/hicolor-icon-theme-0.12-7.el7.noarch.rpm
./base/packages/jasper-libs-1.900.1-31.el7.x86_64.rpm
./base/packages/jbigkit-libs-2.0-11.el7.x86_64.rpm
./base/packages/json-glib-1.2.6-1.el7.x86_64.rpm
./base/packages/lcms2-2.6-3.el7.x86_64.rpm
./base/packages/libXdamage-1.1.4-4.1.el7.x86_64.rpm
./base/packages/libXinerama-1.1.3-2.1.el7.x86_64.rpm
./base/packages/libXrandr-1.5.1-2.el7.x86_64.rpm
./base/packages/libdrm-2.4.83-2.el7.x86_64.rpm
./base/packages/liberation-fonts-common-1.07.2-16.el7.noarch.rpm
./base/packages/liberation-sans-fonts-1.07.2-16.el7.noarch.rpm
./base/packages/libgusb-0.2.9-1.el7.x86_64.rpm
./base/packages/libthai-0.1.14-9.el7.x86_64.rpm
./base/packages/libtiff-4.0.3-27.el7_3.x86_64.rpm
./base/packages/libusbx-1.0.21-1.el7.x86_64.rpm
./base/packages/libwayland-client-1.14.0-2.el7.x86_64.rpm
./base/packages/libwayland-cursor-1.14.0-2.el7.x86_64.rpm
./base/packages/libwayland-server-1.14.0-2.el7.x86_64.rpm
./base/packages/libxkbcommon-0.7.1-1.el7.x86_64.rpm
./base/packages/libxshmfence-1.2-1.el7.x86_64.rpm
./base/packages/mesa-libEGL-17.2.3-8.20171019.el7.x86_64.rpm
./base/packages/mesa-libGL-17.2.3-8.20171019.el7.x86_64.rpm
./base/packages/mesa-libgbm-17.2.3-8.20171019.el7.x86_64.rpm
./base/packages/mesa-libglapi-17.2.3-8.20171019.el7.x86_64.rpm
./base/packages/mesa-libwayland-egl-17.2.3-8.20171019.el7.x86_64.rpm
./base/packages/mozilla-filesystem-1.9-11.el7.x86_64.rpm
./base/packages/nss-pem-1.0.3-4.el7.x86_64.rpm
./base/packages/pango-1.40.4-1.el7.x86_64.rpm
./base/packages/pixman-0.34.0-1.el7.x86_64.rpm
./base/packages/rest-0.8.0-2.el7.x86_64.rpm
./base/packages/startup-notification-0.12-8.el7.x86_64.rpm
./base/packages/xcb-util-0.4.0-2.el7.x86_64.rpm
./base/packages/xkeyboard-config-2.20-1.el7.noarch.rpm
./updates/packages/dconf-0.26.0-3.el7_5.1.x86_64.rpm
./updates/packages/firefox-60.3.0-1.el7.centos.x86_64.rpm
./updates/packages/gtk-update-icon-cache-3.22.26-4.el7_5.x86_64.rpm
./updates/packages/gtk3-3.22.26-4.el7_5.x86_64.rpm
./updates/packages/libepoxy-1.3.1-2.el7_5.x86_64.rpm
./updates/packages/nspr-4.19.0-1.el7_5.x86_64.rpm
./updates/packages/nss-3.36.0-7.el7_5.x86_64.rpm
./updates/packages/nss-softokn-3.36.0-5.el7_5.x86_64.rpm
./updates/packages/nss-softokn-freebl-3.36.0-5.el7_5.x86_64.rpm
./updates/packages/nss-sysinit-3.36.0-7.el7_5.x86_64.rpm
./updates/packages/nss-tools-3.36.0-7.el7_5.x86_64.rpm
./updates/packages/nss-util-3.36.0-1.el7_5.x86_64.rpm
[root@localhost 7]#
这些rpm包就是这台机器上安装firefox所需包。把它们打包压缩为如:firefox_rpms.tar.gz
。 今后我们只要有这个文件,就可以离线用yum安装firefox到其它配置相同的机器上了。
二, 在另外一台机器上安装
我们要做的是,把
firefox_rpms.tar.gz
拷到目标机上并解压到如/root/firefox_rpms/
的目录中,使用createrepo
命令把该目录创建成一个yum源,再将该源加入到CentOS-Media.repo
中,就可以用yum来从本地安装firefox了。
- 安装
createrepo
命令
如果机器上没有createrepo
命令,可以提前用yum在线安装(yum -y install createrepo
),或者下载如下几个rpm手工安装:
createrepo-0.9.9-28.el7.noarch.rpm
deltarpm-3.6-3.el7.x86_64.rpm
libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm
python-deltarpm-3.6-3.el7.x86_64.rpm
- 用
createrepo
命令创建本地yum源
[root@localhost firefox_rpms]# createrepo /root/firefox_rpms/
Spawning worker 0 with 17 pkgs
Spawning worker 1 with 17 pkgs
Spawning worker 2 with 16 pkgs
Spawning worker 3 with 16 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@localhost firefox_rpms]#
目录中会生成一个repodata
目录,其中有所有rpm文件信息,以及它们的依赖关系信息,这是作为一个yum源所需要的。
- 配置
CentOS-Media.repo
让yum从本地安装软件
备份/etc/yum.repos.d
目录的内容。删除其中除了CentOS-Media.repo
之外的所有文件。修改CentOS-Media.repo
内容如下:
[c7-media]
name=CentOS-$releasever - Media
baseurl=file:///root/firefox_rpms/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
其中baseurl=file:///root/firefox_rpms/
指定我们的目录。gpgcheck=0
表示取消gpg验证。enabled=1
表示启动此repo。
三, 从本地安装firefox
先清理yum缓存:yum clean all
,再安装firefox:
yum -y install firefox