如何在 RHEL Linux 中使用 YUM/DNF 禁用软件包更新
DNF(Dandified Yum)是 YUM(Yellowdog Updater,Modified)的下一代版本,是基于 Red Hat 的 Linux 发行版的开源默认包管理器,用于获取、安装、升级、删除、以及从官方软件仓库和第三方仓库查询软件包。
在更新系统时,有时我们不会更新某些软件包,例如 Apache Server (HTTP)、MySQL、PHP、 或任何其他主要应用程序,因为更新此类软件可能会破坏服务器上当前运行的 Web 应用程序并导致重大问题。建议停止此类软件的更新,直到应用程序获得新的更新补丁。
在本文中,我们将向您展示如何在基于 RPM 的发行版(例如 RHEL)上使用 YUM 和 DNF 软件包管理器排除(禁用)某些软件包更新、CentOS、Fedora、Rocky Linux 和 AlmaLinux。我们还可以从任何第三方存储库中排除或禁用某些软件包更新。
排除语法如下。
exclude=package package1 packages*
上面的 exclude 指令是在 /etc/yum.conf 或 /etc/dnf/dnf.conf 配置文件中定义的,其中包含软件包列表从更新或安装中排除。
上述语法将排除“package”、“package1”以及“package”更新或安装列表。每个关键字应该用空格分隔,以排除包。
如何在 YUM 或 DNF 中排除软件包
要排除(禁用)特定软件包更新,请使用您选择的编辑器打开名为 /etc/yum.conf 或 /etc/dnf/dnf.conf 的文件。
vi /etc/yum.conf
OR
vi /etc/dnf/dnf.conf
在文件底部添加以下行以及 exclude 关键字,如下所示。
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
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=16&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release
This is the default, if you make this bigger yum won't see if the metadata
is newer on the remote and so you'll "gain" the bandwidth of not having to
download the new metadata and "pay" for it by yum not having correct
information.
It is esp. important, to have correct metadata, for distributions like
Fedora which don't keep old packages around. If you don't like this checking
interupting your command line usage, it's much better to have something
manually check the metadata once an hour (yum-updatesd will do this).
metadata_expire=90m
PUT YOUR REPOS HERE OR IN separate files named file.repo
in /etc/yum.repos.d
## Exclude following Packages Updates ##
exclude=httpd php mysql
在上面的示例中,行排除将禁用“httpd”、“php”和“mysql”的更新包。让我们尝试使用 YUM 命令安装或更新其中一个,如下所示。
yum update httpd
OR
dnf update httpd
样本输出
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.01link.hk
* extras: centos.01link.hk
* updates: mirrors.hns.net.in
base | 3.7 kB 00:00
extras | 3.0 kB 00:00
updates | 3.5 kB 00:00
updates/primary_db | 2.7 MB 00:16
Setting up Update Process
No Packages marked for Update
如何从 EPEL Repo 中排除软件包
要从 EPEL 存储库中排除软件包安装或更新,请打开名为 /etc/yum.repos.d/epel.repo 的文件。
vi /etc/yum.repos.d/epel.repo
通过指定要从更新中排除的包来添加排除行。
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
## Exclude following Packages Updates ##
exclude=perl php python
现在尝试使用 yum/dnf 命令从 EPEL 存储库更新上述指定的文件,如图所示。
dnf update perl php python
OR
yum update perl php python
样本输出
Last metadata expiration check: 0:00:37 ago on Wednesday 17 November 2021 03:41:28 AM EST.
Package perl available, but not installed.
No match for argument: perl
No match for argument: php
No match for argument: python
Error: No packages marked for upgrade.
您还可以使用 yum/dnf 命令行选项来排除软件包,而不将它们添加到存储库文件中。
yum --exclude=httpd update
Or
dnf --exclude=httpd update
要排除软件包列表,请使用以下命令。
yum --exclude=mysql\* --exclude=httpd\* update
Or
dnf --exclude=mysql\* --exclude=httpd\* update
这样您就可以排除任何您想要的软件包的更新。还有很多其他方法可以做到这一点,例如,最近我们编写了一篇文章,介绍了在 Linux 中使用 yum 命令阻止/禁用或锁定某些软件包的 4 种有用方法。