[提交译文][tech]:0210617 Linux package management with dnf.md

This commit is contained in:
hanszhao80 2022-05-03 23:16:57 +08:00
parent c623b42d7c
commit ba442f6140
2 changed files with 181 additions and 182 deletions

View File

@ -1,182 +0,0 @@
[#]: subject: (Linux package management with dnf)
[#]: via: (https://opensource.com/article/21/6/dnf-linux)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
[#]: collector: (lujun9972)
[#]: translator: (hanszhao80)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
Linux package management with dnf
======
Learn how to install packages on Linux with the dnf command, then
download our cheat sheet to keep the right command at your fingertips.
![Package wrapped with brown paper and red bow][1]
Installing an application on a computer system is pretty simple. You copy files from an archive (like a .zip file) onto the target computer in a place the operating system expects there to be applications. Because many of us are accustomed to having fancy installer "wizards" to help us get software on our computers, the process seems like it should be technically more complex than it is.
What _is_ complex, though, is the issue of what makes up an application. What users think of as a single application actually contains code borrowing from software libraries (i.e., `.so` files on Linux, `.dll` files on Windows, and `.dylib` on macOS) scattered throughout an operating system.
So that users don't have to worry about that veritable matrix of interdependent code, Linux uses a **package management system** to track what application needs what library, and which library or application has security or feature updates, and what extra data files were installed with each software title. A package manager is, essentially, an installer wizard. They're easy to use, they provide both graphical interfaces and terminal-based interfaces, and they make your life easier. The better you know your distribution's package manager, the easier your life gets.
### Installing applications on Linux
If you're a casual desktop user who wants to install an application on Linux, then you may be looking for [GNOME Software][2], a desktop application browser.
![Image of the GNOME Software application][3]
It works as you'd expect: You click through its interface until you find an application that seems like it would be useful, and then you click the **Install** button.
Alternately, you can open `.rpm` or `.flatpakref` packages downloaded from the web in GNOME Software for it to install them for you.
If you're inclined toward controlling your computer with typed commands, read on!
### Finding software with dnf
Before you can install an application, you may need to confirm that it exists on your distribution's servers. Usually, searching for the common name of an application with `dnf` suffices. For instance, say you recently read [an article about Cockpit][4] and decide you want to try it. You could search for `cockpit` to verify that your distribution includes it:
```
$ dnf search cockpit
 Last metadata expiration check: 0:01:46 ago on Tue 18 May 2021 19:18:15 NZST.
 ==== Name Exactly Matched: cockpit ====
 cockpit.x86_64 : Web Console for Linux servers
==== Name & Summary Matched: cockpit ==
 cockpit-bridge.x86_64 : Cockpit bridge server-side component
 cockpit-composer.noarch : Composer GUI for use with Cockpit
 [...]
```
There's an exact match. The package listed as a match is called `cockpit.x86_64`, but the `.x86_64` part of the name only denotes the CPU architecture it's compatible with. By default, your system installs packages with matching CPU architectures, so you can ignore that extension. Therefore, you've confirmed that the package you're looking for is indeed called simply `cockpit`.
Now you can confidently install it with `dnf install`. This step requires administrative privileges:
```
`$ sudo dnf install cockpit`
```
More often than not, that's the typical `dnf` workflow: search and install.
Sometimes, however, the results of `dnf search` aren't clear to you, or you want more information about a package than just its common name. There are a few relevant `dnf` subcommands, depending on what information you're after.
### Package metadata
If you feel like your search got you _close_ to the package you want, but you're just not sure yet, it's often helpful to take a look at the package's metadata, such as the project's URL and description. To get this info, use the pleasantly intuitive `dnf info` command:
```
$ dnf info terminator
Available Packages
Name         : terminator
Version      : 1.92
Release      : 2.el8
Architecture : noarch
Size         : 526 k
Source       : terminator-1.92-2.el8.src.rpm
Repository   : epel
Summary      : Store and run multiple GNOME terminals in one window
URL          : <https://github.com/gnome-terminator>
License      : GPLv2
Description  : Multiple GNOME terminals in one window.  This is a project to produce
             : an efficient way of filling a large area of screen space with
             : terminals. This is done by splitting the window into a resizeable
             : grid of terminals. As such, you can  produce a very flexible
             : arrangements of terminals for different tasks.
```
This info dump tells you the version of the available package, which repository registered with your system provides it, the project's website, and a long description of what it does.
### What package provides a file?
Package names don't always match what you're looking for. For instance, suppose you're reading documentation telling you that you must install something called `qmake-qt5`:
```
$ dnf search qmake-qt5
No matches found.
```
The `dnf` database is extensive, so you don't have to restrict yourself to searches for exact matches. You can use the `dnf provides` command to learn whether anything provides what you're looking for as part of some larger package:
```
$ dnf provides qmake-qt5
qt5-qtbase-devel-5.12.5-8.el8.i686 : Development files for qt5-qtbase
Repo        : appstream
Matched from:
Filename    : /usr/bin/qmake-qt5
qt5-qtbase-devel-5.15.2-3.el8.x86_64 : Development files for qt5-qtbase
Repo        : appstream
Matched from:
Filename    : /usr/bin/qmake-qt5
```
This confirms that the application `qmake-qt5` is a part of a package named `qt5-qtbase-devel`. It also tells you that the application gets installed to `/usr/bin`, so you know exactly where to find it once it's installed.
### What files are included in a package?
There are times when I find myself approaching `dnf` from a different angle entirely. Sometimes, I've already confirmed that an application is installed on my system; I just can't figure out how I got it. Other times, I know I have a specific package installed, but I'm not clear on exactly what that package put on my system.
If you ever need to "reverse engineer" a package's payload, you can use the `dnf repoquery` command along with the `--list` option. This looks at the repository's metadata about a package and returns a list of all files provided by that package:
```
$ dnf repoquery --list qt5-qtbase-devel
/usr/bin/fixqt4headers.pl
/usr/bin/moc-qt5
/usr/bin/qdbuscpp2xml-qt5
/usr/bin/qdbusxml2cpp-qt5
/usr/bin/qlalr
/usr/bin/qmake-qt5
/usr/bin/qvkgen
/usr/bin/rcc-qt5
[...]
```
These lists can get long, so it helps to pipe the command through `less` or your favorite pager.
### Removing an application
Should you decide you no longer need an application installed on your system, you can use `dnf remove` to uninstall it, all of the files that were installed as part of its package, and any dependencies that are no longer necessary:
```
`$ dnf remove bigapp`
```
Sometimes, dependencies get installed with one app and are later found useful by some other application you install. In the event that two packages require the same dependency, `dnf remove` does _not_ remove the dependency. It's not unheard of to end up with a stray package here and there after installing and uninstalling lots of applications. About once a year, I perform a `dnf autoremove` to clear out any unused packages:
```
`$ dnf autoremove`
```
This isn't necessary, but it's a housecleaning step that makes me feel better about my computer.
### Getting to know dnf
The more you know about how your package manager works, the easier it is for you to install and query applications when necessary. Even if you're not a regular `dnf` user, it can be useful to know it when you find yourself interfacing with an RPM-based distro.
Having graduated from `yum`, one of my favorite package managers is the `dnf` command. While I don't love all its subcommands, I find it to be one of the more robust package management systems out there. [**Download our `dnf` cheat sheet**][5] to get used to the command, and don't be afraid to try some new tricks with it. Once you get familiar with it, you might find it hard to use anything else.
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/6/dnf-linux
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[hanszhao](https://github.com/hanszhao)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/brown-package-red-bow.jpg?itok=oxZYQzH- (Package wrapped with brown paper and red bow)
[2]: https://wiki.gnome.org/Apps/Software
[3]: https://opensource.com/sites/default/files/gnome-software.png (The GNOME Software app)
[4]: https://opensource.com/article/20/11/cockpit-server-management
[5]: https://opensource.com/downloads/dnf-cheat-sheet

View File

@ -0,0 +1,181 @@
[#]: subject: (Linux package management with dnf)
[#]: via: (https://opensource.com/article/21/6/dnf-linux)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
[#]: collector: (lujun9972)
[#]: translator: (hanszhao80)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
使用 dnf 进行 Linux 包管理
======
了解如何在 Linux 上使用 `dnf` 命令安装软件包,然后下载我们的速查表,让正确的命令触手可及。
![Package wrapped with brown paper and red bow][1]
在计算机系统上安装应用程序非常简单。你将档案(如 `.zip` 文件)中的文件复制到目标计算机上操作系统想要安装的位置。因为我们中的许多人习惯于使用精美的安装程序“向导”来帮助我们在计算机上安装软件,所以这个过程似乎在技术上应该比实际更复杂。
然而程序的构成是个复杂的问题。用户认为的单个应用程序实际上包含了分散在操作系统中的软件库的各种依赖代码例如Linux 上的 .so 文件、Windows 上的 .dll 文件和 macOS 上的 .dylib 文件)。
用户不必担心程序代码之间的复杂的依赖关系,因为 Linux 使用 **<ruby>包管理系统<rt>package management system</rt></ruby>** 来跟踪哪些应用程序需要哪些库,哪些库或应用程序有安全或功能更新,以及每个软件会附带安装哪些额外的数据文件。包管理器本质上是一个安装向导。它们易于使用,提供图形界面和基于终端的界面,让你的生活更轻松。你越了解你的发行版的包管理器,你的生活就会越轻松。
### 在 Linux 上安装应用程序
如果你在使用 Linux 桌面时,偶尔想要安装一个应用程序,那么你可能正在寻找 [GNOME Software][2],一个桌面应用程序浏览器。
![GNOME Software 程序][3]
它会按你的预期工作:点击它的界面,直到你找到一个看起来有用的应用程序,然后单击 **安装** 按钮。
或者,你可以在 `GNOME Software` 中打开从网络下载的 `.rpm``.flatpakref` 包,以便它进行安装。
如果你更倾向于使用命令行,请继续阅读!
### 用 dnf 搜索软件
在安装应用程序之前,你可能需要确认它存在于你的发行版的服务器上。通常,使用 `dnf` 搜索应用程序的通用名称就足够了。例如,假设你最近阅读了 [一篇关于 Cockpit 的文章][4] 并决定尝试一下。你可以搜索 `cockpit` 验证该发行版是否包含它:
```
$ dnf search cockpit
 Last metadata expiration check: 0:01:46 ago on Tue 18 May 2021 19:18:15 NZST.
 ==== Name Exactly Matched: cockpit ====
 cockpit.x86_64 : Web Console for Linux servers
==== Name &amp; Summary Matched: cockpit ==
 cockpit-bridge.x86_64 : Cockpit bridge server-side component
 cockpit-composer.noarch : Composer GUI for use with Cockpit
 [...]
```
有一个精确的匹配。上面列出的匹配的包名为 `cockpit.x86_64`,但名称中的 `.x86_64` 部分表示它仅兼容该 CPU 架构。默认情况下,你的系统会安装适配当前 CPU 架构的软件包,因此你可以忽略该扩展名。所以你确认你要查找的包确实简称为 `cockpit`
现在你可以放心地使用 `dnf install` 安装它。 此步骤需要管理员权限:
```
$ sudo dnf install cockpit
```
一般来说,这就是典型的 `dnf` 工作流:搜索和安装。
然而,有时 `dnf search` 的结果并不清晰,或者你想要关于一个包的更多信息而不仅仅是它的通用名称。有一些相关的 `dnf` 子命令,具体取决于你想要的信息。
### Package 元数据
如果你觉得你的搜索已 _接近_ 想要的结果,但还不确定,查看包的元数据通常会有所帮助,例如项目的网址和描述。要获取此信息,请使用顾名思义的 `dnf info` 命令:
```
$ dnf info terminator
Available Packages
Name         : terminator
Version      : 1.92
Release      : 2.el8
Architecture : noarch
Size         : 526 k
Source       : terminator-1.92-2.el8.src.rpm
Repository   : epel
Summary      : Store and run multiple GNOME terminals in one window
URL          : <https://github.com/gnome-terminator>
License      : GPLv2
Description  : Multiple GNOME terminals in one window.  This is a project to produce
             : an efficient way of filling a large area of screen space with
             : terminals. This is done by splitting the window into a resizeable
             : grid of terminals. As such, you can  produce a very flexible
             : arrangements of terminals for different tasks.
```
这个信息告诉你可用包的版本、跟你系统关联的哪一个存储库提供它、项目的网站以及一个详细的功能描述。
### 哪个包提供的这个文件?
包名称并不总是与您要查找的内容相匹配。例如,假设你正在阅读的文档告诉你必须安装名为 `qmake-qt5` 的东西:
```
$ dnf search qmake-qt5
No matches found.
```
`dnf` 数据库非常广泛,因此你不要局限于搜索完全匹配的内容。你可以使用 `dnf provides` 命令来了解是否有任何东西提供了您正在寻找的作为某个更大包的一部分的内容:
```
$ dnf provides qmake-qt5
qt5-qtbase-devel-5.12.5-8.el8.i686 : Development files for qt5-qtbase
Repo        : appstream
Matched from:
Filename    : /usr/bin/qmake-qt5
qt5-qtbase-devel-5.15.2-3.el8.x86_64 : Development files for qt5-qtbase
Repo        : appstream
Matched from:
Filename    : /usr/bin/qmake-qt5
```
可以确认应用程序 `qmake-qt5` 是名为 `qt5-qtbase-devel` 的包的一部分。它还告诉你,应用程序会安装到 `/usr/bin`,因此你知道安装后它的确切位置。
### 包中包含哪些文件?
有时我发现自己从完全不同的角度了解 `dnf`。有时,我已经确认我的系统上安装了一个应用程序;我只是不知道我是怎么安装的。还有一些时候,我知道我安装了一个特定的软件包,但我不清楚这个软件包到底在我的系统上安装了什么。
如果你需要对包的<ruby>有效负载<rt>payload</rt></ruby>进行 <ruby>逆向工程<rt>reverse engineer</rt></ruby>,可以使用 `dnf repoquery` 命令和 `--list` 选项。这将查看存储库中有关包的元数据,并返回该包提供的所有文件的列表:
```
$ dnf repoquery --list qt5-qtbase-devel
/usr/bin/fixqt4headers.pl
/usr/bin/moc-qt5
/usr/bin/qdbuscpp2xml-qt5
/usr/bin/qdbusxml2cpp-qt5
/usr/bin/qlalr
/usr/bin/qmake-qt5
/usr/bin/qvkgen
/usr/bin/rcc-qt5
[...]
```
这些列表可能很长,使用 `less` 或你喜欢的翻页命令配合管道操作会有所帮助。
### 移除应用程序
如果你决定系统中不再需要某个应用程序,可以使用 `dnf remove` 卸载它,该包本身安装的文件以及不再需要的任何依赖项都会被移除:
```
$ dnf remove bigapp
```
有时,你发现随着一个应用程序一起安装的依赖项对后来安装的其他应用程序很有用。如果两个包需要相同的依赖项,`dnf remove` _不会_ 删除依赖项。在安装和卸载大量应用程序之后,到处都有一个孤立的包的现象并不少见。大约每年我都要执行一次 `dnf autoremove` 来清除所有未使用的软件包:
```
$ dnf autoremove
```
这不是必需的,但这是一个让我的电脑感觉更好的大扫除步骤。
### 了解 dnf
你对包管理器的工作方式了解得越多,在必要时安装和查询应用程序就越容易。即便你不是 `dnf` 的重度使用者,当你发现自己与基于 RPM 的发行版交互时,了解它也会很有用。
告别 `yum` 后,我最喜欢的包管理器之一是 `dnf` 命令。虽然我不喜欢它的所有子命令,但我发现它是目前最鲁棒的<ruby>包管理系统<rt>package management system</rt></ruby>之一。 [**下载我们的 `dnf` 速查表**][5] 习惯该命令,不要害怕尝试一些新技巧。一旦熟悉了它,您可能会发现很难使用其他任何东西。
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/6/dnf-linux
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[hanszhao80](https://github.com/hanszhao80)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/brown-package-red-bow.jpg?itok=oxZYQzH- (Package wrapped with brown paper and red bow)
[2]: https://wiki.gnome.org/Apps/Software
[3]: https://opensource.com/sites/default/files/gnome-software.png (The GNOME Software app)
[4]: https://opensource.com/article/20/11/cockpit-server-management
[5]: https://opensource.com/downloads/dnf-cheat-sheet