Fedora-Linux-系统管理指南-全-

Fedora Linux 系统管理指南(全)

原文:annas-archive.org/md5/7c78dbc0c182d53af77c578e9b7937c0

译者:飞龙

协议:CC BY-NC-SA 4.0

– 亚历克斯·卡列哈斯

第一章:贡献者

关于作者

亚历克斯·卡列哈斯是红帽公司的服务内容架构师,红帽是全球领先的企业开源解决方案提供商,涵盖了 Linux、云计算、容器和 Kubernetes 等领域。他目前居住在墨西哥城,并且是一名自由开源软件贡献者。作为一名拥有超过 20 年系统管理员经验的专家,他在基础设施加固和自动化方面具有丰富的专业知识。作为开源解决方案的爱好者,他通过在不同的公共访问和大学活动中分享知识,支持社区发展。

天生是极客,选择 Linux,当然也选择 Fedora。

第二章:关于审阅者

本·科顿(Ben Cotton)在 Fedora 和其他开源社区活跃了十多年。他的职业生涯涵盖了公共和私人部门,曾担任桌面支持、高性能计算管理、市场营销和项目管理等多个角色。本是《开源项目的程序管理》一书的作者,他还为《远程人类》一书、The Next Platform、Opensource.com、Scientific Computing 等杂志和网站撰写过文章。

José “Kaz” Casimiro Rios 自 1994 年起便开始接触 Linux。他在 IT 行业身兼多职,担任过系统管理员、DBA、开发人员、支持工程师和讲师等多种角色。Kaz 目前在红帽公司担任系统设计工程师,隶属于认证团队,协助他们进行认证考试的构建、开发和支持。

我要感谢我的妻子,感谢她一直给予我支持和理解。还要感谢我的猫们,让我的每一天充满乐趣。

伊万·查韦罗(Iván Chavero)

目录

前言

第一部分:Fedora 项目

1

Linux 和开源项目

Linux 简史

理解 Linux 发行版

Fedora 项目

红帽贡献路径

Fedora 的使命与基础

参与项目

Fedora 作为系统管理工具

命令行界面

基础知识

引导示例 – 释放文件系统空间

桌面环境

总结

进一步阅读

第二部分:工作站配置

2

安装最佳实践

技术要求

创建启动介质

Fedora 媒体写入器

启动

分区本地存储

第一次启动

软件包管理

附加软件包选择

总结

进一步阅读

3

调节桌面环境

技术要求

初步系统调优

调节 swappiness 值

优化桌面体验

GNOME Tweak 工具

自定义面板和任务栏

任务栏

使用小工具简化任务

Conky

带有 Dock 的实用应用

总结

进一步阅读

4

优化存储使用

技术要求

理解文件格式和文件系统

创建 Btrfs 文件系统

优化存储空间大小

空间分配检查

使用 btrfs-usage-report 命令

深入了解逻辑卷管理器

快照之间的差异

发现 Stratis 存储

创建 Stratis 池

总结

进一步阅读

5

网络与连接

技术要求

了解基础知识

NetworkManager 命令行接口(nmcli)

优化无线连接

识别设备

寻找最佳质量的网络连接

nm-connection-editor

安全性如何?

通过 VPN 改善网络连接

基于 IPSec 的 VPN

OpenVPN

使用控制中心配置 VPN 客户端

网络性能监控

nmon

bpytop

总结

进一步阅读

第三部分:生产力工具

6

沙盒应用程序

技术要求

检查沙盒应用程序

SELinux 沙盒

深入了解 AppImage 应用程序

运行 AppImage

开发 AppImages

检查 Flatpak 应用程序

使用 Flatpak 应用程序

构建 Flatpak 应用程序

总结

进一步阅读

7

文本编辑器

技术要求

文本编辑器和命令行

Emacs 概述

基础

精通 GNU Emacs

Nano 基础

强大的 vim

基础

精通 vim

总结

进一步阅读

8

LibreOffice 套件

技术要求

探索 Fedora Linux 上的办公工具

WPS Office

ONLYOFFICE

Calligra

字体

LibreOffice

习惯使用 Writer 和 Calc

Writer

Calc

创建幻灯片和图像管理

总结

进一步阅读

9

邮件客户端和浏览器

技术要求

使用 Evolution 进行邮件发送

使用 Thunderbird 进行邮件发送

信赖的老版 Firefox

自定义 Firefox

通过 Google Chrome 扩展浏览

总结

进一步阅读

第四部分:系统管理工具

10

系统管理

技术要求

系统管理员的三条法则

KISS 原则

了解基本任务

一点 Git 和编程基础

Bash 脚本编写

Git

基础知识

别忘了备份

归档与压缩

使用 Git 进行版本管理

使用 Ansible 自动化

基础知识

入门

永无止境的学习

总结

11

性能调优最佳实践

技术要求

理解内核调优

调优内核参数

主要调优 – CPU 和内存

监控工具概述

改善 CPU 使用

改善内存使用

不要忽视存储调优

改善存储空间使用

通过网络调优提升性能

分析度量标准

总结

12

通过 SELinux 解开安全之谜

技术要求

了解强制访问控制

标签与类型强制

SELinux 的工作原理

如何排查 SELinux 问题

标签

SELinux 必知

策略漏洞

黑客攻击

总结

进一步阅读

13

虚拟化与容器

技术要求

使用 QEMU、KVM 和 libvirt 的虚拟化

管理工具

简化虚拟机创建过程

使用 GNOME Boxes

通过 Podman 发现 OCI 容器

总结

进一步阅读

索引

你可能喜欢的其他书籍

序言

Linux 系统管理是一项要求你始终保持在技术前沿的工作。因此,你需要拥有合适的工具来确保工作得当。

Fedora Linux 是基于 Red Hat Enterprise Linux 开发的发行版,提供了能够帮助我们完成任务的工具。

在本书中,我将与您分享如何将 Fedora Linux 用作工作站操作系统来管理 Linux 系统。

通过我的 20 年系统管理员经验,结合建议、最佳实践、技巧,甚至一些窍门,我将帮助你建立一个能够优化系统管理员任务的工作站。

本书适合谁阅读

本书适合所有希望开始使用 Fedora Linux 作为工作站,执行系统管理员日常任务的人。它还将帮助你学习如何优化该发行版的工具以进行管理任务。

你需要了解 Linux 和系统管理的基础知识,但不需要深入的知识。

本书提供了一个实际的背景,展示如何使用工作站进行最常见的系统管理任务。

本书的内容

第一章Linux 与开源项目,介绍了当今最流行的开源项目和 Linux 发行版,重点突出它们的主要用途和区别。

第二章安装最佳实践,研究了安装 Fedora Linux 以及优化其作为工作站使用的最佳实践。

第三章调整桌面环境,概述了增强工作环境可用性的不同小程序和插件。

第四章优化存储使用,分析了不同类型的本地存储及其配置,以优化性能。

第五章网络与连接性,概述了网络连接管理以及性能监控工具。

第六章沙箱应用程序,探讨了桌面沙箱应用程序的使用与配置。

第七章文本编辑器,总结了 Fedora Linux 中最流行和广泛使用的文本编辑器的特点。

第八章LibreOffice 办公套件,概述了 LibreOffice 办公套件的办公工具,并总结了每个应用程序的主要选项——Writer 用于文字处理,Calc 用于电子表格,Impress 用于幻灯片,Draw 用于图像。

第九章邮件客户端与浏览器,探讨了 Fedora Linux 中包含的互联网生产力工具、邮件客户端和浏览器,如 Evolution、Thunderbird、Firefox 和 Google Chrome。

第十章系统管理,提供了系统管理的基础知识,以及一些有用的技巧和快捷键。它还探讨了应用最佳实践的基础。

第十一章性能调优最佳实践,探讨了操作系统调优的最佳实践,作为提高系统管理性能的一种方法。

第十二章SELinux,介绍了基于策略的访问控制基础,作为 Fedora Linux 中的安全执行模块。

第十三章虚拟化与容器,概述了不同的 Fedora Linux 虚拟化资源。它提供了虚拟化的基础以及 Fedora Linux 中可用的方法——基于 KVM/libvirt 的虚拟化或使用 Podman 的容器。

最大化使用本书的效果

尽管管理员需要具备基本的 Linux 知识,但要跟随每章中的安装和配置指南并不需要深入的知识。

操作系统 下载链接
Fedora Linux 工作站 fedoraproject.org/workstation/download/

如果你正在使用本书的数字版本,我们建议你亲自输入代码或从本书的 GitHub 仓库获取代码(链接在下一节提供)。这样做有助于避免与复制和粘贴代码相关的潜在错误。

下载示例代码文件

你可以从 GitHub 下载本书的示例代码文件,网址为github.com/PacktPublishing/Fedora-Linux-System-Administration。如果代码有更新,它将会在 GitHub 仓库中更新。

我们还在github.com/PacktPublishing/上提供了其他代码包,来自我们丰富的书籍和视频目录。赶紧去看看吧!

使用的约定

本书中使用了许多文本约定。

文本中的代码:表示文本中的代码词汇、数据库表名、文件夹名、文件名、文件扩展名、路径名、虚拟 URL、用户输入和 Twitter 用户名。例如:“将下载的WebStorm-10*.dmg磁盘镜像文件作为另一个磁盘挂载到你的系统中。”

一块代码块如下所示:

for <variable> in <list>
do
command <variable>
done

当我们希望引起你对代码块中特定部分的注意时,相关的行或项目会用粗体标出:

if <condition>;
then
<statement 1>
...
<statement n>
else
<statement alternative>
fi

任何命令行输入或输出都按以下方式书写:

$ sudo grep -E 'svm|vmx' /proc/cpuinfo
$ sudo dnf install qemu-kvm virt-manager virt-viewer guestfstools virt-install genisoimage

粗体:表示新术语、重要单词或屏幕上显示的文字。例如,菜单或对话框中的词语以粗体显示。以下是一个例子:“从管理面板中选择系统信息。”

提示或重要备注

看起来是这样的。

联系我们

我们始终欢迎读者的反馈。

一般反馈:如果你对本书的任何部分有问题,请发送邮件至 customercare@packtpub.com,并在邮件主题中提到书名。

勘误表:尽管我们已尽最大努力确保内容的准确性,但仍可能出现错误。如果你在本书中发现错误,我们将非常感谢你向我们报告。请访问 www.packtpub.com/support/errata 并填写表格。

盗版:如果你在互联网上发现任何形式的非法复制品,我们将非常感谢你提供该资料的地址或网站名称。请通过 copyright@packtpub.com 与我们联系并附上资料的链接。

如果你有兴趣成为作者:如果你在某个领域拥有专长,并且有兴趣撰写或贡献一本书,请访问 authors.packtpub.com

分享你的想法

阅读完 Fedora Linux 系统管理 后,我们很希望听到你的想法!请 点击这里直接访问亚马逊评论页面 并分享你的反馈。

你的评价对我们和技术社区都非常重要,帮助我们确保提供优质内容。

下载这本书的免费 PDF 版本

感谢购买本书!

你喜欢随时随地阅读,但又无法随身携带印刷版书籍吗?你的电子书购买是否与所选设备不兼容?

不用担心,现在购买每本 Packt 书籍时,你都会免费获得该书的无 DRM PDF 版本。

在任何地方、任何设备上阅读。直接从你最喜爱的技术书籍中搜索、复制并粘贴代码到你的应用程序中。

这里的优惠还不止这些,你可以获得独家的折扣、新闻通讯,并且每天将有精彩的免费内容直接发送到你的邮箱

按照以下简单步骤享受这些福利:

  1. 扫描二维码或访问下面的链接

packt.link/free-ebook/978-1-80461-840-0

  1. 提交你的购买凭证

  2. 就是这样!我们会直接将你的免费 PDF 以及其他福利发送到你的邮箱

第一部分:Fedora 项目

本部分介绍了今天最受欢迎的开源项目和 Linux 发行版,重点介绍了它们的主要用途和差异,并着重于 Fedora 项目的优势以及如何与之合作。

本部分包含以下章节:

  • 第一章Linux 和开源项目

第三章:Linux 和开源项目

系统管理是一项需要正确工具以实现所需流程优化的工作。要管理基于 GNU/Linux 的系统,你必须拥有一台能够促进上述优化的工作站。Linux 工作站在这方面提供了许多优势。根据我的经验,Fedora Linux,一个由Red Hat赞助的社区开发发行版,提供了一套推荐的 Linux 系统管理工具。

在学习如何配置工作站进行系统管理之前,我们将回顾一下这个操作系统的历史,以便我们能够更好地理解背景,最重要的是,知道我们如何能够帮助开发该发行版,以便我们能够改进它。

在本章中,我们将了解以下内容:

  • Linux 简史

  • 理解 Linux 发行版

  • Fedora 项目

  • 命令行界面

  • 桌面环境

让我们开始吧!

Linux 简史

在进入正题之前,我想提供一些关于操作系统历史的小背景。正如我们所知,个人计算机的历史相对较短——大约 50 年,特别是GNU/Linux的历史,甚至还不到这个时间。

在 1960 年代末期,Ken Thompson基于 Multics 在 PDP-7 小型计算机上编写了第一个版本的Unix,它由内核、shell、编辑器和汇编器组成,那时是黑暗的时代。

1970 年,操作系统的开发在 AT&T 贝尔实验室继续进行。此时,Brian Kernighan建议使用Uniplexed Information & Computing ServiceUNICS)作为名称,运行在 PDP-11 计算机上。然而,使用的 BCPL 和 B 语言在新平台上存在一些实现问题。1972 年,Denis Ritchie利用这两种语言开发了一种新的高级语言,现在称为C 语言,增加了数据类型和其他强大功能。由此,Unix系统诞生了。

该平台的软件开发持续进行,操作系统有了重要的补充。1976 年,MIT 的学生Richard Stallman在一个完全使用自由软件的小组工作时,编写了第一个版本的Emacs,它是文本编辑器与 修正工具TECO)。

在 1980 年代初期,几乎所有的软件都是专有的,因为科技公司各自单独集中精力,而没有考虑协作开发。这促使斯托尔曼于 1983 年创建了GNU 项目(即GNU 不是 Unix),该项目旨在创建一个基于 Unix 的自由操作系统。因为 Unix 的总体设计已经得到了验证且具备可移植性,并且带回了计算机社区早期的合作精神。

Stallman 通过以 150 美元分发代码启动了GNU Emacs。他随后用这笔钱资助了 1985 年成立的自由软件基金会。Emacs 是根据Emacs 通用公共许可证发布的,该许可证允许自由分发和使用,同时保留版权,并且即使在对代码进行修改或添加时,也要求保留版权。

基于这一理念,1989 年发布了GNU 通用公共许可证GPL)的第一个版本,将自由软件的使用和分发扩展到所有将其作为 GNU 项目一部分的编程开发。

许可证的第二个版本于 1991 年发布,主要的区别在于许可证的义务不能分开,因为存在冲突的义务。这一条款旨在阻止任何一方以专利侵权或其他诉讼为由,妨碍用户自由使用早期版本的权利。

在同一年,Linus Torvalds,一名芬兰学生,使用 Tanenbaum 的 1987 年书籍《操作系统:设计与实现》、Bach 的 1986 年书籍《UNIX 操作系统的设计》、以及 Jolitz 的文章《将 UNIX 移植到 386:一种实际方法》(William Jolitz,Dr. Dobb’s Journal,1991 年 1 月,16 卷 1 期,页码 16-46),移植了一些基础工具,创建了一个(免费的)操作系统……用于 386(486) AT 克隆机,并在 Usenet 小组comp.os.minixgroups.google.com/g/comp.os.minix/c/dlNtH7RRrGA/m/SwRavCzVE7gJ)上寻求帮助,这就是我们今天所知道的Linux

图 1.1 – Linus Torvalds 在 Usenet 小组 comp.os.minix 中的原始帖子

图 1.1 – Linus Torvalds 在 Usenet 小组 comp.os.minix 中的原始帖子

Linux 并非一直都是开源的。最初的 Linux 许可证禁止商业再分发。直到 1992 年初发布的0.12版本,Linux 内核才在 GPL 下发布。Linus Torvalds 表示,开源 Linux 是他做过的最好的事情。

受到这一努力成功的启发,出现了多个新的软件开发项目,以推动新创建的操作系统。这些开发提供了新的功能和工具,补充和促进了 Linux 的使用,并扩展了该平台在商业和个人用途等不同领域的应用。

GPL 的使用是推动基于计算机社区合作性质发展的动力。

这种合作精神奠定了今天我们所称之为 Linux 的基础,但为了让这些努力的成果惠及每个人,还需要推动力。这一推动力来自于 Linux 发行版,即称为 distro 的版本。

了解 Linux 发行版

GNU 项目在实现其官方开发的内核时遇到了一些问题。这个内核被称为Hurd,它是一个协议集合,规范了不同组件之间应如何相互作用 (www.gnu.org/software/hurd/index.html)。这些工具运行良好,但没有足够的凝聚力与操作系统集成。当 Linus Torvalds 发布 Linux 内核时,许多热心的开发者将 GNU 代码,包括编译器,移植到 Linux 上。这些努力填补了剩余的空白,最终得到了一个完全免费的操作系统。

1992 年,Linux 和 GNU 项目联手 (laurel.datsi.fi.upm.es/~ssoo/IG/download/timeline.html),理查德·斯托曼提议将其称为 GNU/Linux,因为许多工具都是从 GNU 项目集成的。这导致了新项目的诞生,这些项目将 GNU 工具和 Linux 内核整合成了我们今天所知的Linux 发行版

发行版由 Linux 内核、GNU 工具和其他大量软件包组成;许多发行版还提供了类似于其他现代操作系统的安装系统。发行版通常被分为多个软件包,其中一些只提供内核二进制文件、编译工具和安装程序。软件包以已编译的代码形式提供,软件包的安装和卸载由软件包管理系统PMS)而不是简单的文件归档工具来处理。

一些发行版甚至作为嵌入式操作系统交付到某些设备上,除了基于Android的移动发行版外。这些发行版是专门为移动电话创建的。

多年来,出现了不同的 GNU/Linux 发行版项目,其中那些历时较长的项目尤为重要。除此之外,它们还催生了新的发行版,包括以下内容:

  • Slackware:发布于 1993 年,最初名为Softlanding Linux SystemSLS),并包含了 X Window 系统。在短暂的一段时间内(1992 年),它是最完整的发行版。凭借当时最新的工具,Slackware Linux 为新老用户提供了一个功能齐全的系统,适合用于各种需求,无论是作为桌面工作站还是服务器。Web、FTP 和电子邮件服务开箱即用,还有丰富的流行桌面环境可供选择。为了有意开发或编译额外软件的用户,还提供了完整的开发工具、编辑器和库。它是第一个受益于全球数百万开发者工作成果的发行版。

  • Debian:1993 年,Ian Murdock 对 SLS(后来称为Slackware)的维护不善和漏洞频发感到失望,发布了他最初称之为Debian Linux 发行版的操作系统。Debian 是他与当时女友 Debra Lynn 的名字与他自己名字的合成词(将几个单词的部分合并为一个新词)。Debian 的稳定版本在个人计算机和服务器中使用最为广泛。该版本包含了 Debian Linux 宣言,其中 Murdock 描述了他对操作系统的愿景,呼吁将其保持为“在 Linux 和 GNU 精神下开放”。Debian 的发行版本以玩具总动员电影中的人物命名。Debian 也是许多其他发行版的基础,Ubuntu 是其中最著名的。Debian 使用 dpkgDebian 包管理器)作为包管理系统,以及其众多衍生版本。

  • Red Hat:同样在 1993 年,Marc Ewing 在北卡罗来纳州罗利市的家中创建、调试并分发他的 Linux 发行版 CD。Red Hat 这个名字来源于他大学时在计算机实验室的日子;他总是戴着一顶红色帽子,用户会说:“如果你需要帮助,找那个戴红色帽子的人。” Bob Young 在一次技术会议上遇到了他,并开始购买他的 CD 进行转售,因 Linux 的兴趣日益增长。1995 年,他们联合创办了 Red Hat Software。Red Hat 使用 rpm(最初名为 Red Hat Package Manager;由于它在多个 Linux 发行版中流行,后来改为 RPM Package Manager)作为包管理系统,以及其众多衍生版本。

现在,是时候了解我们使用的发行版了,它有一些有趣的原则。

Fedora 项目

Red Hat Linux 每 6 个月发布一次,甚至可以在 Best Buy 买到。经过几次发布后,它开始获得大企业客户,部分原因是 2000 年左右 Microsoft 遭遇的反垄断诉讼,但它并没有为满足这些客户需求而定义明确的支持周期。公司意识到,他们试图在两个不同的方向上开发产品——一方面寻求行业所需的稳定性,另一方面则寻求使用最新的开源发展进行创新。

因此,他们决定将工作分成两个基本上独立的实体——Red Hat 企业 LinuxRHEL)和 Fedora 项目——每个实体都尽力解决自己的问题。

对于 RHEL,任务是将其打造成一个稳固、稳定的平台,客户和合作伙伴可以依赖它进行 5 至 7 年的支持周期。Red Hat 首次为 Red Hat Linux 6.1 提供了企业级 Linux 支持订阅。这并不是一个独立的产品;而是一个名为 Red Hat 6.2E 的订阅服务。此后,Red Hat 开始构建一个独立的产品,基于 Red Hat Linux,并具有商业服务级协议和更长的生命周期。

Fedora LinuxFedora 项目(最初名为Fedora.us)开发,并由红帽资助。它遵循自己独立的发布计划,每 6 个月发布一个新版本(分别在 4 月和 10 月)。Fedora 提供了一个现代化的 Linux 操作系统,采用了行业中许多最新的技术。

要创建一个新的 RHEL 版本,大部分开发工作发生在上游项目中。然后,这个新版本会集成到 Fedora Linux 中,随后的“产品化”过程会在 CentOS Stream 中进行,最终成为 RHEL。

这个过程被称为红帽的贡献路径,对于深入了解该发行版的发展流程至关重要。它也帮助我们理解在这个流程中发行版的重要性。

红帽贡献路径

2020 年 12 月,红帽宣布停止开发CentOS 项目,这个项目自 2014 年起由红帽资助,其 2004 年版本 2 从RHEL 2.1AS分支而来,从那时起它被集成为CentOS Stream,并成为 RHEL 开发贡献路径的一部分。

RHEL 的开发始于社区项目,在这些项目中,行业中最新和最创新的技术被开发出来。Fedora 的角色是将这些技术适应并融入到每个新版本的发行版中。

每 3 年发布一个 RHEL 的新主版本。当下一个主版本的 RHEL 发布大约在 1 年内时,这些创新达到了一个最优开发水平,通过Fedora 项目的开发者、集成商以及独立软件和硬件供应商之间的反馈促进了这一进程,提供了行业所需的稳定性。然后,CentOS StreamFedora Linux分支出来。CentOS Stream 的代码成为下一个 RHEL 版本,这意味着用户可以在发布前参与产品开发并测试其工作负载。这成为了 RHEL 开发的持续集成,从而缩短了在未来 RHEL 版本发布中应考虑的反馈循环。

以下图展示了这一流程:

图 1.2 – 红帽贡献路径

图 1.2 – 红帽贡献路径

Fedora Linux在 RHEL 的开发中扮演着至关重要的角色,如前所述,但Fedora Linux本身就是一个可用的操作系统,拥有行业中最现代化和创新的工具。作为副产品,它也被广泛应用于企业环境中,以管理基于 RHEL 的系统。这得益于上述红帽贡献路径所提供的便利。

Fedora 的使命和基础

Fedora 项目致力于构建一个自由和开源的软件平台,合作并分享面向最终用户的解决方案。

由于Fedora 社区包含成千上万具有不同观点和方法的个人,他们的合作精神基于他们所称的四大基础

图 1.3 – Fedora 的四大基础

图 1.3 – Fedora 的四大基础

让我们详细看看它们:

  • 自由

我们致力于自由软件 和内容。”

(docs.fedoraproject.org/en-US/project/)

目标是生产一个只包含自由软件的可用操作系统。避免使用专有或受专利保护的内容,采用免费的替代方案,从而提供一个能够将最具创新性的软软件带给每个人的发行版,使得任何人都能合法使用。

  • 朋友

我们是一个强大且关爱的社区。”

(docs.fedoraproject.org/en-US/project/)

Fedora 社区是一个多学科且多元化的社区,具有共同的目标:推动自由软件的发展。任何愿意帮助的人,无论其技能如何,只要相信其核心价值观,都可以在这个社区找到一席之地,并享受一个友好与协作的环境。

  • 特点

我们关心 卓越的软件。”

(docs.fedoraproject.org/en-US/project/)

许多赋能 Linux 的功能来自 Fedora 社区,使其对全球的许多人都变得灵活且有用。Fedora 社区是自由软件开发的坚定信徒,无论这些功能是否被用于该发行版,它都支持功能的清晰和透明开发,使得任何想参与发行版的人都能使用这些功能。

  • 首先

我们致力于 创新。”

(docs.fedoraproject.org/en-US/project/)

Fedora 项目提供最新的稳定且强大的自由软件,是一个展示操作系统使用未来的平台。它推动这种软件的发展,展示了合作技术进步的成果。Fedora 总是考虑在一切之前为未来做好准备。

除了这四大基础,Fedora 项目还有一个非常明确的愿景和使命。

愿景

“Fedora 项目设想一个每个人都能从由包容、欢迎和开放思维的社区构建的自由和开源软件中受益的世界。”

(docs.fedoraproject.org/en-US/project/#_our_vision)

Fedora 的愿景遵循 GNU 项目的原则,使用自由和开源软件的好处扩展到所有需要使用它的人,以一种包容所有社区并开放所有可能性的方式。

使命

“Fedora 为硬件、云和容器创建了一个创新平台,使软件开发人员和社区成员能够为其用户构建量身定制的解决方案。”

(docs.fedoraproject.org/en-US/project/#_our_mission)

Fedora 的使命专注于创新和适应现有与未来平台上的技术,为增强最终用户体验的解决方案提供支持。

贡献项目

Fedora 社区致力于构建和开发自由开源软件,并推动对整个社区具有重要意义的进展。它快速且定期地将这些进展融入到发行版中,甚至融入到其他 GNU/Linux 发行版中。Fedora 在短期内整合了自由开源方法和易用性。

软件开发不仅仅涉及程序员——它还需要设计师、艺术家、作家、演讲者、翻译员、系统管理员等。协调这一切工作需要社区中的领导力,这使得决策不至于过度拖延。

重要事项

您无需成为贡献者即可使用 Fedora Linux。在接下来的章节中,您将学习 如何使用 Fedora Linux 进行系统管理。但是,如果您想要为项目做出贡献,以下是方法!

项目的领导由 Fedora 委员会提供,该委员会由八个职位组成,其中两个由选举产生的社区贡献者担任。除了委员会,还有几个领导小组:

  • Fedora 工程指导委员会FESCo):管理 Fedora 发行版的技术特性及 Fedora 项目中政策的具体实施。

  • Fedora 思维委员会:代表用户和贡献者社区的增长与支持的领导层。

除了工作组(各个版本的发行版在这里开发,如 工作站服务器物联网CoreOS),一些子项目在 Fedora 模式下开发机会领域。还有一些较为非正式的兴趣小组(SIGs),它们为发行版采用框架或轻量级桌面环境。

行为准则

Fedora 通过一套准则促进其协作者与社区成员之间的最佳互动,这些准则包含在一个被称为 行为准则 的文档中,决策依据所有可用的信息和背景,以追求为其成员提供最佳的环境。它并不打算限制表达或惩罚任何社区成员;它仅详细说明了被视为可接受或不可接受的行为类型。

注意

有关完整的行为准则,请参阅 Fedora 文档:docs.fedoraproject.org/en-US/project/code-of-conduct/

开始使用 Fedora

要为 Fedora 项目做出贡献,请遵循以下步骤:

  1. 第一步是创建一个用户账户,访问 accounts.fedoraproject.org/

图 1.4 – Fedora 账户

图 1.4 – Fedora 账户

  1. 填写注册表单后,系统将要求您验证注册的电子邮件地址:

图 1.5 – Fedora 账户 – 账户创建

图 1.5 – Fedora 账户 – 账户创建

  1. 点击你通过邮件收到的链接后,系统会要求你创建密码:

图 1.6 – Fedora 账户 – 设置密码

图 1.6 – Fedora 账户 – 设置密码

  1. 访问个人资料后,点击 协议 标签来签署 Fedora 项目贡献者协议 文档:

图 1.7 – Fedora 账户 – 配置文件设置

图 1.7 – Fedora 账户 – 配置文件设置

  1. 点击 签署 按钮来签署协议:

图 1.8 – Fedora 账户 – Fedora 项目贡献者协议

图 1.8 – Fedora 账户 – Fedora 项目贡献者协议

签署协议后,继续进入个人资料配置区并加入一个感兴趣的开发或协作小组。

如有需要,Fedora 提供了一款在线工具 whatcanidoforfedora.org/,通过你提问的问题,帮助你找到与兴趣相匹配的小组:

图 1.9 – 我能为 Fedora 做些什么?

图 1.9 – 我能为 Fedora 做些什么?

尽管此页面对于初次接触非常有用,但已被项目文档弃用。欲了解更多信息,请参考 docs.fedoraproject.org/en-US/project/join/ 中的兴趣小组 (SIG)。

现在我们知道如何为 Fedora 项目做出贡献,接下来让我们学习如何使用该发行版来管理基于 Linux 的系统。

Fedora 作为系统管理工具

Fedora Linux 作为工作或学习中的主要发行版,也支持 Fedora 项目的发展,作为最终用户,你可以通过提供反馈来贡献,分享你对发行版中工具和服务的使用体验,还可以利用这些创新,使自己在日常活动中更加高效和有生产力。

在 IT 行业中,最重要的活动之一是系统管理,主要职责是维护、配置并确保计算机系统的可靠运行,尤其是多用户计算机如服务器。系统管理员需要确保被管理计算机的正常运行时间、性能、资源和安全性满足用户需求,同时确保在这一过程中不超出预定预算。

使用 Linux 工作站管理 Linux 系统提供了许多优势,这些优势可以提高效率和生产力,因为通过宿主操作系统,你可以自动化任务或根据被管理的操作系统执行功能测试。

Fedora 由于在 RHEL 贡献开发中的重要地位,成为了管理类似平台(如其变种(rpm 软件包))的一个高效工具。

同时,当使用这种方法进行 Linux 系统管理时,有可能为发行版的开发做出贡献,因为在这里我们会不断测试平台及其内部的各种软件包和项目。

因此,如果你希望使用Fedora Linux来管理基于 Linux 的系统,你可以通过报告意外行为或为系统性能的改进做贡献来支持Fedora 项目。这包括提出新功能的建议。在报告错误之前,建议查阅Ask Fedora网站 ask.fedoraproject.org/,这是社区支持论坛,以确认该意外行为是否曾经发生过:

图 1.10 – Ask Fedora

图 1.10 – Ask Fedora

注意

有关如何报告错误的更多信息,请参考 Fedora 文档 docs.fedoraproject.org/en-US/quick-docs/howto-file-a-bug/

在将发行版自定义为我们的系统管理工作站之前,让我们回顾一下如何使用我们的主要工具。

命令行界面

随着图形用户界面的发展,命令行的使用通常被舍弃。然而,命令行仍然是操作系统管理的主要工具,无论是系统管理员还是高级和权力用户。除了基于 Linux 的系统外,命令行也可以在 Windows 或 Mac 系统上使用。命令行的使用扩展了系统管理和运维的能力。

要达到最大效率所需的流利度,需要在每个机会中进行练习。使用 Linux 作为工作站让我们有机会练习这一技能,从而提高我们在执行日常任务时的生产力和效率。

Fedora Linux中默认的命令行解释器(Shell)是Bourne Again ShellBash),可以通过发行版提供的不同终端模拟程序访问:

  • xterm:这是 X Window 系统的原始终端模拟程序。

  • Konsole:用于Konsole 桌面环境KDE)的终端模拟程序。

  • gnome-terminal:用于 GNOME 桌面环境的终端模拟程序。

  • Alacritty:这是一个轻量级的基于 OpenGL 的终端模拟程序,注重性能。

  • yakuakeguake):这是一个下拉式终端模拟程序,可以通过快捷键轻松访问。guake是用于 GNOME 桌面环境的版本。

  • Terminator:这是基于gnome-terminal的终端模拟程序。它在一个窗口中提供多个终端模拟器实例,生成一个占满屏幕区域的终端控制台,并将其分割成一个可调整大小的网格。

现在我们了解了不同的终端模拟程序选项,让我们深入探讨一些有助于我们进入系统管理的主题。

基础知识

实践是提升控制台技能的唯一途径,但有一条路径可以帮助你提高。这条路径由四个部分组成,每个部分有三个层级;在练习过程中,以此作为指南,应该能帮助你在控制台上更加流利。

1. 请向懂的人请教

当遇到意外行为或需要某个工具来提高任务效率时,很多人都会犯一个常见的错误——首先在互联网上查找;这个习惯甚至成为了一种常见的说法:Google it(谷歌一下)。但开源项目为我们提供了参考资料(即 man 和 info 页面),解释了选项以及大多数情况下的包、命令、进程和服务的使用。信息搜索的流程是我们作为系统管理员需要打破的第一个范式,以提高效率。

基础水平

使用内置的 help 命令。如果你不知道或记不住如何使用某个命令,可以使用内置的 help 模块。为此,可以添加 --help-h 参数,或者甚至直接运行该命令而不带任何参数。该命令可能会有这些基本的help选项之一。

注意

不是所有命令都有内置帮助选项。在某些情况下,命令可能提供一两个选项。你可以测试命令是否附带此类帮助选项。如果没有,请使用下一级帮助。

让我们看一下它们的每个例子:

  • 使用内置help模块运行的命令:

图 1.11 – 内置帮助模块

图 1.11 – 内置帮助模块

  • 使用 -h 参数运行的命令:

图 1.12 – 带有 -h 参数的命令运行

图 1.12 – 带有 -h 参数的命令运行

  • 无参数运行的命令:

图 1.13 – 无参数运行的命令

图 1.13 – 无参数运行的命令

这些是大多数命令中包含的基本推荐选项。现在我们来看一种更复杂的帮助方式。不幸的是,有时候它没有作为包的一部分提供,或者需要安装一个单独的包才能获得这种帮助。在每种情况下,都会提到这一点。

中级水平

man 是你的朋友。除了内置的 help 命令,大多数命令都附带用户手册,详细说明了可用的各个选项和参数的使用方法。要查看手册,运行 man <命令> 命令:

图 1.14 – 命令用户手册

图 1.14 – 命令用户手册

在某些情况下,命令可能会包含 info 页面。这些页面可能引用相同的 man 页面,或者在某些情况下,提供有关命令使用方法和选项的更详细信息。要查看 info 页面,请运行 info <命令> 命令。

高级水平

操作系统提供了一个目录,存放软件包和服务的文档——例如,在/usr/share/doc目录中。你应该考虑安装内核文档,包括内核附带的驱动程序文档和各种配置选项的参考。kernel-doc包包含内核文档,用于安装和运行多个任务作为 root 用户:

[root@workstation ~]# dnf -y install kernel-doc
...output omitted…
[root@workstation ~]# ls /usr/share/doc/kernel-doc-6.0.9-300/Documentation/
ABI atomic_t.txt crypto  features  ia64  kernel-hacking memory-barriers.txt
...output omitted...
[root@workstation ~]# cat \
> /usr/share/doc/kernel-doc-6.0.9-300/Documentation/networking/bonding.rst
...output omitted...
Introduction
============
The Linux bonding driver provides a method for aggregating
multiple network interfaces into a single logical "bonded" interface.
The behavior of the bonded interfaces depends upon the mode; generally
speaking, modes provide either hot standby or load balancing services.
Additionally, link integrity monitoring may be performed.
...output omitted...

在操作系统提供的选项耗尽后,你可以访问不同的在线选项社区在你未找到合适的解决方案时非常有帮助。为此,你可以访问邮件列表电报频道IRC站点,如RedditStack Overflow,或者由Fedora 项目本身提供的站点,如Ask Fedora。通过这些选项,你可以与社区取得联系,社区将始终提供帮助。

2. 使用控制台

拥有一个基于 Linux 的工作站意味着在任何情况下都可以使用控制台,甚至在闲暇时也是如此。这个想法是利用任何机会使用它来启动应用程序或任务,并尽量减少使用鼠标和图形解决方案。

使用控制台的权限取决于使用它的用户的权限。特权账户是具有比普通用户更多权限的用户账户。特权账户可以执行例如安装或卸载软件、更新操作系统或更改系统或应用程序设置等操作。它们可能还可以访问普通用户无法访问的文件。

命令提示符提供有关正在使用它的用户权限的信息。

如果在打开控制台时,提示符显示为$,这意味着用户作为非特权用户登录到该系统。$提示符是普通用户的默认提示符:

[user@workstation ~]root user’s prompt is #. Logging in as a root user can be done in two ways:

*   By logging in with the **root** user’s username and password
*   By switching to the **root** user

This last point could involve the same username and password of the privileged user. Use the `su –` command to become the `root` user:

[user@workstation ~]$ su -

密码:[root 密码]

[root@workstation ~]sudo (超级用户执行) 命令,这是一个帮助我们为普通用户提供权限的程序。

大多数 Linux 发行版默认安装sudo。在某些情况下,甚至root访问权限被关闭。发生这种情况时,我们只能通过sudo命令访问。

在安装操作系统时,创建用户时可以选择将他们添加为系统管理员组的一部分。这将允许他们通过使用sudo命令和他们自己的密码切换到root用户。

要查看用户是否具有不同的sudo权限,请运行以下代码:

[user@workstation ~]$ sudo -l
Password: [user password]
Matching Defaults entries for user on workstation:
...output omitted...
User user may run the following commands on workstation:
    (ALL) PASSWD: ALL
...output omitted...

(ALL) PASSWD: ALL表示用户可以通过使用sudo命令和他们的密码访问操作系统的任何命令。要切换到root用户,请运行以下代码:

[user@workstation ~]$ sudo -i
Password: [user password]
[root@workstation ~]#

所以,现在我们知道如何使用控制台,根据我们的活动和权限,让我们学习如何提高我们的控制台技能。

基础级别

只需使用它。尽可能多地键入,以列出、搜索和打开应用程序。

中级水平

链接、重定向和连接。输入命令并理解它们的输出结果后,我们可以开始玩弄它们,将它们组合起来,从而简化任务。通过使用管道符号(|)以及使用 >< 进行输出和输入的重定向,我们可以生成一串命令,这就是我们所说的单行命令Bash 单行命令在计算机世界中非常著名,能够将命令链式连接起来执行特定任务甚至被视为一种艺术。在互联网上,甚至在社交网络中,我们可以找到许多这样的命令。有些命令使用这种重定向来解释用AWK 编程语言编写的模式处理并作为输出。

示例:将以下命令的输出发送到一个新文件:

[user@workstation ~]$ ip link show > link.txt
[user@workstation ~]$ cat link.txt
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 52:54:00:f9:69:14 brd ff:ff:ff:ff:ff:ff

示例:获取开放并正在监听的 TCP 端口及与之相关的进程,使用逗号分隔:

[root@workstation ~]# ss -tulpn | grep tcp | awk '{ print $1","$2","$5","$7 }'
tcp,LISTEN,0.0.0.0:22,users:(("sshd",pid=844,fd=3))
tcp,LISTEN,127.0.0.54:53,users:(("systemd-resolve",pid=707,fd=19))
tcp,LISTEN,0.0.0.0:5355,users:(("systemd-resolve",pid=707,fd=11))
tcp,LISTEN,127.0.0.1:6010,users:(("sshd",pid=1514,fd=9))
tcp,LISTEN,127.0.0.53%lo:53,users:(("systemd-resolve",pid=707,fd=17))
tcp,LISTEN,127.0.0.1:631,users:(("cupsd",pid=842,fd=7))
tcp,LISTEN,[::]:22,users:(("sshd",pid=844,fd=4))
tcp,LISTEN,[::]:5355,users:(("systemd-resolve",pid=707,fd=13))
tcp,LISTEN,[::1]:6010,users:(("sshd",pid=1514,fd=8))
tcp,LISTEN,[::1]:631,users:(("cupsd",pid=842,fd=6))

高级水平

如果你输入两次,你应该只编写一次脚本。

在系统管理中,任务变得重复是非常常见的。自动化这些任务的第一步,就是将它们汇总并转化为一系列指令,这就是所谓的shell 脚本。这个脚本或指令序列可以包含执行复杂任务的命令,例如使用输出作为变量设置并在同一执行中重复使用它们。

有很多关于如何创建 shell 脚本的文档。它们的结构应该类似于以下内容:

#!/bin/bash ← [1]
#
# IDENTITY ← [2]
#
# VARIABLES ← [3]
# COMMANDS ← [4]

让我们来看一下每个部分中突出显示的文本所表示的含义:

  • [1]Shebang。这表示使用这些指令的命令行解释器;脚本的功能测试必须确认其使用。

  • [2]:脚本必须包含身份识别信息——它的用途、作者、使用的版本,甚至创建日期和经历的修改。这个文档将帮助你使用它,并识别其范围。

  • [3]:在本节中,将设置用于执行指令的变量。

  • [4]:在本节中,你将找到将要执行的指令。

  1. 编辑文本文件

在 Linux 中,一切皆文件。因此,我们必须使用文本编辑器来帮助我们执行配置或管理任务。深入了解首选编辑器有助于提高这项工作的效率,特别是如果某些编辑器具有专门的附加功能或插件,用于处理如识别或验证用不同编程语言或格式编写的文件的语法。

基础水平

GNU Nano 是一个简单、轻量级、开源的命令行文本编辑器,使用 C 语言编写。作为 GNU 项目的一部分开发,它模拟了 Pico 文本编辑器,后者是 Pine 邮件客户端的一部分:

图 1.15 – Nano 编辑器

图 1.15 – Nano 编辑器

GNU Nano 没有很多附加功能,但它有一些内置的功能,例如用于突出显示不同编程语言的功能。

中级水平

Vim 是一个开源命令行文本编辑器(以charityware许可证授权),用C语言编写,并带有一个名为 Vim(或 VimL)的脚本语言。它于 1970 年代作为ex行编辑器的可视模式vi,其基础)开发。最初的vi是一个没有语法高亮、用C编写且只有命令行界面的模式化文本编辑器。后来,在 1980 年代,vim作为vi文本编辑器的克隆版本发布,支持个人计算机,并作为gVim移植,增加了语法高亮、脚本语言(用于定制和扩展)以及对更多计算机平台的支持:

图 1.16 – Vim 编辑器

图 1.16 – Vim 编辑器

vim有许多附加组件和插件可以增强其使用功能。甚至可以为特定或特殊需求创建特别的插件。

高级层级

GNU Emacs 是一个免费、开源、可扩展、自我文档化的文本编辑器,使用C语言和自有的Lisp编程语言(Emacs Lisp)编写。它由理查德·斯托尔曼盖·L·斯蒂尔开发,最初版本发布于 1985 年,现已移植到所有主要操作系统。作为 GNU 项目的一部分开发,Emacs 的使用通过编写Emacs Lisp插件得以扩展,这些插件可以在官方 Fedora 仓库中找到。它还通过AppImage包(沙箱应用)在 Fedora 上运行:

图 1.17 – GNU Emacs 编辑器

图 1.17 – GNU Emacs 编辑器

4. 处理正则表达式

bash 命令解释器有多种方法来处理正则表达式,通过扩展命令行的功能来实现。

基础层级

在基础层面,使用模式匹配(通配符)、循环和退出代码是非常重要的。使用通配符,处理多个文件变得更加容易。通过使用元字符作为通配符,可以扩展匹配搜索的文件名和路径,命令一次性作用于一组文件。

下表展示了在基本用法中作为通配符使用的字符:

字符

|

描述

|

*

|

匹配任意数量的字符——例如,列出目录中所有.txt文件:

$ ls *.txt

|

?

|

匹配任意单个字符——例如,列出以compar字符串开头的.sh文件:

$ ls compar*.sh

compare.sh

|

[ ]

|

匹配方括号中任意一个字符——例如,列出一个目录中的文件,并筛选出以字母开头的文件:

$ ls | grep [1]

ompare.sh

onkyrc

abkey

abkey.pub

|

{ }

|

包含一个以逗号分隔的字符串列表或序列。如果是这样,使用双点语法。一个例子是创建五个空文件并列出它们:

$ touch file{1..5}
$ ls file?
file1  file2  file3  file4  file5

|

~

|

匹配当前用户的主目录——例如,列出用户主目录中的Downloads目录:

$ ls ~/Downloads/

|

$

|

将字符串表示为变量——例如,打印用户的PATH变量到屏幕上:

$ echo $PATH
/home/user/.local/bin:/home/user/bin::/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin

|

循环帮助我们轻松地执行重复任务。在 Bash 中,for循环由以下语法构成:

for <variable> in <list>
do
command <variable>
done

你可以给这些循环添加一个条件,以便根据情况执行不同的操作:

if <condition>;
   then
       <statement 1>
       ...
       <statement n>
   else
       <statement alternative>
exit command shown in the following code, with an optional argument between 0 and 255, represents an exit code:

[user@workstation ~]$ cat test.sh

!/bin/bash

echo "Hello, I'm a test"

exit 0

[user@workstation ~]$ ./test.sh

你好,我是一个测试

[user@workstation ~]$ echo $?

0 表示脚本成功运行且没有错误;任何其他值表示错误输出。

中级层次

正则表达式提供了一种模式匹配机制,帮助你搜索特定内容。greplessvim命令在使用时都支持正则表达式。大多数编程语言也支持正则表达式,尽管每种语言的语法可能有所不同。如前所述,这些命令可以串联并转换为搜索更复杂的结构。

在本节的末尾,你将找到一个引导示例,能够更好地说明这一层次。

高级层次

使用正则表达式和模式优化地编写脚本。

在脚本中处理正则表达式时要小心,因为链式命令使用一定量的内存和 CPU 处理能力,这不应低估。电池和功能测试阶段应该仔细规划,绝对不能在生产服务器上进行;此时,拥有一台 Linux 工作站来管理我们的服务器非常有利。我们可以基于本地虚拟机在实例中重建生产环境,并进行超级脚本的首次功能测试。

这就是 方式。”

耐心一点, 年轻的帕达万。”

这看起来可能是一个非常费力的过程,但并非如此。很多时候,它取决于实践,结合前面提到的特点,从而培养必要的技能。这不是一天的练习就能完成的——它需要努力和专注,因此你必须一步一步地走下去。

接下来,我们将进行一个引导练习,我将一步一步展示如何从一个简单的命令走向一个有明确目的的链式命令。这将向你展示如何根据需要结合下面显示的工具。

引导示例——释放文件系统中的空间

描述:因为一台托管服务器的根文件系统显示磁盘使用率为 92%,所以我们被分配了一个工单。我们需要确定多个责任并提供证据,以便记录问题并解决它,从而关闭工单。

分析:由于服务器的文件系统中没有单独的目录,因此需要确定哪些目录使用了最多的磁盘空间,并识别与之相关的应用程序或服务。

解决方案

  1. 作为 root 用户,切换到root目录(/)并列出可用的目录:

    [root@workstation ~]# cd /
    [root@workstation /]# ls
    afs  bin  boot  dev  etc  home  lib  lib64  lost+found  media  
    mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    
    1. 运行以下命令列出目录。使用-l参数运行长列表,仅列出目录:
    [root@workstation /]# ls -l | grep ^d
    dr-xr-xr-x.   1 root root    0 Aug  9 08:27 afs
    dr-xr-xr-x.   6 root root 4096 Nov 22 13:12 boot
    drwxr-xr-x.  21 root root 4000 Nov 22 13:12 dev
    drwxr-xr-x.   1 root root 5186 Nov 22 13:12 etc
    drwxr-xr-x.   1 root root   18 Nov 21 21:41 home
    drwx------.   1 root root    0 Nov  5 02:18 lost+found
    drwxr-xr-x.   1 root root    0 Aug  9 08:27 media
    drwxr-xr-x.   1 root root    0 Aug  9 08:27 mnt
    drwxr-xr-x.   1 root root    0 Aug  9 08:27 opt
    dr-xr-xr-x. 329 root root    0 Nov 22 13:12 proc
    dr-xr-x---.   1 root root  188 Nov 22 01:45 root
    drwxr-xr-x.  58 root root 1580 Nov 22 13:14 run
    drwxr-xr-x.   1 root root    0 Aug  9 08:27 srv
    dr-xr-xr-x.  13 root root    0 Nov 22 13:12 sys
    drwxrwxrwt.  20 root root  460 Nov 23 00:06 tmp
    drwxr-xr-x.   1 root root  168 Nov  5 02:4.0 usr
    drwxr-xr-x.   1 root root  200 Nov  5 03:15 var
    
    1. 使用awk选择仅列出目录的名称(第 9 列):
    [root@workstation /]# ls -l | grep ^d | awk '{ print $9 }'
    afs
    boot
    dev
    etc
    home
    lost+found
    media
    mnt
    opt
    proc
    root
    run
    srv
    sys
    tmp
    usr
    var
    
    1. 使用xargsdu命令确定每个目录使用的磁盘空间:
    [root@workstation /]# ls -l | grep ^d | awk '{ print $9 }' | xargs du -sk
    0    afs
    293680    boot
    0    dev
    33212    etc
    176728    home
    0    lost+found
    0    media
    0    mnt
    0    opt
    du: cannot read directory 'proc/3945/task/3945/net': Invalid argument
    du: cannot read directory 'proc/3945/net': Invalid argument
    du: cannot read directory 'proc/3946/task/3946/net': Invalid argument
    du: cannot read directory 'proc/3946/net': Invalid argument
    du: cannot access 'proc/7762/task/7762/fd/3': No such file or directory
    du: cannot access 'proc/7762/task/7762/fdinfo/3': No such file or directory
    du: cannot access 'proc/7762/fd/3': No such file or directory
    du: cannot access 'proc/7762/fdinfo/3': No such file or directory
    0    proc
    32    root
    du: cannot access 'run/user/1000/doc': Permission denied
    1632  run
    0    srv
    0    sys
    8    tmp
    8371056    usr
    6576996    var
    
    1. 为避免混淆,将标准错误输出(stderr)发送到/dev/null
    [root@workstation /]# ls -l | grep ^d | awk '{ print $9 }' | \
    > xargs du -sk 2> /dev/null
    0    afs
    293680    boot
    0    dev
    33212    etc
    176728    home
    0    lost+found
    0    media
    0    mnt
    0    opt
    0    proc
    32    root
    1632    run
    0    srv
    0    sys
    8    tmp
    8371056    usr
    6576996    var
    
    1. 对结果进行排序:
    [root@workstation /]# ls -l | grep ^d | awk '{ print $9 }' | \
    > xargs du -sk 2> /dev/null  | sort -n
    0    afs
    0    dev
    0    lost+found
    0    media
    0    mnt
    0    opt
    0    proc
    0    srv
    0    sys
    8    tmp
    32    root
    1632    run
    33212    etc
    176728    home
    293680    boot
    6576996    var
    8371056    usr
    
    1. 丢弃磁盘空间使用最少的目录,仅保留前 5个:
    [root@workstation /]# ls -l | grep ^d | awk '{ print $9 }' | \
    > xargs du -sk 2> /dev/null  | sort -n | tail -5
    33212    etc
    176728    home
    293680    boot
    6576996    var
    8371056    usr
    
    1. 现在我们已经找到了磁盘使用量最高的前 5个目录,我们将仅处理这个顺序,并将其作为证据:
    [root@workstation /]# ls -l | grep ^d | awk '{ print $9 }' | \
    > xargs du -sk 2> /dev/null  | sort -n | tail -5 \
    > awk '{ print $2 }' | xargs du -sh
    33M        etc
    173M    home
    287M    boot
    6.3G    var
    8.0G    usr
    

应对每个前 5个目录执行相同的步骤,以便我们可以找到占用最多磁盘空间的子目录,它是导致问题的根源。找出哪个服务负责处理释放问题取决于目录的不同。

既然这些概念已经清晰,我们可以开始考虑如何为系统管理目的安装我们的工作站了。不过,在此之前,我们应该花点时间选择我们想要使用的桌面环境。

桌面环境

Fedora 的默认桌面环境是GNOME,但它为我们提供了使用其他桌面环境的选择,无论是轻量级的,还是具有特殊功能的桌面环境,例如那些使用不同图形引擎和专门库或专注于性能的桌面环境。这些替代环境由 Fedora 项目以 Spin 发行版 的形式提供。你可以下载基于 Fedora 的预配置桌面环境的 Spin

有关 Fedora 的替代桌面环境的更多信息,请参考 Fedora Spinsspins.fedoraproject.org/

到此为止,我们已经结束了 第一章。让我们快速回顾一下我们学到的内容。

总结

在本章中,我们简要回顾了 Unix 操作系统的历史,这帮助我们了解了 Linux 及其发行版的起源与发展。除了让我们了解负责开发我们将用作系统管理工作站的发行版的项目如何运作外,它还帮助我们学习如何在日常工作中参与其中,并改进发行版。

在下一章中,我们将学习一些最佳实践和技巧,这些将帮助确保我们有一个良好的安装环境,从而帮助我们开展工作。

进一步阅读

若要了解本章涉及的更多内容,请访问以下链接:



第二部分:工作站配置

在本部分中,您将学习如何将计算机配置为系统管理员的工作站——从操作系统和工具的安装最佳实践到配置和优化资源(如存储和网络)。

本部分包含以下章节:

  • 第二章安装最佳实践

  • 第三章调整桌面环境

  • 第四章优化存储使用

  • 第五章网络与连接性

第四章:安装最佳实践

现在我们已经对 Linux 发行版的历史和发展有了非常完整的了解,接下来我们来看一下安装操作系统OS)的建议和最佳实践,安装到将作为我们工作站的计算机上。在这种情况下,最理想的是选择一台具备良好内存和 CPU 资源的便携式计算机,因为如果我们能在其上虚拟化,它将对我们进行功能测试有很大帮助。

本章将覆盖的主题如下:

  • 创建启动介质

  • 本地存储分区

  • 第一次启动

  • 包管理

开始吧!

技术要求

根据Fedora 文档getfedora.org/en/workstation/download/),创建 Fedora Linux 启动介质需要一个 2 GB 的 USB 闪存驱动器。安装 Fedora Linux 至少需要 20 GB 的本地存储和 2 GB 的 RAM;推荐的配置是将两者的容量加倍。

访问getfedora.org获取要安装的Fedora 版本镜像。Fedora 镜像是混合 ISO 格式,因此你可以在安装之前以实时模式测试它们。

在本章中,我们将介绍安装工作站的最佳实践,从性能和灵活性的角度,帮助我们管理 Linux 系统的应用程序。

为了创建启动介质,我们将使用 Fedora Linux。不过,创建启动介质的操作可以在任何 Linux 发行版上进行,最好是基于 rpm 的,也可以在 Windows 或 Mac 系统上进行。

在我们的安装中,我们将选择Fedora Workstation镜像作为最佳版本,因为它是一个经过精细打磨且易于在笔记本电脑和台式机上使用的操作系统,拥有适用于开发人员和各类用户的完整工具集。下载相应的镜像后,我们将创建启动介质,创建方法有多种。

创建启动介质

有多种方法可以创建启动介质,从 dd 命令到 syslinux 启动加载程序到设备的应用程序。此过程基于 Fedora Linux 构建一个启动加载程序,但它与镜像中的启动加载程序不同,因此启动介质的构建与镜像不一致,可能会导致启动错误。

dnfyum 命令作为 root 用户:

[root@host ~]# dnf install mediawriter

注意

你可以在github.com/FedoraQt/MediaWriter下载 Fedora 媒体写入工具的其他平台版本。

让我们看看创建启动介质的过程是如何进行的。

Fedora 媒体写入工具

该方法会擦除 USB 闪存驱动器上现有的数据,所以如果需要,务必先备份数据。

注意

获取有关替代启动介质创建方法的更多信息,请访问Fedora Docs创建和使用实时安装镜像,网址:docs.fedoraproject.org/en-US/quick-docs/creating-and-using-a-live-installation-image/#proc_creating-and-using-live-usb

按照以下步骤使用 Fedora Media Writer 创建启动介质:

  1. 应用程序菜单中选择Fedora Media Writer。如果尚未下载镜像,该工具会提供下载选项:

图 2.1 – Fedora Media Writer

图 2.1 – Fedora Media Writer

  1. 点击下一步按钮,选择要安装的版本:

图 2.2 – Fedora Media Writer – 选择 Fedora 发布版

图 2.2 – Fedora Media Writer – 选择 Fedora 发布版

如果选择下载镜像,请启用官方版本单选按钮,并从下拉菜单中选择Fedora Workstation

  1. 点击下一步按钮,在下一个屏幕上,选择写入发布版/版本、硬件架构和要安装的设备。你可以选择在写入设备后删除已下载的镜像:

图 2.3 – Fedora Media Writer – 写入选项

图 2.3 – Fedora Media Writer – 写入选项

  1. 点击下载并写入按钮,等待其下载镜像并写入设备。

当镜像下载完成后,开始将镜像写入设备:

图 2.4 – Fedora Media Writer – 写入镜像

图 2.4 – Fedora Media Writer – 写入镜像

镜像写入完成后,开始检查已写入设备的数据:

图 2.5 – Fedora Media Writer – 验证已写入的数据

图 2.5 – Fedora Media Writer – 验证已写入的数据

  1. 当数据验证完成后,点击完成按钮,移除启动介质设备。

图 2.6 – Fedora Media Writer – 写入完成

图 2.6 – Fedora Media Writer – 写入完成

将镜像写入启动介质后,现在是时候在计算机上安装并开始安装过程。

启动

如前所述,Fedora Linux 镜像是混合型的,这意味着操作系统可以从启动介质启动,进行功能测试,然后从那里执行完整的发行版安装。为此,请按照以下步骤进行:

  1. 重启计算机并插入创建的启动介质,开始安装 Fedora Linux 镜像。启动屏幕会显示:

图 2.7 – 启动介质屏幕

图 2.7 – 启动介质屏幕

  1. 选择测试此介质并启动 Fedora-Workstation-Live 37选项;这对于确定在安装工作站时启动介质是否存在一致性问题非常重要。

  2. 测试结束时,桌面会显示欢迎使用 Fedora屏幕,我们可以选择测试Fedora Live发行版或将其安装到硬盘设备上:

图 2.8 – Fedora Live – 欢迎屏幕

图 2.8 – Fedora Live – 欢迎屏幕

  1. 选择安装到硬盘选项。在下一个屏幕上,根据你的偏好选择安装时使用的语言

图 2.9 – Fedora 安装 – 选择语言

图 2.9 – Fedora 安装 – 选择语言

点击继续按钮。

  1. 安装摘要屏幕出现。选择键盘映射,设置日期和时间以及当前位置。

点击安装目标按钮,选择系统应安装的设备:

图 2.10 – Fedora 安装摘要

图 2.10 – Fedora 安装摘要

  1. 存储配置选项设置为自定义

图 2.11 – Fedora 安装目标

图 2.11 – Fedora 安装目标

  1. 完成后点击完成按钮。

分区是存储管理的基本最佳实践之一。它意味着将本地存储分为多个小单元,按需划分,以便分开存储在每个部分的数据,除了能更好地组织数据外,还能避免整个操作系统崩溃的可能性。在没有分区的极端情况下,存储可能没有足够的空闲空间来继续操作。

由于最好在操作系统安装时进行此组织步骤,让我们利用这个步骤进行深入分析。开始吧。

本地存储分区

安装向导包括一个快捷方式,用于创建标准的分区。这在像安装工作站这样的情况下非常有用,但在其他特定用途的服务器情况下则不适用。我们将在后续章节讨论这些情况,并扩展一些基本的存储管理概念。

我们从向导提供的标准分区开始,并添加一些额外的挂载点,这将有助于我们的系统管理任务。

按照以下步骤对本地存储进行分区:

  1. 手动分区屏幕允许你选择格式方案,保持为Btrfs,并通过点击点击这里自动创建它们链接来创建基本挂载点。

图 2.12 – Fedora 手动分区

图 2.12 – Fedora 手动分区

应该创建一个挂载点基础,文件系统包括/home/目录,以及/boot目录和BIOS boot,并将它们挂载到硬盘设备的前两个物理分区上。

  1. 点击加号[+]按钮,以添加另一个挂载在/var/lib/libvirt/images目录上的文件系统;该目录将成为后续章节中将配置的客人虚拟机VMs)的位置。

  2. 更改分配的磁盘空间,使/home/目录大约为 50 GiB。剩余的硬盘空间应分配给/var/lib/libvirt/images目录。

例如,对于一个 500 GiB 的硬盘设备,分配 100 GiB 给/home/目录后,剩余的 400 GiB 存储空间被分配给/var/lib/libvirt/images目录。

图 2.13 – Fedora 手动分区

图 2.13 – Fedora 手动分区

点击完成按钮。

  1. 安装总结屏幕显示没有遗漏的配置项,并启用开始安装按钮:

图 2.14 – Fedora 安装总结

图 2.14 – Fedora 安装总结

点击开始安装按钮,等待安装完成。

图 2.15 – Fedora 安装进度

图 2.15 – Fedora 安装进度

  1. 点击完成安装按钮。

  2. 重启实时媒体并移除可启动媒体设备。

现在我们已经在计算机上安装了操作系统,让我们在第一次启动时完成配置。

第一次启动

操作系统已安装到计算机上,但访问的用户配置仍然缺失,以及一些首次启动时可能进行的个性化设置。让我们添加一些个性化配置,完成该发行版的安装。

按照以下步骤完成配置:

  1. 重启系统时,设置屏幕会显示:

图 2.16 – Fedora 欢迎界面

图 2.16 – Fedora 欢迎界面

点击开始设置按钮。

  1. 隐私设置屏幕上,如果您同意,请激活位置服务自动问题报告开关。

图 2.17 – Fedora 欢迎界面 – 隐私

图 2.17 – Fedora 欢迎界面 – 隐私

启用位置服务允许一些应用程序,如地图天气,根据您当前的位置提供本地信息。在考虑激活开关之前,请阅读隐私政策。

启用自动问题报告会将故障的技术报告发送给Fedora 项目。在发送之前,个人信息会被删除。操作系统会收集这些信息。

点击下一步按钮。

  1. 在下一个屏幕上,通过点击启用第三方 软件库按钮来启用第三方软件库。

图 2.18 – Fedora 欢迎界面 – 第三方软件库

图 2.18 – Fedora 欢迎界面 – 第三方软件库

第三方仓库提供了从选定外部来源访问额外软件的途径,如流行的应用程序或某些驱动程序,包括专有软件。

点击下一步按钮。

  1. 下一个屏幕让你通过选择服务并登录账户,连接你的GoogleNextcloudMicrosoft 在线服务账户。

图 2.19 – Fedora 欢迎 – 在线账户

图 2.19 – Fedora 欢迎 – 在线账户

提示

这一步是可选的,不会干扰操作系统的活动。

要跳过此步骤,请点击跳过按钮。

  1. 在下一个屏幕上创建你的登录账户:

图 2.20 – Fedora 欢迎 – 关于你

图 2.20 – Fedora 欢迎 – 关于你

完成后,点击下一步按钮。

  1. 然后,为你的登录账户创建一个强密码;系统会指示最低可接受的 密码强度

图 2.21 – Fedora 欢迎 – 密码

图 2.21 – Fedora 欢迎 – 密码

  1. 完成后,点击下一步按钮。设置现已完成。

图 2.22 – Fedora 欢迎 – 设置完成

图 2.22 – Fedora 欢迎 – 设置完成

  1. 点击开始使用 Fedora Linux按钮,工作区桌面将显示。

图 2.23 – Fedora Linux 37 Workstation GNOME 桌面

图 2.23 – Fedora Linux 37 Workstation GNOME 桌面

在控制台工作时间较长时,保护视力至关重要,尽量避免造成永久性损伤。GNOME 提供的选项之一是配置暗黑模式,它有助于避免这些视觉问题。

点击右上角的按钮,然后点击暗黑模式按钮以选择暗黑模式。

图 2.24 – Fedora 桌面 – 暗黑模式

图 2.24 – Fedora 桌面 – 暗黑模式

重要提示

给你的眼睛休息一下。如果你长时间使用电脑或专注于某件事,有时会忘记眨眼,眼睛可能会感到疲劳。尝试使用 20-20-20 规则——每 20 分钟,看向前方约 20 英尺的地方,保持 20 秒。这一简单的练习有助于减轻眼睛疲劳。

如需更多关于如何预防视力丧失的建议,请参考视力健康倡议VHI),访问 www.cdc.gov/visionhealth/risk/tips.htm/

安装和基本配置完成后,我们可以开始安装那些将帮助我们完成日常任务的实用工具。

为此,必须确保我们的电脑连接到互联网。与存储类似,在后续章节中,我们将回顾网络配置的基本概念,以优化其运行。此时,仅需知道系统网络可用,无论是有线或无线形式,都能下载和安装操作系统的包和更新。

现在,让我们学习如何更新操作系统并下载提供所需工具的额外软件包。

包管理

Fedora Linux 实时安装介质构建基于 kickstart(文本)文件,具体内容取决于镜像的版本,所包含的软件包也会有所不同。

Fedora 工作站版本包括以下软件包组:

  • 常见网络管理器子模块

  • 容器管理

  • 核心

  • Fedora 工作站产品核心

  • Firefox 浏览器

  • 字体

  • GNOME

  • 客户端桌面代理

  • 硬件支持

  • LibreOffice

  • 多媒体

  • 打印支持

  • base-x

注意

有关Fedora 编译版的更多信息,请参考fedora-kickstarts,网址为 pagure.io/fedora-kickstarts/

要查看已安装的软件包,请执行以下步骤:

  1. 打开活动概览窗口,并从底部图标栏选择软件

图 2.25 – 活动概览

图 2.25 – 活动概览

  1. 点击已安装标签:

图 2.26 – 已安装软件

图 2.26 – 已安装软件

注意

获取发行版中包含的详细软件包列表,请访问我们的 GitHub 仓库,链接为 github.com/PacktPublishing/Fedora-Linux-System-Administration/blob/main/chapter2/fedora-37-packages.txt

  1. 更新标签表明有软件包需要更新。点击更新标签:

图 2.27 – 软件更新

图 2.27 – 软件更新

重要

由于这是第一次启动,更新所有 操作系统软件包 非常重要。

有关软件包更新内容的详细信息,请参阅 Fedora 更新系统,网址为 bodhi.fedoraproject.org/releases/F38

  1. 点击重启 & 更新按钮。

图 2.28 – 重启 & 安装更新

图 2.28 – 重启 & 安装更新

  1. 点击重启 & 安装按钮。

图 2.29 – 安装更新中…

图 2.29 – 安装更新中…

等待安装更新,当计算机重启后,登录并验证在软件 | 更新中,确认更新已是最新版本。

图 2.30 – 软件更新

图 2.30 – 软件更新

关闭此窗口以继续安装额外的软件包。

额外的软件包选择

Fedora Linux 在其软件仓库中包含了许多可以简化系统管理日常任务的工具,但其中一些工具并未包含在系统安装中,因此需要单独安装。要在 Fedora Linux 上安装软件包,可以使用基于 rpm 包 的包管理器(在终端使用 dnf 命令)。

注意

有关在 Fedora Linux 中安装软件包的更多信息,请参阅软件包管理系统docs.fedoraproject.org/en-US/quick-docs/package-management/

以下是一些推荐的工具:

  • 开发

    • gcc:一个 GNU 项目的CC++编译器

    • git:一个分布式版本控制系统

    • make:一个 GNU Make 实用工具,用于维护程序组

  • 笔记本电池电量

    • tlp:一款应用节能设置和控制电池护理功能的工具
  • 网络扫描器

    • nmap:一款网络探索工具和安全/端口扫描工具
  • 终端模拟器

    • terminator:在一个窗口中存储和运行多个 GNOME 终端
  • 虚拟化

    • genisoimage:一个生成 ISO 9660/Joliet/HFS 混合文件系统的程序

    • libguestfs:一个用于访问和修改虚拟机磁盘映像的工具

    • qemu-kvm:一个 QEMU PC 系统模拟器

    • virt-install:一个使用libvirt虚拟化管理库创建新的KVMXenLinux 容器客户机的工具

    • virt-manager:一个用于管理libvirt虚拟机的图形化工具

    • virt-viewer:一个显示虚拟机图形控制台的工具

打开 GNOME 终端以安装额外的软件包。然后,打开活动概览窗口,选择终端

图 2.31 – GNOME 终端

图 2.31 – GNOME 终端

使用sudo命令切换到root用户并清除所有仓库元数据:

[user@workstation ~]$ sudo -i
[sudo] password for user: [password]
[root@workstation ~]# dnf clean all
68 files removed

注意

清除元数据以强制包管理器刷新它是可选的。我一直认为这是一个良好的实践。

使用dnf命令安装额外的软件包:

[root@workstation ~]# dnf install gcc make libgcc git \
> qemu-kvm virt-manager virt-viewer libguestfs \
> virt-install genisoimage terminator nmap tlp

在后续章节中,我们将安装更多工具,以完成一些特定任务。

一旦系统升级并安装了额外的软件包,每个人都可以享受使用这些工具的体验。然而,你可能会发现某些工具或软件包可以通过增加一些功能或特性来得到增强。你可以将你的提议提交给 Fedora 项目,进行功能增强评估。

我们的工作站安装和基本配置已经完成,接下来让我们总结一下我们学到的内容。

总结

在本章中,我们回顾了安装 Fedora 到计算机上的最佳实践和技巧,包括基本的内存分区设置,以及我们工作站的初步配置步骤。

在下一章中,我们将学习如何通过合适的工具定制我们的工作站,以执行 Linux 系统管理。

进一步阅读

要了解本章涵盖的主题,可以参考以下资源:

第五章:调优桌面环境

在我们的计算机上安装操作系统并完成必要的配置后,我们可以继续定制工作站,以优化流程。但此时,作为桌面环境的用户,感受到舒适的使用体验非常重要。在本章中,我们将逐步探讨如何定制桌面环境;你可以根据个人喜好省略或添加任何组件。我们将使用的桌面环境是GNOME,但大部分定制操作(除了 GNOME 插件)在任何桌面环境中都可以完成。

本章涵盖的定制组件如下:

  • 初始系统调优

  • 自定义面板和任务栏

  • 通过小部件简化任务

  • 配备快捷应用的任务栏

技术要求

本章中的练习需要你有一台安装了Fedora Workstation的个人计算机,版本为 37,这是本书的基础版本。如果你没有安装该操作系统,可以参考前一章,我提供了最佳安装实践。

让我们从资源优化开始调优桌面环境。

初始系统调优

调优 Linux 系统涉及其多个组件。因此,我们将从桌面调优开始,逐步进行,以尽可能优化我们的工作站。和往常一样,在这种情况下,最需要关注的是计算机的内存和 CPU 使用情况。

根据 Fedora 的官方文档(docs.fedoraproject.org/en-US/fedora/latest/release-notes/welcome/Hardware_Overview/),我们硬件上必须具备的最低系统要求如下:

  • 2 GHz 双核处理器或更快

  • 2 GB 系统内存

根据多个资源使用基准测试网站,如 diffen.com(www.diffen.com/difference/GNOME_vs_KDE)、webdock.io(webdock.io/en/docs/how-guides/desktop-environments/overview-of-desktop-environments)和 pcgamebenchmark.com(www.pcgamebenchmark.com/find-the-gnome-system-requirements),GNOME 桌面有以下规格:

  • 800 MHz CPU 性能(1 GHz 以获得最佳性能)

  • 512 MB 随机访问内存RAM)(1 GB 以获得最佳性能)

如果我们系统内存有限,一个有帮助的调优是调整swappiness值。

0100,是匿名页面文件页面之间的比例。

调节 swappiness 值

swappiness 值决定了写入硬盘虚拟内存(交换空间)中的数据量,这可能会导致系统变慢。

默认情况下,这个值是60,你可以通过从终端运行以下命令来验证它:

[root@workstation ~]# cat /proc/sys/vm/swappiness
/etc/sysctl.conf file by adding a lower value to this parameter. As a recommendation, add a comment of the change made as a backup:

[root@workstation ~]# cat /etc/sysctl.conf

...省略输出...

默认值是 60,减小该参数的值

vm.swappiness=10


 After saving the file, reboot the computer and confirm the changed value. With this, less information should write to the virtual drive.
With this basic tuning done, let’s get a better experience with the use of our desktop resources. So now, let’s go ahead and customize it to have the tools at hand and perform better day-to-day activities.
Tuning the desktop experience
In the GNOME desktop, in the upper-left corner, you will find the **Activities** menu button. By clicking it, access to the programs and system settings is available. Pressing the *Super* key also opens the **Activities** menu.
From the **Activities** menu, we can search for any program, but GNOME can not only search for software but also for any item that is available, such as locations, contacts, calendar appointments, notes, and so on.
![Figure 3.1 – Searching from the Activities menu](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/3.1.jpg)

Figure 3.1 – Searching from the Activities menu
This is very useful, but it can also mean a resource drain on the performance of the desktop environment. To limit GNOME’s search, click on the icons at the top right and click on the gear to open the configuration window. In the **Search** section, select only the source of the items to search for.
![Figure 3.2 – Settings – Search](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_002.jpg)

Figure 3.2 – Settings – Search
When a file search is enabled, a background program runs to read filenames and generate an index of them. This service runs often to detect new files as they are created. So, disabling file indexing can free up resources.
![Figure 3.3 – Settings – Files disabled](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_003.jpg)

Figure 3.3 – Settings – Files disabled
Note
File indexing in GNOME comes from a service called **tracker**, which can be completely uninstalled to ensure that it does not run in the background. To uninstall it, run the **dnf** **remove** command:
**[root@workstation ~]# dnf** **remove tracker**
More tuning exists under GNOME, but the `dnf` command:

[root@workstation ~]# dnf install gnome-tweaks


 Let’s now take a look at some of the tunings available with the tool.
GNOME Tweak Tool
GNOME Tweak Tool is a new application that contains a variety of customization options beyond what’s available in the system settings ([`wiki.gnome.org/Gnome3CheatSheet`](https://wiki.gnome.org/Gnome3CheatSheet)).
![Figure 3.4 – GNOME Tweaks](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_004.jpg)

Figure 3.4 – GNOME Tweaks
These tunings change the behavior or use of different desktop elements, such as the following:

*   **Appearance**
*   **Fonts**
*   **Keyboard &** **Mouse**
*   **Startup Applications**
*   **Top Bar**
*   **Window Titlebars**
*   **Windows**

A very useful tuning when our workstation is not portable is to disable the **suspension** of the computer when the lid closes. To disable this behavior, turn off the **Suspend when laptop lid is closed** switch in the **General** section of **Tweaks**.
![Figure 3.5 – GNOME Tweaks – General](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_005.jpg)

Figure 3.5 – GNOME Tweaks – General
Note
Suspension is useful when moving around because it *saves* battery power. Please consider whether this option is useful for your day-to-day tasks.
Another tuning we should keep an eye on is the **system startup** applications, which are set in the **Startup Applications** section of **Tweaks**.
![Figure 3.6 – GNOME Tweaks – Startup Applications](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_006.jpg)

Figure 3.6 – GNOME Tweaks – Startup Applications
To add a startup application, click on the plus sign (**+**) and add the program.
![Figure 3.7 – GNOME Tweaks – Adding Startup Applications](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_007.jpg)

Figure 3.7 – GNOME Tweaks – Adding Startup Applications
These tunings are the basic ones to optimize the performance of the desktop environment in our workstation. Now, let’s see some customizations to improve the user experience.
GNOME Shell extensions
These customizations come through the available GNOME desktop extensions. In the same way as with Chrome and Firefox browser extensions, **GNOME Shell extensions** enable small changes and modify the way GNOME works.
GNOME shell extensions can be installed with the `dnf install` command, and also from the browser, as follows:

1.  Open the URL [`extensions.gnome.org/`](https://extensions.gnome.org/) in the Firefox web browser.

![Figure 3.8 – GNOME Shell extensions](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/3.8.jpg)

Figure 3.8 – GNOME Shell extensions

1.  Click on the **Click here to install browser extension** link to install the browser extension, add the **GNOME Shell integration** extension, and accept the installation.

![Figure 3.9 – GNOME Shell integration extension installed](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/3.9.jpg)

Figure 3.9 – GNOME Shell integration extension installed

1.  Once the installation is complete, the GNOME icon appears in the browser bar.

![Figure 3.10 – GNOME icon in the browser bar](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_010.jpg)

Figure 3.10 – GNOME icon in the browser bar

1.  Press the *F5* key to refresh the page and install the extensions.

![Figure 3.11 – GNOME Extensions page](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/3.11.jpg)

Figure 3.11 – GNOME Extensions page
Now, let’s go over how to customize each component of our desktop environment, starting with the panel and the taskbar.
Customizing the panel and the taskbar
One of the functionalities that everybody misses in the desktop environment is having a menu that allows organized access to applications.
In the GNOME extensions, there are different menu options that help with this need.
In my case, I like to use the **ArcMenu extension**, as it provides a clean and clear organization of applications, and it adds some shortcuts to directories and system settings.
In the search bar, type `ArcMenu`, then click on the extension name, and toggle the switch to **ON** on the extension page ([`extensions.gnome.org/extension/3628/arcmenu/`](https://extensions.gnome.org/extension/3628/arcmenu/)). Click on the **Install** button.
![Figure 3.12 – ArcMenu extension installation](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_012.jpg)

Figure 3.12 – ArcMenu extension installation
The **ArcMenu** icon appears in the upper-left corner instead of the **Activities** menu. Click on it, and the menu opens.
![Figure 3.13 – ArcMenu extension](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_013.jpg)

Figure 3.13 – ArcMenu extension
Click on the ArcMenu **Settings** button to change the appearance and organization of the applications, as well as to add or remove shortcuts.
![Figure 3.14 – ArcMenu settings](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_014.jpg)

Figure 3.14 – ArcMenu settings
Instead of the *ArcMenu* icon, I prefer the *Fedora* icon to be shown. To do this, click on the **Menu Button** tab and click on the **Browse** button in the **Choose a new icon** section. Then, in the **Distro Icons** tab, choose the **Fedora** icon.
![Figure 3.15 – Choosing the Fedora icon](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_015.jpg)

Figure 3.15 – Choosing the Fedora icon
Close the window, and now the Fedora icon shows up as the menu in the top-left corner.
![Figure 3.16 – ArcMenu with the Fedora icon](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_016.jpg)

Figure 3.16 – ArcMenu with the Fedora icon
The extension installation procedure is the same in all cases. Let’s now look at the extensions that will help us to improve the taskbar.
The taskbar
In the taskbar, the best practices include shortcuts to the most used applications, as well as to the general information and system settings.
On the left side, next to the menu, add the **Frippery Panel Favorites** extension ([`extensions.gnome.org/extension/4/panel-favorites/`](https://extensions.gnome.org/extension/4/panel-favorites/)).
![Figure 3.17 – Frippery Panel Favorites](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_017.jpg)

Figure 3.17 – Frippery Panel Favorites
To add or remove favorites, from the menu, click on the **Activities Overview** icon and drag and drop the application icons to the bottom bar.
![Figure 3.18 – Favorites applications](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/3.18.jpg)

Figure 3.18 – Favorites applications
To have local weather information, add the **OpenWeather** extension ([`extensions.gnome.org/extension/750/openweather/`](https://extensions.gnome.org/extension/750/openweather/)) next to the clock in the center of the taskbar.
![Figure 3.19 – OpenWeather extension](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_019.jpg)

Figure 3.19 – OpenWeather extension
To change the location, click on the *settings icon* button in the lower-right corner, and in the **Locations** tab of the **Settings** window, click on the **Add** button to find the location and add it.
![Figure 3.20 – OpenWeather Settings](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_020.jpg)

Figure 3.20 – OpenWeather Settings
The extensions that I recommend for the upper-right corner are as follows:

*   **Clipboard Indicator** – Saves the text copied for reuse:

[`extensions.gnome.org/extension/779/clipboard-indicator/`](https://extensions.gnome.org/extension/779/clipboard-indicator/)

*   **Extension List** – Provides access to the extension’s configuration:

[`extensions.gnome.org/extension/3088/extension-list/`](https://extensions.gnome.org/extension/3088/extension-list/)

*   **Hide Keyboard Layout** – Hides the keyboard layout configuration:

[`extensions.gnome.org/extension/2848/hide-keyboard-layout/`](https://extensions.gnome.org/extension/2848/hide-keyboard-layout/)

*   **Section Todo List** – Adds a simple to-do list:

[`extensions.gnome.org/extension/1104/section-todo-list/`](https://extensions.gnome.org/extension/1104/section-todo-list/)

*   **Removable Drive Menu** – When inserting a removable disk, it adds a quick access to unmount it:

[`extensions.gnome.org/extension/7/removable-drive-menu/`](https://extensions.gnome.org/extension/7/removable-drive-menu/)
In the case of a portable computer, the **Battery Time** extension ([`extensions.gnome.org/extension/5425/battery-time/`](https://extensions.gnome.org/extension/5425/battery-time/)) indicates the remaining charge time.
![Figure 3.21 – Taskbar – Upper-right corner](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_021.jpg)

Figure 3.21 – Taskbar – Upper-right corner
Other extensions that help improve the look and feel of the desktop environment are as follows:

*   **User Avatar In Quick Settings** – Displays the user’s profile picture in the **Settings** part of the upper-right corner:

[`extensions.gnome.org/extension/5506/user-avatar-in-quick-settings/`](https://extensions.gnome.org/extension/5506/user-avatar-in-quick-settings/)

*   **Transparent Shell** – Makes desktop components (top bar, dash, workspace view, etc.) transparent:

[`extensions.gnome.org/extension/3518/transparent-shell/`](https://extensions.gnome.org/extension/3518/transparent-shell/)
![Figure 3.22 – Customized taskbar](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/3.22.jpg)

Figure 3.22 – Customized taskbar
Fedora provides an extension that displays the distribution logo on the desktop wallpaper. You can install the `gnome-shell-extension-background-logo` package using the following `dnf` command:

[root@workstation ~]# dnf install gnome-shell-extension-background-logo


 Click on the **Extensions List** icon in the upper-right corner, then click on the *gear* icon next to the **Background Logo** extension to customize how the Fedora logo is displayed.
![Figure 3.23 – Background Logo settings](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_023.jpg)

Figure 3.23 – Background Logo settings
Another thing that also improves the look and feel of the desktop environment is the **wallpaper**. One recommendation is to use a neutral background that does not distract our attention. On the [`pixabay.com/`](https://pixabay.com/) page, you can find some free-usage minimalist images to use as a wallpaper.
To change the wallpaper, right-click on the desktop background and select **Change background**, then in the **Appearance** section of the **Settings** window, click on the **+ Add Picture** button to add the downloaded image and apply the change.
![Figure 3.24 – Appearance settings](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_024.jpg)

Figure 3.24 – Appearance settings
Now, our custom desktop environment looks and feels better.
![Figure 3.25 – Customized desktop](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_025.jpg)

Figure 3.25 – Customized desktop
Our desktop environment looks pretty good, but we could improve it even more with other elements. Let’s see how to do that next.
Making tasks easy with widgets
**Widgets** are small applications designed to ease access or provide information on the desktop.
A to-do list shows a simple list of tasks to do, but if you need more details about the tasks or their development, you can use the GNOME **Task Widget** extension ([`extensions.gnome.org/extension/3569/task-widget/`](https://extensions.gnome.org/extension/3569/task-widget/)).
![Figure 3.26 – Task widget extension](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/3.26.jpg)

Figure 3.26 – Task widget extension
The **Task Widget** extension displays tasks near the Calendar widget, providing easy access to the task list along with the ability to merge task lists, group them by due date, mark them as completed for historical record, plus integrate with mail application calendars.
In Linux, widgets are generally used to provide computer performance information. There are various extensions that can be used to present information in the taskbar:

*   **Resource Monitor** – Displays CPU, memory, storage and network usage:

[`extensions.gnome.org/extension/1634/resource-monitor/`](https://extensions.gnome.org/extension/1634/resource-monitor/)
![Figure 3.27 – Resource Monitor extension](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_027.jpg)

Figure 3.27 – Resource Monitor extension

*   **System Monitor** – Shows the use of resources by icons that change color according to their use. By default, it is placed in the center of the taskbar, but its location could be customized:

[`extensions.gnome.org/extension/1064/system-monitor/`](https://extensions.gnome.org/extension/1064/system-monitor/)
![Figure 3.28 – System Monitor extension](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_028.jpg)

Figure 3.28 – System Monitor extension

*   **TopHat** – Displays CPU, memory, and network usage in a small graph:

[`extensions.gnome.org/extension/5219/tophat/`](https://extensions.gnome.org/extension/5219/tophat/)
![Figure 3.29 – TopHat extension](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_029.jpg)

Figure 3.29 – TopHat extension

*   **Vitals** – Displays a summary of CPU, memory, and network usage via icons, but clicking on the extension displays all monitored resources:

[`extensions.gnome.org/extension/1460/vitals/`](https://extensions.gnome.org/extension/1460/vitals/)
![Figure 3.30 – Vitals extension](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_030.jpg)

Figure 3.30 – Vitals extension
I do not use these extensions, as they somehow take away space from the taskbar and, in some cases, the taskbar looks like it has collapsed with so much information. In my case, I prefer an option that keeps my taskbar cleaner and displays all the system information I need on the desktop.
Conky
**Conky** ([`github.com/brndnmtthws/conky`](https://github.com/brndnmtthws/conky)) is a lightweight system monitor, created for the *X Window System*. It displays any kind of information on the desktop.
You can install Conky using the following `dnf` command:

[root@workstation ~]# dnf install conky


 You can browse the internet to find a lot of templates with different designs that could apply to Conky. I like to use those posted at [`www.deviantart.com/`](https://www.deviantart.com/).
![Figure 3.31 – Conky designs at deviantart.com](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/3.31.jpg)

Figure 3.31 – Conky designs at deviantart.com
I found my favorite Conky template, `conky_harmattan`, in a Deviant Art post ([`github.com/zagortenay333/conky_themes/tree/master/conky_harmattan`](https://github.com/zagortenay333/conky_themes/tree/master/conky_harmattan)). It has many themes included, but we’ll use the `conky_themes` repository in the user’s home directory to install it:

[acallejas@workstation ~]$ git clone \


 Note
We’ll talk a bit more about Git in a later chapter.
Change to the `conky_harmattan` directory inside the `conky_themes` directory:

将.harmattan-assets 和.harmattan-themes 目录添加到用户的主目录,保持它们为隐藏目录:

[acallejas@workstation conky_harmattan]$ cp -r .harmattan-assets ~/
[acallejas@workstation conky_harmattan]$ cp -r .harmattan-themes ~/

注意

隐藏目录是那些以句点(.)开头的目录。

我们将在用户主目录中的.config/autostart目录下创建一个文件,以将conky添加到系统启动项中。这样,在启动会话时,conky就会运行并显示在桌面上。

创建~/.config/autostart/start_conky.desktop文件,并输入以下内容:

[acallejas@workstation ~]$ vi .config/autostart/start_conky.desktop
[Desktop Entry]
Type=Application
Exec=/bin/sh -c "$HOME/.config/autostart/start_conky.sh"
Hidden=false
NoDisplay=false
Terminal=false
X-GNOME-Autostart-enabled=true
Name=Conky
GenericName=ConkyStartup
conky startup script as ~/.config/autostart/start_conky.sh with the following content:

[acallejas@workstation ~]$ cat .config/autostart/start_conky.sh

!/bin/bash

killall conky

sleep 10

start_conky.sh 脚本:

[acallejas@workstation ~]$ chmod +x .config/autostart/start_conky.sh

注意

你可以在本书的仓库中找到这两个文件,地址为github.com/PacktPublishing/Fedora-Linux-System-Administration/tree/main/chapter3

conky_harmattan文档所示,在OpenWeatherMapopenweathermap.org/)注册一个私有 API 密钥,以获取当前天气信息。在同一页面上,查找城市代码,方法是搜索该城市并从浏览器的 URL 地址中复制代码:

图 3.32 – OpenWeatherMap 页面

图 3.32 – OpenWeatherMap 页面

通过编辑~/.harmattan-themes/Glass/God-Mode/.conkyrc文件添加这两项数据:

[acallejas@workstation ~]$ vi .harmattan-themes/Glass/God-Mode/.conkyrc
...output omitted...
-------------------------------------
--  API Key
-------------------------------------
template6="d60bb6c3b2b806caf46c43...",
-------------------------------------
--  City ID
-------------------------------------
template7="3530597",
...output omitted...

保存文件并通过从终端运行conky来测试配置:

conky displays on the desktop:
![Figure 3.33 – Conky on the desktop](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/3.33.jpg)

Figure 3.33 – Conky on the desktop
To customize the `conky` location, edit the `~/.harmattan-themes/Glass/God-Mode/.conkyrc` file in the `Windows Specifications` section by changing the `gap_x` and `gap_y` values, depending on the screen resolution:

[acallejas@workstation ~]$ vi .harmattan-themes/Glass/God-Mode/.conkyrc

...省略输出...


-- 窗口规格


gap_x=1900,

gap_y=15,

...省略输出...


 Saving the file displays the change in Conky’s position:
![Figure 3.34 – Conky monitor](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/3.34.jpg)

Figure 3.34 – Conky monitor
To see the transparent window, uncomment the following lines from the `Windows Specifications` section of the `~/.``harmattan-themes/Glass/God-Mode/.conkyrc` file:

own_window_argb_visual=true,

own_window_argb_value=0,


 ![Figure 3.35 – Conky with transparent window](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/3.35.jpg)

Figure 3.35 – Conky with transparent window
To fix the network graph, check that the device name is correct in the `conky.text` section of the `~/.``harmattan-themes/Glass/God-Mode/.conkyrc` file:

${if_existing /proc/net/route enp1s0}

\({voffset -344}\){goto 40}${color5}上传: \({color2}\){upspeed enp1s0}\({color5}\){goto 150}下载: \({color2}\){downspeed enp1s0}

\({voffset 10}\){goto 40}\({upspeedgraph enp1s0 26,80 FFFFFF FFFFFF}\){goto 150}${downspeedgraph enp1s0 26,80 FFFFFF FFFFFF}

\({voffset 9}\){goto 40}${color5}发送: \({color2}\){totalup enp1s0}\({color5}\){goto 150}接收: \({color2}\){totaldown enp1s0}

${else}


 Saving the file displays the graphs in Conky:
![Figure 3.36 – Conky graphs](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/3.36.jpg)

Figure 3.36 – Conky graphs
GNOME extensions include many extra ways of customization. A very popular element is **docks**. Let’s see some of these options in the following section.
Handy applications with docks
**Launchers** or **docks** are a convenient way to access favorite and used applications. GNOME does not provide a dock, but you could get one through GNOME extensions.
The most popular are as follows:

*   **Dash to Dock** – Provides the classic dock with favorite applications. The dock location could change in the extension settings:

[`extensions.gnome.org/extension/307/dash-to-dock/`](https://extensions.gnome.org/extension/307/dash-to-dock/)
![Figure 3.37 – Dash to Dock extension](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_037.jpg)

Figure 3.37 – Dash to Dock extension

*   **Floating Dock** – Provides a dock with a button, which, when pressed, displays favorite applications. The location of the dock could change in the extension settings:

[`extensions.gnome.org/extension/2542/floating-dock/`](https://extensions.gnome.org/extension/2542/floating-dock/)
![Figure 3.38 – Floating Dock extension](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_03_038.jpg)

Figure 3.38 – Floating Dock extension
This customization provides us with a clean desktop for our day-to-day tasks.
These customizations, of course, are not the only ones that exist. A `fedy` ([`github.com/rpmfusion-infra/fedy`](https://github.com/rpmfusion-infra/fedy)), which let us add codecs and third-party or proprietary software from a graphical interface.
Important
**RPM Fusion** is a set of repositories that are not affiliated with or supported by the **Fedora Project**. For more information, refer to *Enabling the RPM Fusion repositories* in the Fedora documentation at [`docs.fedoraproject.org/en-US/quick-docs/setup_rpmfusion/`](https://docs.fedoraproject.org/en-US/quick-docs/setup_rpmfusion/).
Before discussing the functionality of the applications involved, the following chapters provide a brief overview of the basic concepts of storage and networking that are needed for later use.
Summary
This chapter offered an overview of desktop environment customization, from resource tuning to tools and tips for a clean and minimalistic desktop.
We divided it into tuning and customization. Keeping that in mind, we customized only the taskbar and its components, as well as added the `conky` system monitor.
Customizing each of the elements of the desktop environment, besides improving the way it looks and feels, allows us to focus on our tasks. It is well known in Linux-based environments, which provide extreme customization of each component, that this task could be endless.
In my experience as a system administrator, not getting distracted by this kind of customization helps me to focus on my daily work.
In the next chapter, we will review the concepts of basic local storage, as well as how to optimize its use.
Further reading
To learn more about the topics that were covered in this chapter, take a look at the following resources:

*   *What Is Swappiness on Linux? (and How to Change* *It)*: [`www.howtogeek.com/449691/what-is-swapiness-on-linux-and-how-to-change-it/`](https://www.howtogeek.com/449691/what-is-swapiness-on-linux-and-how-to-change-it/)
*   *GNOME* *43*: [`release.gnome.org/43/`](https://release.gnome.org/43/)
*   *GNOME* *Wiki*: [`wiki.gnome.org/`](https://wiki.gnome.org/)
*   *GNOME 3 Cheat* *Sheet*: [`wiki.gnome.org/Gnome3CheatSheet`](https://wiki.gnome.org/Gnome3CheatSheet)
*   *GNOME* *Help*: [`help.gnome.org/`](https://help.gnome.org/)
*   *GNOME Shell* *Extensions*: [`extensions.gnome.org/about/`](https://extensions.gnome.org/about/)



第六章:优化存储使用

在上一章中,我们提到过在操作系统安装过程中,本地存储配置的重要性。本地存储资源的优化对系统的健康至关重要。现在是时候回顾一下帮助优化的基本存储管理概念,如格式、文件系统、大小设置,以及管理工具,如逻辑卷和 Stratis。

在本章中,我们将涵盖以下主要内容:

  • 理解文件格式和文件系统

  • 优化存储空间大小

  • 深入探讨逻辑卷管理器

  • 探索 Stratis 存储

让我们开始吧!

技术要求

要执行本章中涉及的一些配置,您需要有空闲的本地存储空间,如果可能的话,最好在操作系统独立的本地磁盘上。无论空间大小如何,这样都可以完成任务。如果没有额外空间,一些配置可以在安装操作系统的同一磁盘上的本地空闲空间进行,尽管不推荐这样做。

当需要对与操作系统独立的磁盘或备用磁盘进行某些配置时,将会提到这一点。

下表显示了测试环境中的存储配置:

设备 大小 用途:
/``dev/vda 50 GiB 操作系统
/``dev/vdb 50 GiB Btrfs, Stratis
/``dev/vdc 10 GiB Stratis
/``dev/vdd 20 GiB Stratis

理解文件格式和文件系统

一个文件系统使操作系统能够找到它存储在本地磁盘上的数据。这些基本的可寻址存储单元构成一个(通常大小约为 4,096 字节)。为了找到文件的内容,在大量可用存储块中,它使用 inode。一个inode包含有关文件的信息,这些信息存储在特定格式的存储块中,如文件的大小、位置、访问规则(即谁可以读取、写入或执行该文件)等。

从 Fedora Linux 33 开始,Workstation 版本的默认文件系统格式是 xfs 或者甚至是 ext4,Btrfs 是一个写时复制COW)的 Linux 文件系统,具备许多先进的功能。

在 COW 文件系统中,一旦文件被修改,它不会被写回到磁盘上的同一块;它更像是一个重定向。这是为了保护原始数据,并确保将新数据写入未占用的 inode 中。这允许对文件的旧版本进行引用,便于访问,就像在快照中一样,保留系统在某一时刻的状态。

这种行为的缺点是,文件碎片化可能比其他文件系统更快,尽管对于常规的桌面使用来说,这个差异不太可能造成影响。

为了使修改后的文件在文件系统中显示,所有包含对此文件的引用的目录项都会以递归的方式进行更新。而且因为目录本身是一个指向文件的指针,并且具有 inode(因为它指示其中的文件),任何文件修改也会为目录创建一个新的 inode,这个过程会通过文件系统传递到根目录(/)。因此,只要旧目录的引用存在且没有被修改,整个文件系统仍然可以指向一个先前的状态,就像快照一样。

除了廉价且快速的快照,Btrfs 还包含其他功能,如错误检测、容错、恢复、透明压缩和集成的卷管理,并提供多设备存储池、类似 RAID 的功能以及数据和元数据的校验和,所有这些都具有易于使用的管理功能。

让我们创建一个测试用的 Btrfs 文件系统,以展示它的功能。

创建 Btrfs 文件系统

在这个示例的开发过程中,使用与操作系统安装位置不同的本地磁盘。这将从零开始向你介绍 Btrfs 文件系统的功能。请按照以下步骤操作:

  1. 存储管理需要超级用户权限,因此使用sudo命令切换到root用户,并创建用于托管文件系统的/btrfs目录:

    [user@workstation ~]$ sudo -i
    [sudo] password for user: [password]
    [user@workstation ~]$ mkdir /btrfs
    
  2. 确认要使用的设备。确保测试存储设备没有被占用。格式化意味着数据将被完全销毁。

有几个命令可以提供有关存储设备使用情况的信息。建议使用多个命令来验证它们。这些建议包括以下内容:

  • (s)fdisk -l

  • parted -l

  • cat /proc/partitions

  • ls -****l /dev/disk/by-path

  • lsblk -p

图 4.1 – 识别存储设备

图 4.1 – 识别存储设备

在我们的示例中,未使用的存储设备是/dev/vdb,因为它显示没有任何分区。然而,它应该由逻辑卷管理器LVM)管理。运行以下命令以显示由 LVM 管理的设备:

pvs -a

图 4.2 – 丢弃存储设备上的 LVM

图 4.2 – 丢弃存储设备上的 LVM

注意

如果使用分区,需要确认该分区没有被其他文件系统占用,且没有挂载目录。可以使用以下基本命令:fdiskpartedmountdflsblk、和blkid

  1. 创建存储池。使用 Btrfs 时,不需要在存储设备上创建物理分区。首先创建一个存储池,然后创建子卷。这些子卷可以设置配额和快照,因为这些可调整大小的分区可以共享数据块。

运行mkfs.btrfs命令将/dev/vdb格式化为Btrfs文件系统:

mkfs.btrfs -L testbtrfs /dev/vdb

图 4.3 – 创建 Btrfs 存储池

图 4.3 – 创建 Btrfs 存储池

  1. 将存储设备挂载到应该创建子卷的目录。使用mount命令:

    [root@workstation ~]# mount /dev/vdb /btrfs
    

在挂载了 Btrfs 文件系统后,我们来分析其存储空间的使用情况。

  1. 要显示文件系统的结构,请运行以下命令:

    [root@workstation ~]# btrfs filesystem show /btrfs
    Label: 'testbtrfs'  uuid: def9423d-8684-487f-bd11-4829937752b6
    Total devices 1 FS bytes used 144.00KiB
    devid    1 size 50.00GiB used 1.56GiB path /dev/vdb
    
  2. 要测量文件系统上可用的空间,请运行以下命令:

    [root@workstation ~]# btrfs filesystem df /btrfs
    Data, single: total=1.00GiB, used=0.00B
    System, DUP: total=32.00MiB, used=16.00KiB
    Metadata, DUP: total=256.00MiB, used=128.00KiB
    GlobalReserve, single: total=3.50MiB, used=0.00B
    

注意

以下部分将详细解释命令输出的含义。

在 Btrfs 中,子卷类似于包含在主机中的文件系统。一个 Btrfs 文件系统包含一个子卷,但可以存在多个额外的子卷。这些子卷以目录的形式出现在挂载的 Btrfs 文件系统中。子卷可以像任何其他用户可访问的目录一样访问,或者它可以挂载为一个独立的文件系统。

Btrfs 会将每个卷分开。默认情况下,Btrfs 文件系统包含一个子卷。这个子卷被设置为顶级子卷,并且即使没有指定,也会被挂载。随着子卷的创建,它们可以互相嵌套。但顶级子卷除外。因此,每个子卷都有一个可挂载的根目录,并且可以包含多个文件树。这为每个子卷以及主子卷的挂载点设置了一个相对位置。

Btrfs 子卷被认为更像是一个命名空间。

子卷有一些基本布局(包括快照),它们包括以下几种:

  • 扁平 – 子卷是顶级的子级

  • 嵌套 – 子卷位于文件层次结构的任何位置,位于其他子卷下面,而不是顶级子卷下

  • 混合 – 两种基本方案可以混合使用,例如,基本结构采用平面布局,文件系统的某些部分被放置在嵌套的子卷中

让我们创建一个子卷:

  1. 使用btrfs subvolume create命令创建test子卷:

    [root@workstation ~]# btrfs subvolume create /btrfs/test
    Create subvolume '/btrfs/test'
    
  2. 使用btrfs subvolume命令查看子卷信息:

    [root@workstation ~]# btrfs subvolume show /btrfs/test
    

图 4.4 – 子卷信息

图 4.4 – 子卷信息

  1. 创建/test目录并将子卷挂载到该目录:

    [root@workstation ~]# mkdir /test
    [root@workstation ~]# mount -o bind /btrfs/test /test
    

在这个示例中,子卷没有分配空间,因此它可以使用池中的所有可用空间。

  1. 验证存储池的已用空间:

    [root@workstation ~]# btrfs filesystem du /btrfs
         Total   Exclusive  Set shared  Filename
         0.00B       0.00B           -  /btrfs/test
         0.00B       0.00B       0.00B  /btrfs
    

要获取更多关于空间使用的细节,请运行以下命令:

btrfs filesystem usage /btrfs

图 4.5 – Btrfs 池使用的存储空间

图 4.5 – Btrfs 池使用的存储空间

让我们创建一个 1GB 的空文件,以观察分配使用空间的变化。

  1. 使用dd命令在/test目录中创建一个 1GB 的空文件:

    [root@workstation ~]# dd if=/dev/zero of=/test/example \
    > bs=1M count=1024
    
  2. 验证存储池中分配空间的变化:

    [root@workstation ~]# btrfs filesystem du /btrfs
    [root@workstation ~]# btrfs filesystem usage /btrfs
    

图 4.6 – Btrfs 池使用的存储空间变化

图 4.6 – Btrfs 池使用的存储空间变化

这是示例的结束部分。我们从识别一个可免费使用的存储设备开始,将其格式化为Btrfs,在上面创建了一个存储池,创建了一个子卷,并将其挂载到文件系统中,详细分析了存储空间的使用情况。

在下一节中,我们将继续回顾 Fedora Linux 存储管理的基本概念。

优化存储空间大小

在上一节中,我们回顾了 Btrfs 文件系统的一些特性。现在是时候更详细地学习如何优化这些已用存储空间了。

Btrfs 在其最低级别保留了一些原始存储空间,因为卷需要包含文件数据或卷元数据。为此,它为文件系统分配了原始存储空间的一部分。存储空间的一部分被称为。它的主要功能是容纳文件数据或卷元数据,以便在同一卷或另一个相似设备上进行复制。

存储空间被分配到块中,空间由块使用。没有块使用的块是未分配的;有一个或多个块被使用的块是已分配的。即使没有使用所有空间,所有块仍然可以被分配。

Btrfs 使用延迟分配来实现更好的磁盘分配。Btrfs 仅在系统需要处理脏页时才分配磁盘空间,因此最终,您会得到更大的分配和更大的连续数据块,这使得数据读取速度更快。

Btrfs 通过为数据分配 1 GB 的块和为元数据分配 256 MB 的块来分配磁盘空间。这意味着一个块有一个特定的配置文件:一旦为数据或元数据分配了一个块,该空间只能用于其中之一。因此,Btrfs 对于元数据和数据有不同的分配配置文件。

这种元数据和数据的划分可能会让人感到困惑:一个文件系统可能显示 10 GB 的数据,但只有 2 GB 的空闲空间。常见的操作系统命令,如 dfdu,无法显示 Btrfs 中空间使用和块分配的完整信息。因此,Btrfs 包含了自己的命令,用于显示已用空间和已分配的块。

使用上一节创建的文件系统,让我们更仔细地查看存储空间使用情况和块分配。使用 btrfs filesystem df 命令来显示 / btrfs 信息:

[root@workstation ~]# btrfs filesystem df /btrfs
Data, single: total=1.00GiB, used=1.00GiB
System, DUP: total=8.00MiB, used=16.00KiB
Metadata, DUP: total=256.00MiB, used=1.17MiB
GlobalReserve, single: total=3.50MiB, used=16.00KiB

请观察以下内容:

  • 数据系统元数据是不同的块组类型:

    • single 是在 mkfs 时定义的分配配置文件。

    • DUP 意味着 重复。它保证在同一磁盘上存在两个副本。此模式可以防止数据或元数据损坏,但无法防止磁盘故障。

    • total 是为给定类型的所有分配配置文件保留的空间总和,即所有 数据/单一。注意,这不是文件系统的总大小

    • used 是数据已用空间的总和,即文件扩展和元数据块。

  • GlobalReserve 是人工设置的内部紧急空间:

    • GlobalReserve 空间是用于的元数据的一部分。当文件系统的元数据耗尽时,它将被使用。虽然它没有被分配,但它显示为未使用的元数据空间。

从这里,您可以将其他存储设备添加到/btrfs文件系统中,使其成为一个跨所有添加设备的单一分区。为此,请按照以下步骤操作:

  1. 使用mkfs.btrfs命令将额外的设备格式化为Btrfs

    mkfs.btrfs /dev/vdc /dev/vdd...
    
  2. 向挂载的设备添加设备:

    btrfs device add /dev/vdc /dev/vdd...
    Performing full device TRIM /dev/vdc (10.00GiB) ...
    Performing full device TRIM /dev/vdd (20.00GiB) ...
    

如果我们重新运行btrfs filesystem df命令,输出显示没有变化:

[root@workstation ~]# btrfs filesystem df /btrfs
Data, single: total=1.00GiB, used=1.00GiB
System, DUP: total=8.00MiB, used=16.00KiB
Metadata, DUP: total=256.00MiB, used=1.17MiB
GlobalReserve, single: total=3.50MiB, used=0.00B

这是因为添加的磁盘既没有分配给数据,也没有分配给元数据。使用btrfs filesystem show命令,以下输出显示:

[root@workstation ~]# btrfs filesystem show /btrfs
Label: 'testbtrfs'  uuid: 6c8ccaad-f9a0-4957-919e-8d87e02078e3
Total devices 3 FS bytes used 1.00GiB
devid    1 size 50.00GiB used 1.52GiB path /dev/vdb
devid    2 size 10.00GiB used 0.00B path /dev/vdc
devid    3 size 20.00GiB used 0.00B path /dev/vdd

size值是每个磁盘的大小,used值是分配在该磁盘上的块的大小。因此,新的文件系统大小是 80 GB,但没有从新设备分配任何块,留下 79 GB 的空闲空间供分配。使用常规的df命令显示此信息:

[root@workstation ~]# df -h /btrfs
Filesystem      Size  Used Avail Use% Mounted on
/dev/vdb         80G  1.1G   79G   2% /btrfs

Btrfs 可以重新分配空间并回收任何浪费的空间。如果添加了磁盘,可以运行balance命令,确保所有内容均匀分布到磁盘上。

平衡任何受更新影响的Btrfs卷并防止分配卷中的每个块非常有用。通常,只需要平衡使用率达到 50%或 70%的块即可。

要自动平衡已挂载的/btrfs文件系统,请运行btrfs filesystem balance命令:

[root@workstation ~]# btrfs filesystem balance /btrfs
Done, had to relocate 3 out of 3 chunks

通过重新运行btrfs filesystem df命令,它显示了文件系统中新块的分布:

[root@workstation ~]# btrfs filesystem df /btrfs
Data, single: total=1.00GiB, used=1.00GiB
System, DUP: total=32.00MiB, used=16.00KiB
Metadata, DUP: total=256.00MiB, used=1.16MiB
GlobalReserve, single: total=3.50MiB, used=0.00B

添加设备时,通常建议在文件系统上运行一次平衡。

Btrfs 本身不会对文件系统执行定期的重新平衡,并且可能会遇到磁盘空间管理的问题。如果不加以注意,这些错误信息可能会导致无法重新平衡文件系统上的分区或设备。

该问题通常发生在磁盘 I/O 和文件大小呈现特定模式时。这会导致磁盘空间使用效率低下,并且阻止新写入磁盘,从而生成No space left on device错误。

为了防止这种情况,请时不时运行一次长期的空间分配检查,通常基于我们系统的工作周期。最简单的解释这种时间段的方法是为某些企业的现金结算。例如:如果企业每月的 30 号进行现金结算,我们就有一个窗口期,在该日期前后各一天,这样我们就有 28 或 29 个生产性的日子,在这段时间内系统无法更改。在这两个日期上,我们可以检查分配的空间,确认一切正常。

让我们来看一下如何执行此空间分配检查。

空间分配检查

Btrfs 分配的块大小最大为分区大小的 1/10,最多为 1 GB。理想情况下,至少必须保留一个未分配的块,以便在重新平衡操作时使用。

注意

如果 Btrfs 没有分配文件系统的大部分空间,则无需运行重新平衡。文件系统的大部分空间是指大于 80%的大小,或者是整个文件系统大小减去 2 GB。

要确定再平衡是否能释放空间,比较分配给数据的空间与数据实际使用的空间。如果它们之间的差异大于最大块大小,那么再平衡可能会释放一些空间。

btrfs filesystem usage 命令提供了获取这两个值所需的信息:

图 4.7 – Btrfs 文件系统使用情况输出

图 4.7 – Btrfs 文件系统使用情况输出

让我们创建一个额外的文件,更好地举例说明此检查:

[root@workstation ~]# dd if=/dev/zero \
> of=/btrfs/test/example2 bs=1M count=49152
49152+0 records in
49152+0 records out
51539607552 bytes (52 GB, 48 GiB) copied, 70.17 s, 734 MB/s

现在我们有一个已使用 49 GB 的文件系统,接下来看看它们是如何分配的:

[root@workstation ~]# btrfs filesystem df /btrfs
Data, single: total=50.00GiB, used=48.45GiB
System, DUP: total=32.00MiB, used=16.00KiB
Metadata, DUP: total=256.00MiB, used=54.98MiB
GlobalReserve, single: total=53.69MiB, used=16.00KiB
[root@workstation ~]# btrfs filesystem show /btrfs
Label: 'testbtrfs'  uuid: 6c8ccaad-f9a0-4957-919e-8d87e02078e3
Total devices 3 FS bytes used 48.51GiB
devid    1 size 50.00GiB used 34.56GiB path /dev/vdb
devid    2 size 10.00GiB used 3.00GiB path /dev/vdc
devid    3 size 20.00GiB used 13.00GiB path /dev/vdd

使用 btrfs filesystem usage 命令并加上 –b(字节)参数来计算是否需要再平衡:

图 4.8 – Btrfs 文件系统使用情况输出

图 4.8 – Btrfs 文件系统使用情况输出

从输出中,突出显示设备的大小、设备上的分配情况,以及数据的大小和使用情况:

[root@workstation ~]# btrfs filesystem usage -b /btrfs
Overall:
    Device size:          85899345920
    Device allocated:          54291070976
    Device unallocated:     31608274944
    Device missing:          0
... output omitted...
Data,single: Size:53687091200, Used:52613349376 (98.00%)
... output omitted...

使用这些信息,按照以下步骤进行计算:

  1. 设备大小为 85899345920。计算 80%:

    85899345920 * 0.80 = 68719476736
    
  2. 设备大小为 85899345920。减去 2 GB (2147483648):

    85899345920 - 2147483648 = 83751862272
    
  3. 比较两者结果。选择较大的值 (83751862272) 来与设备上的分配量进行比较:

    54291070976 < 83751862272
    

注意

如果设备分配小于计算出的条件,那么这部分检查为负,且不需要对该文件系统进行再平衡。但是必须进行数据存储效率检查,以确认不需要再平衡。

  1. 估算分配空间与使用空间之间的差异:

    53687091200 - 52613349376 = 1073741824
    
  2. 计算块大小:

    85899345920*0.10 = 8589934592
    
  3. 比较两者结果:

    8589934592 > 1073741824
    

如果块大小大于差值,那么检查也是负的。不需要再平衡。

只有在设备分配大于设备大小的 80%(或设备大小减去 2 GB)且分配和使用的空间大于块大小时,才需要进行再平衡。

记住,两个条件都不能满足。如果第一次检查为负,则必须运行第二次检查,以确认不需要进行再平衡。

Fedora Linux 提供了一个工具,帮助我们获取 Btrfs 文件系统使用情况的详细报告。我们来看看如何使用它。

使用 btrfs-usage-report 命令

python-btrfs 模块的主要目的是检查 Btrfs 文件系统,作为对底层内核调用和 Btrfs 数据结构的封装。该模块包括 btrfs-usage-report 工具,展示 Btrfs 文件系统的使用报告。

要安装该工具,请使用 dnf 命令安装 python3-btrfs 包:

[root@workstation ~]# btrfs-usage-report command to get the report of the filesystem mounted on /btrfs. The report is complete, as shown in *Figure 4**.9*.
At the beginning, it shows the following:

*   The filesystem ID
*   The physical space used
*   The profiles

It also shows an estimate of the virtual space available and how much raw disk is allocated per chunk type:
![Figure 4.9 – The Btrfs-usage-report output](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_04_09.jpg)

Figure 4.9 – The Btrfs-usage-report output
The last part of the report adds detailed information about storage space allocation:
![Figure 4.10 – The Btrfs-usage-report output](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_04_10.jpg)

Figure 4.10 – The Btrfs-usage-report output
Besides checking the allocated space and the efficiency of the data storage check, this tool helps optimize the storage space used on our workstation.
Note
Get the complete output of the **btrfs-usage-report** command from the **/btrfs** filesystem of our GitHub repository at [`github.com/PacktPublishing/Fedora-Linux-System-Administration/blob/main/chapter4/btrfs-usage-report_btrfs.txt`](https://github.com/PacktPublishing/Fedora-Linux-System-Administration/blob/main/chapter4/btrfs-usage-report_btrfs.txt).
Despite all these advanced features, the use of Brtfs as a filesystem is not the default in distributions other than Fedora Linux or OpenSUSE. Generally, most distributions prefer to use LVM with `ext4` or `xfs` as the filesystem format.
Let’s see how different the Brtfs filesystem is from LVM.
Deep diving into Logical Volume Manager
Despite basic differences between Btrfs and LVM, they have a lot in common:

*   They focus on protecting against filesystem corruption
*   They support single - or multiple-device setup
*   They can create quick snapshots
*   Several tools exist to help manage them in graphic or command - line form

LVM sits on a layer before the filesystem, so it supports any filesystem. LVM converts *any* device or partition into a physical device (`pv`) that is dynamically manageable. Physical devices get placed in volume groups (`vg`), enabling the creation of logical volumes (`lv`).
This is how an LVM structure gets created sequentially:

1.  It creates physical devices with the available devices, using the **pvcreate [device1] [device2] ...** command.
2.  It joins the physical devices, creating the volume group, using the **vgcreate [vg_name] [pv1] [pv2] ...** command.
3.  It creates the logical volume with the **lvcreate** command.

To remove each layer from the LVM structure, run similar commands in reverse order:

1.  To delete the logical volume, use the **lvremove** command.
2.  Remove the volume group with the **vgremove** command.
3.  Remove the physical devices with the **pvremove** command.

The following figure shows the LVM creation and removal sequence:
![Figure 4.11 – LVM creation and removal sequence and commands](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_04_11.jpg)

Figure 4.11 – LVM creation and removal sequence and commands
Physical devices could be added or removed to grow or reduce a volume group. In the same way, a logical volume could be extended or reduced but requires data verification before running it. In the case of `xfs` filesystems on LVM, there is no logical volume reduction.
LVM takes a set of block devices and presents the system with a new block device (`lv`) with a fixed mapping to physical blocks.
A Btrfs subvolume is different from an LVM logical volume, in the structure and behavior. A `Btrfs` volume is a mountable filesystem tree, not a block device.
The following figure shows the basic structures:
![Figure 4.12 – Btrfs subvolumes layouts](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_04_12.jpg)

Figure 4.12 – Btrfs subvolumes layouts
The typical Btrfs snapshot structure uses the flat scheme.
Let’s compare the snapshots of the Brtfs filesystem and LVM.
Differences between snapshots
LVM supports read-only and read-write snapshots. This eases the creation of consistent backups of active filesystems, with a defined size on the source volume group of the logical volume or an external source. This allows snapshots to belong to a thin provisioned pool when the target LV is a thin LV, which doesn’t consume fixed-size chunks of the source volume.
Creating snapshots on LVM is similar to creating a logical volume. Specify the snapshot name and the targeted LV, adding the following parameters to the `lvcreate` command:

lvcreate -s -n snapshot_name -L snapshot_size target_lv disk


 After taking the snapshot, all modifications made on the target LV will be stored in the new snapshot LV, but the old data remains in the source LV.
As needed, use the snapshot LV to roll back some changes in the source LV, mounting it in the affected directory instead of the original LV. Use the usual `mount` and `unmount` commands.
Remove the snapshot as an LV with the `lvremove` command:

lvremove /dev/vg_name/snapshot_name


 Note
It is possible to deactivate the logical volume before deleting it. Use the **lvchange -an** command. It will *not* show the prompt verifying whether you want to delete an active logical volume.
Snapshots in Btrfs are **COW clones** of a complete subvolume. They look similar to a new subvolume that contains the data of the cloned subvolume, but any changes in one do not affect the other. The space consumed increases as the original subvolume and the snapshot grow apart.
Create a snapshot using the `btrfs subvolume` command stating that it is a snapshot. Observe the following steps to create a snapshot on our `/btrfs` filesystem from the *Creating a Btrfs* *filesystem* section:

1.  List the subvolumes of the filesystem mounted on **/btrfs**:

    ```

    [root@workstation ~]# btrfs 子卷列表 /btrfs

    ID 256 gen 72 top level 5 path test

    ```

Note
The top-level subvolume with **Btrfs** ID **5** is the root of the volume. All subvolumes mount below it.

1.  Create a snapshot of the **test** subvolume:

    ```

    [root@workstation ~]# btrfs 子卷快照 \

    > /btrfs/test /btrfs/test_snapshot

    在 '/btrfs/test' 创建一个快照到 '/btrfs/test_snapshot'

    ```

     2.  Listing the subvolumes again displays the snapshot:

    ```

    [root@workstation ~]# btrfs 子卷列表 /btrfs

    ID 256 gen 76 顶级 5 路径 test

    ID 257 gen 76 顶级 5 路径 test_snapshot

    [root@workstation ~]# ls /btrfs/

    test   test_snapshot

    ```

     3.  Inspect the directories to verify that they contain the same data:

    ```

    [root@workstation ~]# tree /btrfs/

    /btrfs/

    ├── test

    │   └── example

    └── test_snapshot

    └── example

    3 个目录,2 个文件

    ```

With COW, each subvolume could change without changes in one affecting the other.
The differences between Btrfs and LVM are remarkable:

*   A Btrfs subvolume holds the capacity of the filesystem to which it belongs. A logical volume in LVM contains a capacity, which is a space reservation of the volume group it belongs to.
*   A Btrfs snapshot is a full-fledged subvolume, and once created there is no "original" and "snapshot." On LVM, a snapshot depends on the logical volume from the one that it came from.

For example, it is not possible to resize a logical volume or move its data with the `pvmove` command (from one physical volume to another) while having active snapshots of that logical volume.
Note
For more information about the LVM commands and their Btrfs equivalents, refer to the Fedora wiki article at [`fedoraproject.org/wiki/User:Chrismurphy/lvm2btrfs`](https://fedoraproject.org/wiki/User:Chrismurphy/lvm2btrfs).
Let’s now learn about Fedora’s latest approach to storage management, Stratis storage.
Discovering Stratis storage
`device mapper`, and existing filesystems such as `XFS`.
Here’s what you can do with Stratis:

*   Create, modify, and destroy storage pools
*   Allocate and detach filesystems from storage pools
*   Encrypt and decrypt filesystem data

Stratis storage brings together common storage management tools and automates them for ease of use. Here are some things it uses:

*   **device-mapper** – A framework for logical to physical mapping of data blocks
*   **LUKS** – An on-disk format for encryption that can securely manage multiple passwords
*   **XFS** – A scalable, journaling, and performant filesystem
*   **Clevis** – A framework for automated decryption

Here are the Stratis components:

*   **Blockdev** – Block devices, either a disk or a partition.
*   **Pool** – Consists of one or more block devices. A pool has a fixed total size, which represents the sum of all the block devices present in it.
*   **Filesystem** – Filesystems don’t have a fixed total size, thinly provisioned. The real size of the filesystem grows with the data stored in it. The thin volume and the filesystem increase automatically. Each pool could contain one or more filesystems, formatted with XFS.

The following diagram shows the layout of the components in the Stratis pool:
![Figure 4.13 – Stratis storage components](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_04_13.jpg)

Figure 4.13 – Stratis storage components
Let’s create a Stratis test pool to show its capabilities.
Creating a Stratis pool
For this practice example, reuse the disks used in the *Btrfs* section in this chapter.
Follow these steps:

1.  Unmount the **Btrfs** and **/test** filesystem and remove the **/****btrfs** directory:

    ```

    [root@workstation ~]# umount /btrfs/test

    [root@workstation ~]# umount /btrfs

    [root@workstation ~]# rm -rf /btrfs

    ```

     2.  Removes the **Btrfs** filesystem from the storage devices using the **wipefs** command:

    ```

    [root@workstation ~]# wipefs /dev/vdb --all –f

    [root@workstation ~]# wipefs /dev/vdc --all -f

    [root@workstation ~]# wipefs /dev/vdd --all -f

    ```

The `--all` parameter deletes all available signatures.
The `-f` parameter forces erasure even if the filesystem is mounted.

1.  Install Stratis using the **dnf** command:

    ```

    [root@workstation ~]# dnf 安装 stratis-cli stratisd

    ```

     2.  Use the **systemctl** command to enable the **stratisd** service:

    ```

    [root@workstation ~]# systemctl 启用 --now stratisd

    ```

     3.  With the **fdisk** command, verify the size of the disk:

![Figure 4.14 – Checking the disk size](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_04_14.jpg)

Figure 4.14 – Checking the disk size

1.  Use the **stratis pool create** command to create the pool test on the **/****dev/vdb** device:

    ```

    [root@workstation ~]# stratis 池创建 test /dev/vdb

    ```

     2.  List the Stratis pool using the **stratis pool** **list** command:

![Figure 4.15 – Stratis pool list](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_04_15.jpg)

Figure 4.15 – Stratis pool list
The following table lists the fields of the `stratis pool` `list` output:

  **Field**
 |
  **Description**
 |

  Name
 |
  The name of the pool
 |

  Total/Used/Free
 |
  The physical usage statistics for the pool
 |

  Properties
 |
  Boolean-valued properties that the pool may have. Each property has a two-letter code in camel case. If the pool does not have the property, add a `~` symbol, which stands for *negation*, to the property code.
If the engine has experienced an error in obtaining the property, add a `?` symbol, which stands for *unknown*, to the property code.
The property codes are as follows:

*   **Ca** - Indicates that the pool has a cache

*   **Cr** - Indicates encrypted pool

*   **Op** - indicates that the pool allows over-provisioning

 |

  UUID
 |
  Universally Unique IDentifier of the pool
 |

  Alerts
 |
  Anything unusual or urgent information about the pool that the user needs information on.
 |

Let’s now add another device to the pool to see how the available space in the pool increases:

1.  Use the **stratis pool add-data** command to add the **/dev/vdc** device to the Stratis pool and list the pool again to observe the change:

![Figure 4.16 – Stratis pool list](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_04_16.jpg)

Figure 4.16 – Stratis pool list
Now let’s create a filesystem to show the differences from a common filesystem.

1.  Create the filesystem data using the **stratis filesystem** **create** command:

    ```

    [root@workstation ~]# stratis 文件系统创建 test data

    ```

     2.  List the filesystems in the **test** pool to observe their properties. Use the **stratis filesystem** **list** command:

![Figure 4.17 – Stratis filesystem list](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_04_17.jpg)

Figure 4.17 – Stratis filesystem list
Note that the total size of the filesystem is 1 TiB, even though the sum of the actual available disk space is 60 GiB:

[root@workstation ~]# stratis 文件系统列表 test

池     文件系统     总计 / 已用 / 可用

test   data         1 TiB / 546 MiB / 1023.47 GiB

...输出省略...


 As mentioned, the filesystem gets created on a thinly provisioned 1 TiB block device by design. Stratis handles the allocation of blocks from the pool and resizes the `XFS` filesystem on demand during its lifetime.
Stratis also facilitates the creation of snapshots of filesystems. A Stratis snapshot is a thinly provisioned real-time read/write copy of the source filesystem.
Let’s see how to create snapshots.

1.  Use the **stratis filesystem snapshot** command to create the snapshot, and then list the filesystems of the pool:

![Figure 4.18 – Stratis filesystem list](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_04_18.jpg)

Figure 4.18 – Stratis filesystem list
The filesystems and their snapshots could mount in the traditional way in the operating system. Use the `mount` command and, to keep them mounted permanently, configure an entry in the `/``etc/fstab` file.
Note
For more information about Stratis, refer to the Stratis documentation at [`stratis-storage.github.io/howto/`](https://stratis-storage.github.io/howto/).
This concludes our review of storage basics. In the following chapters, we will review some other fundamental concepts, such as networking and the use of applications, along with applying system administration of Linux systems.
Summary
This chapter consisted of reviewing the basic concepts of storage management and focused on the Btrfs filesystem, performing practical examples to understand its use and main features, identifying storage devices, formatting storage devices, creating pools, and mounting filesystems.
We clarified some items that assist in optimizing the allocation of storage space and learned the main differences between Btrfs subvolumes and LVM.
As a bonus, we discussed the Fedora Project’s new approach to storage management with Stratis, the intention of which is to ease storage management.
In the next chapter, we will cover the fundamental concepts and configurations of network connections and how to optimize them.
Further reading
To learn more about the topics that were covered in the chapter, please visit the following links:

*   *Fedora Wiki –* *Btrfs*: [`fedoraproject.org/wiki/Btrfs`](https://fedoraproject.org/wiki/Btrfs)
*   *Btrfs* *documentation*: [`btrfs.readthedocs.io/en/latest/`](https://btrfs.readthedocs.io/en/latest/)
*   *Choose between Btrfs and* *LVM-ext4*: [`fedoramagazine.org/choose-between-btrfs-and-lvm-ext4/`](https://fedoramagazine.org/choose-between-btrfs-and-lvm-ext4/)
*   *Getting started with Stratis – up and* *running*: [`fedoramagazine.org/getting-started-with-stratis-up-and-running/`](https://fedoramagazine.org/getting-started-with-stratis-up-and-running/)
*   *Stratis* *storage*: [`stratis-storage.github.io/`](https://stratis-storage.github.io/)

第七章:网络与连接性

现在,是时候回顾一下我们工作站上的网络配置和管理基础知识了。这将帮助我们了解在网络配置中可以调整和微调的基本要点。我们将深入了解 NetworkManager 管理工具,以及有关其使用的最常见技巧和示例。

我们还将讲解有线和无线网络配置。在下一部分,我们将回顾如何访问 虚拟专用网络VPN)。最后,我们将学习用于监控网络性能和连接性工具。

本章我们将涵盖以下主要内容:

  • 基础知识概述

  • 调整无线连接

  • 使用 VPN 改善网络连接

  • 网络性能监控

让我们开始吧!

技术要求

本章中提到的配置需要访问本地网络,无论是有线还是无线。进行这些测试不需要专用的网络接口。

你可以在本书的 GitHub 仓库中找到本章使用的模板和示例文件:github.com/PacktPublishing/Fedora-Linux-System-Administration/tree/main/chapter5

基础知识概述

NetworkManager 使用 连接配置文件 的概念,包含网络配置。这些配置文件支持多种格式来存储属性,例如 int32booleanuint32stringarray of stringuint64 等等。随着 NetworkManager 使用和功能的增长,ifcfg 文件可能不再受支持。

NetworkManager 使用插件来读取和写入这些数据。NetworkManager 增加了对新连接类型的支持,最终采用了一个更简化的配置文件格式,称为 keyfile 插件。

keyfile 插件支持所有 NetworkManager 支持的连接类型和功能。NetworkManager 使用 INI-key 文件格式来存储连接配置文件数据。

这个插件始终保持活动状态,并存储其他没有支持的活动插件所无法支持的任何连接。出于安全考虑,它会忽略任何非 root 用户可能读取或写入的文件,因为私钥和密码可能以明文存储在文件中。

让我们以一个以太网连接配置文件为例,更好地理解插件:

[connection]
id=net_connection
uuid= 9cd8a444-f501-4179-900e-f3754dbbe7c0
type=ethernet
autoconnect=true
[ipv4]
method=auto
[ipv6]
method=auto
[ethernet]
mac-address=48:2a:e3:8f:4b:aa

它由键值对的组(部分)组成,每组对应一个配置名称,描述为属性。规范中的大多数属性也以相同的格式写在 key 文件中。然而,一些值不太友好使用,这些值以更可读的形式存储在 /etc/NetworkManager/system-connections//usr/lib/NetworkManager/system-connections//run/NetworkManager/system-connections/ 中。

注意

有关密钥文件设置描述插件的更多信息,请参阅GNOME 开发者文档developer-old.gnome.org/NetworkManager/stable/nm-settings-keyfile.html

和 Linux 中的任何文件一样,密钥文件可以被修改,但 NetworkManager 必须知道这些更改,才能考虑它们。

现在,让我们回顾一下帮助我们处理 NetworkManager 和网络配置的工具。

NetworkManager 命令行界面(nmcli)

NetworkManager 可以通过nmcli命令行工具进行管理(nmcli允许我们创建、显示、编辑、删除、激活和停用网络连接。除了控制和显示网络设备的状态外,它还可以作为终端中的命令运行,或从脚本中调用)。

作为一个从终端运行的命令,nmcli可以创建、编辑、启动和停止网络连接,或显示网络设备的状态,而无需使用 GUI 或桌面小应用程序。

对于脚本,nmcli提供了一种简单的输出格式,可以适应数据处理,将网络配置集成到一个过程,而不是手动管理网络连接。

基本的nmcli命令运行格式如下:

nmcli [OPTIONS] OBJECT { COMMAND | help }

注意

要查看完整的选项列表,请在终端中运行man nmcli命令。

然后,要获取连接概况,运行nmcli -o conn命令:

图 5.1 – 网络连接概览

图 5.1 – 网络连接概览

要查看enp1s0网络设备的概况,请运行nmcli -o dev show enp1s0命令:

图 5.2 – 网络设备概览

图 5.2 – 网络设备概览

你可以列出nmcli -f TYPE,FILENAME,NAME conn命令:

图 5.3 – 检查网络连接密钥文件

图 5.3 – 检查网络连接密钥文件

在这种情况下,注意到工作站的IP 地址被设置为auto,这意味着它使用 DHCP 作为启动协议:

[ipv4]
method=192.168.122.100 instead of the one assigned by DHCP, follow these steps:

1.  Back up the original configuration of the connection:

    ```

    # cat /run/NetworkManager/system-connections/Wired\ connection\ 1.nmconnection >>  wired_connection_1.bkp.txt

    ```

![Figure 5.4 – Backing up network connection settings](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_04.jpg)

Figure 5.4 – Backing up network connection settings
Note
Running the **cat** command to redirect the output to a file lets us know about and identify the contents of the file.

1.  Add the **enp1s0** connection as a new NetworkManager connection profile:

    ```

    # nmcli connection add con-name wired-conn1

    ifname enp1s0 type ethernet

    ```

![Figure 5.5 – Adding the new NetworkManager connection profile](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_05.jpg)

Figure 5.5 – Adding the new NetworkManager connection profile

1.  Set the IP address of this profile:

    ```

    # nmcli connection modify wired-conn1

    ipv4.addresses 192.168.122.100/24

    ```

![Figure 5.6 –  Setting the IP address](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_06.jpg)

Figure 5.6 –  Setting the IP address

1.  Set the connection method to **manual**:

    ```

    # nmcli connection modify wired-conn1

    ipv4.method manual

    ```

![Figure 5.7 – Setting the connection method](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_07.jpg)

Figure 5.7 – Setting the connection method

1.  Set the gateway to **192.168.122.1**:

    ```

    # nmcli connection modify wired-conn1  ipv4.gateway 192.168.122.1

    ```

![Figure 5.8 – Setting the gateway](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_08.jpg)

Figure 5.8 – Setting the gateway

1.  Set **dns** to **192.168.122.1**:

    ```

    # nmcli connection modify wired-conn1  ipv4.dns 192.168.122.1

    ```

![Figure 5.9 – Setting dns](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_09.jpg)

Figure 5.9 – Setting dns

1.  Activate the new NetworkManager connection profile:

    ```

    [root@workstation ~]# nmcli connection up wired-conn1

    ```

![Figure 5.10 – Activating the NetworkManager profile](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_10.jpg)

Figure 5.10 – Activating the NetworkManager profile

1.  Check the device’s status:

    ```

    [root@workstation ~]# nmcli device status

    ```

![Figure 5.11 – Reviewing the device’s status](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_11.jpg)

Figure 5.11 – Reviewing the device’s status

1.  Check the network configuration of the workstation. Confirm the changes in the IP address of the **enp1s0** device. Then, use the **ip** command:

    ```

    [root@workstation ~]# ip a

    ```

![Figure 5.12 – Verifying the network configuration](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_12.jpg)

Figure 5.12 – Verifying the network configuration

1.  Review the workstation connection’s keyfiles:

    ```

    # nmcli -f TYPE,FILENAME,NAME conn

    ```

![Figure 5.13 – Checking the keyfiles](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_13.jpg)

Figure 5.13 – Checking the keyfiles
Note that the *active configuration* is now highlighted in green.
Let’s compare the original backup file with the created one. Use the `diff` command to highlight the differences:
![Figure 5.14 – Keyfile differences](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_14.jpg)

Figure 5.14 – Keyfile differences
The first difference is in the `id` value of the *connection name*. But also, notice that there is a change in the `uuid` value:

< id=Wired connection 1

< uuid=0938b01a-f879-3f76-a796-b3f856dc27ac


id=wired-conn1

uuid=uuid) 用于此连接配置文件。这允许多个连接应用于同一设备,但只能激活其中一个。额外的连接使您可以快速切换不同的网络或配置。

连接在安装过程中被自动配置为 DHCP。由于在安装过程中此参数保持不变,启动时协议分配的属性之一是自动连接优先级

< auto-connect, higher-priority connections are preferred. The default is 0. *A higher number means a* *higher priority*.
An interesting piece of data that can be found in the original keyfile is `timestamp`, in seconds since the **Unix Epoch**, when the connection was last successfully activated:

< ipv4 配置部分:

< method=auto
---
> address1=192.168.122.100/24,192.168.122.1
> dns=192.168.122.1;
> auto. The current keyfile is set to manual, plus the configuration of the *IP address*, *gateway*, and *DNS*.
A *wired connection* is the simplest example of network configuration, although several extra properties could be added.
A *wireless connection*, which is used for the kind of work involved in managing Linux servers, is the type of connection we will use the most, given its inherent mobility advantages.
Now, let’s see how, besides configuring our *wireless connection*, to optimize it with some fine-tuning settings.
Tuning wireless connectivity
Sometimes, the network’s devices are not recognized by the operating system. This is one of the common issues of using wireless networks.
Let’s start this section by ensuring that the wireless network device has support from the operating system by identifying which chipset it uses and which drivers work best for its operation.
Identifying the device
In a network device, the main thing is to identify the *Vendor ID* (the 16-bit number of the vendor – that is, the **VID**) and the *Product ID* (the 16-bit number of the product number – that is, the **PID**). Use the following commands for each case, depending on the bus used by the device:

*   PCI: **lspci -nn**
*   USB: **lsusb**:

![Figure 5.15 – Identifying the network device](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_15.jpg)

Figure 5.15 – Identifying the network device
As shown in the preceding figure, the output of the `lspci` command provides the required chipset information:

00:00.0 PCI 桥 [0604]: Broadcom Inc. 及其子公司 BCM2711 PCIe 桥 [14e4:2711] (rev 10)


 Now, go to Google and type the chipset information in the form:

"14e4 2711" site:cateee.net/lkddb/


 This search leads to a chipset match in **Linux Kernel Driver DataBase** ([`cateee.net/lkddb/`](https://cateee.net/lkddb/)), which allows us to identify whether the wireless network device has support and which driver is the correct one to use:
![Figure 5.16 – LKDDb search](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_16.jpg)

Figure 5.16 – LKDDb search
If you search for chipset information in the form – that is, `"14e4:2711"` – it leads to several useful sites that provide more detailed information. Click on the **Hardware for Linux** page link ([`linux-hardware.org/`](https://linux-hardware.org/)) and find the chipset’s details:
![Figure 5.17 – The Hardware for Linux site](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_17.jpg)

Figure 5.17 – The Hardware for Linux site
With the preceding tools, we can identify the wireless device in our computer. Follow these steps to identify the vendor and its product ID:

1.  Run the **lspci** command to identify the card:

    ```

    [root@workstation ~]# lspci | grep -i broadcom

    00:00.0 PCI 桥: Broadcom Inc. 及其子公司 BCM2711 PCIe 桥 (rev 10)

    ```

Note
If the card is not integrated into the motherboard of the computer, use the **lsusb** command to identify this device.
A bus number, a device number, and a function number can be identified at the beginning of the output:

00:00.0


1.  Use this information to get detailed device information. To do so, run the **lspci** command:

    ```

    [root@workstation ~]# lspci -vv -s 00:00.0

    00:00.0 PCI 桥: Broadcom Inc. 及其子公司 BCM2711 PCIe 桥 (rev 10) (prog-if 00 [正常解码])

    设备树节点: /sys/firmware/devicetree/base/scb/pcie@7d500000/pci@0,0

    控制: I/O- Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR- FastB2B- DisINTx-

    状态: Cap+ 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR- INTx-

    延迟: 0

    中断: pin A 路由到 IRQ 38

    ...省略输出...

    ```

     2.  List the modules that were loaded in the kernel and search the identified chipset of the wireless card. Use the **lsmod** command:

    ```

    root@workstation ~]# lsmod | grep -i broadcom

    broadcom               28672  1

    bcm_phy_ptp            24576  1 broadcom

    bcm_phy_lib            28672  2 bcm_phy_ptp,broadcom

    ```

     3.  Use the **modinfo** command to display the module information:

    ```

    [root@workstation ~]# modinfo broadcom

    文件名:       /lib/modules/6.1.12-200.fc37.aarch64/kernel/drivers/net/phy/broadcom.ko.xz

    许可证:        GPL

    作者:         Maciej W. Rozycki

    描述:    Broadcom PHY 驱动

    ...省略输出...

    依赖:        bcm-phy-lib,bcm-phy-ptp

    intree:         Y

    名称:           broadcom

    vermagic:       6.1.12-200.fc37.aarch64 SMP 预占

    mod_unload aarch64

    sig_id:         PKCS#7

    签名者:         Fedora 内核签名密钥

    sig_key:        4C:FF:C2:28:93:D4:32:A9:E6:E0:32:6A:D9:B0

    ...省略输出...

    ```

With these simple steps, we can determine whether the wireless card is using the right, updated driver. This prevents any initial issues when connecting to a network.
Now, let’s learn how to identify the available wireless networks and configure our wireless card to get the best connectivity performance.
Finding the best quality network connection
By default, the `nmcli` command lists the available **Wi-Fi access points** that are no more than *30 seconds old* by enabling a network scan if necessary.
To list the available Wi-Fi access points, run the `nmcli dev wifi` `list` command:
![Figure 5.18 – Listing the Wi-Fi access points](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_18.jpg)

Figure 5.18 – Listing the Wi-Fi access points
The `nmcli` command shows the Wi-Fi access points in different colors. Each one represents different signal strengths.
Soft colors, such as cyan, represent a weak signal (less than 30% intensity). A stronger signal (30-50%) is represented by a magenta color. Orange indicates a better signal (60-80%), whereas green represents an excellent signal (80-100%).
With the `--rescan` option, the scan could be forced to be disabled, regardless of the age of the access point list.
Use the `nmcli device` command to identify the wireless network device:
![Figure 5.19 – Identifying the wireless network device](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_19.jpg)

Figure 5.19 – Identifying the wireless network device
From the list of access points, select those that belong to the location of the workstation. Both have good signal quality:
![Figure 5.20 – Selecting Wi-Fi access points](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_20.jpg)

Figure 5.20 – Selecting Wi-Fi access points
Select the access point with the best signal quality and connect to it. Use the `nmcli` command with the following format:

nmcli device wifi connect [SSID] password [SSID-password]


 Note
This command preserves the **password** in the shell history. To remove it from the history, delete the corresponding line in the **~/.bash_history** file or run the **history -c** command to delete all entries.
Then, use the `nmcli device` command to confirm the device status change:
![Figure 5.21 – Connecting to the access point](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_21.jpg)

Figure 5.21 – Connecting to the access point
Repeat the same command and add the `show` and `device` options to display the connection details:
![Figure 5.22 – Connection details](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_22.jpg)

Figure 5.22 – Connection details
The **received signal strength indicator** (**RSSI**), which specifies how strong the received signal is, is measured in *decibels relative to one milliwatt* (**dBm**) or **10-3 W**.
To find the RSSI, it’s necessary to interpret the *raw value* given by `/proc/net/wireless` and then display it in the appropriate unit or greatest value (using 8-bit arithmetic).
Use the `iw` command to get the device link information:

[root@workstation ~]# iw wlan0 link

已连接到 2c:58:4f:7c:4a:1b (在 wlan0 上)

SSID: innsmouth

频率: 2462

RX: 1817351 字节 (8922 数据包)

TX: 806585 字节 (3517 数据包)

信号: -38 dBm

rx 比特率: 58.5 MBit/s

tx 比特率: 72.2 MBit/s

bss 标志:     短前导码

dtim 周期:     1

beacon int:     100


 The `iw` command grabs the information from the `/proc/net/wireless` file and interprets it. By combining this with the `watch` command, it is possible to directly monitor changes in the file. Here’s an example:

watch -n 1 cat /proc/net/wireless


 ![Figure 5.23 – Monitoring the quality of the connection](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_23.jpg)

Figure 5.23 – Monitoring the quality of the connection
Monitoring the frequency of changes in quality values and comparing them on different networks could establish the optimal network to use for our system administration tasks. This will help us reduce connectivity interruptions in such tasks.
In the last section of this chapter, we will take a more in-depth look at monitoring connections for better performance and minimizing outages.
The `nm-connection-editor` is a *GTK-based* application for adding, deleting, and modifying network connections that NetworkManager stores. Let’s see how to do it.
nm-connection-editor
In the top-right corner of the taskbar, there is the control panel for settings, including the network connection. If the workstation is not connected to any network, only the icons for keyboard language, volume, and system power will appear:
![Figure 5.24 – The top-right corner of the taskbar](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_24.jpg)

Figure 5.24 – The top-right corner of the taskbar
Click on that area to display the control panel:
![Figure 5.25 – Taskbar control panel](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_25.jpg)

Figure 5.25 – Taskbar control panel
Follow these steps to connect the workstation to the wireless network:

1.  Click on the gear icon and select **Wi-Fi** to discover the available wireless networks:

![Figure 5.26 – Wi-Fi – Visible Networks](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_26.jpg)

Figure 5.26 – Wi-Fi – Visible Networks

1.  Locate the desired wireless network and click on it. If required, you will be prompted to enter the access password:

![Figure 5.27 – Requesting access to the wireless network](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_27.jpg)

Figure 5.27 – Requesting access to the wireless network

1.  If the password you’ve entered is correct, you will be able to access the wireless network and the connection details will be shown:

![Figure 5.28 – Connection details](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_28.jpg)

Figure 5.28 – Connection details

1.  Now, the wireless connection icon will appear in the top-right corner of the taskbar:

![Figure 5.29 – Wireless icon on the taskbar](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_29.jpg)

Figure 5.29 – Wireless icon on the taskbar
When we click on it, the control panel will show us an icon that we can use to disable the connection or view the available networks:
![Figure 5.30 – Wireless direct access icon](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_30.jpg)

Figure 5.30 – Wireless direct access icon
Now, we can configure the network access to our workstation, either cable or wireless, depending on our needs.
In the examples shown in this chapter, we have focused on setting access and ensuring a healthy connection to the network.
However, we cannot omit the security risks involved in connecting to a network, especially those with public access.
Let’s talk about this and how we can mitigate these risks.
What about security?
The use of wireless networks has boosted the mobility of jobs that rely on the use of computers.
Open wireless networks provide many facilities for remote work from anywhere in the world. However, they also include the risk of exposing confidential information about ourselves, our employers, or the clients we work with.
The most basic principle of security is to be aware of what we have open, such as ports, sockets, files, and/or processes. Some processes use the network’s open ports to maintain connectivity between the client and server machine. Applying security’s basic principle, we must be aware of the ports that have been opened and only allow those that we know must remain that way.
The tool that helps us in these cases is the **network firewall**.
As its name suggests, a network firewall is *a protective barrier that prevents unauthorized network communications*. Its main purpose is to prevent unauthorized access or use of the network services of our system.
`firewalld`.
`firewalld` provides dynamic firewall management with support for network/firewall zones. That support defines the *trust level* of network connections or interfaces. The `firewalld` D-Bus interface uses the `firewall-cmd`, `firewall-config`, and `firewall-applet` firewall configuration tools.
The following diagram shows the firewall management flow with `firewalld`:
![Figure 5.31 – The firewall management flow with firewalld](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_31.jpg)

Figure 5.31 – The firewall management flow with firewalld
By default, the `firewalld` service comes installed with Fedora. You can use the `firewall-cmd --``state` command:

[root@workstation ~]# firewall-cmd --state

firewall-cmd --list-all 命令:

[root@workstation ~]# firewall-cmd --list-all
FedoraWorkstation (active)
  target: default
  icmp-block-inversion: no
  interfaces: wlan0
  sources:
  services: dhcpv6-client mdns samba-client ssh
  ports: 1025-65535/udp 1025-65535/tcp
  protocols:
  forward: yes
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
dhcpv6-client, mdns, samba-client, and ssh services, as well as the 1025-65535/udp and 1025-65535/tcp dynamic ports.
In our workstation use case, it is likely that we do not need to have services or ports exposed to the network. So, the best practice is to close them all.
Before closing them, use the `ss` command to investigate the ports that have been opened by the processes to determine whether they refer to the services and ports allowed in the firewall:

ss -tulpn


 ![Figure 5.32 – Ports and services connections](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_32.jpg)

Figure 5.32 – Ports and services connections
As a result of this analysis, it is clear that our network is not using the open services allowed in the firewall, so we should close them. Let’s look at the main reasons why we should do this:

*   The network does not have **ipv6** (**dhcpv6-client**)
*   DNS resolution does not use *multicast* on the network. (**mdns**)
*   We do not use shared directories by **CIFS** or **samba** (**samba-client**)
*   The workstation will be used to connect to other computers via **ssh**, but not in the opposite direction

To permanently remove the services from the configuration, use the `firewall-cmd` command:

[root@workstation ~]# firewall-cmd --permanent --delete-service={dhcpv6-client,mdns,samba-client}

错误: BUILTIN_SERVICE: 'dhcpv6-client' 是内建服务

错误: BUILTIN_SERVICE: 'mdns' 是内建服务

错误: BUILTIN_SERVICE: 'samba-client' 是内建服务


 Note
These services come as part of the system and they cannot be removed from the configuration.
The fourth point (from the preceding list) is also optional. We can do this if we need to connect to the workstation and connect to the managed servers from there, either as a *pivot server* or a *jump box*.
With the same command, remove the dynamic ports:

[root@workstation ~]# firewall-cmd --permanent --remove-port={1025-65535/udp,1025-65535/tcp}

成功


 Note
The **firewall-cmd --permanent** command only affects the configuration files, not the firewall running in memory.
The service must reload its configuration to consider the changes.
Use the `firewall-cmd --reload` command and verify the configuration change:

[root@workstation ~]# firewall-cmd --reload

成功

[root@workstation ~]# firewall-cmd --list-all

FedoraWorkstation (活动)

目标: 默认

icmp-block-inversion: 否

interfaces: wlan0

sources:

services: dhcpv6-client mdns samba-client ssh

ports:

protocols:

forward: yes

masquerade: no

forward-ports:

source-ports:

icmp-blocks:

rich rules:


 Thus, by adding services and/or ports, we will have better control of the connections to our workstation.
Note
For more information on the use of **firewalld**, refer to the service manual included in the operating system. Run the **man firewalld** command to access it.
From our side, the workstation has a secure environment that knows and authorizes the services and ports exposed to the network. But, especially in a public network, this communication must have an extra level of security. This could come from using a VPN.
Let’s learn how to use a VPN to secure the connection from our workstation to the managed computers.
Improving network connectivity using a VPN
A VPN is a way of connecting to a local network over the internet.
A VPN connection creates a *single virtual private network between two points*. Each bit of data gets encrypted from point to point via **tunneling**. Tunneling refers to the process by which VPN packets reach their destination. VPNs operate at the operating system level, redirecting all their traffic through other servers. All traffic, including its physical location, remains hidden during network browsing. When using a VPN, the VPN router, called the *proxy server*, appears as the origin of the connection, not the real one. So, neither the site offering public access nor anyone else will be able to deduce who you are or what you are doing.
The following diagram shows, with a red line, the tunneling created by the VPN by connecting the workstation to the managed server through the public network:
![Figure 5.33 – Virtual private network](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_33.jpg)

Figure 5.33 – Virtual private network
A VPN could be created with different types of tunneling using a variety of protocols, such as **Secure Shell** (**SSH**) or **Point-to-Point Protocol** (**PPP**). They also support different service providers, such as **Cisco**, **Juniper**, **NetScreen**, and **SonicWall**, and they are multi-compatible.
Now, let’s provide an overview of their two most common services: an **IPSec-based VPN** and **OpenVPN**.
IPSec-based VPN
`Libreswan` is an `Libreswan` uses the `NSS` library.
In Fedora, by default, `Libreswan` is not installed. So, install the `NetworkManager-libreswan-gnome` package with the `dnf` command:

[root@workstation ~]# dnf install NetworkManager-libreswan-gnome


 Later, we will describe how to configure the VPN client with this protocol.
OpenVPN
**OpenVPN** supports client authentication methods based on certificates, smart cards, and/or two-factor authentication. It allows access control policies through firewall rules. OpenVPN *is not* a web application proxy and *does not work* through a web browser. It needs to be set up in the properties of the network connection.
The OpenVPN client integrates into `NetworkManager` as an *applet*.
Now, let’s learn how to connect the workstation as a VPN client.
Configuring a VPN client with the Control Center
Follow these steps to connect the workstation as a VPN client:

1.  Go to the top-right corner, click on it, and click on the **gear** icon to open the **Control** **Center** area:

![Figure 5.34 – Control Center](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_34.jpg)

Figure 5.34 – Control Center

1.  In **Settings**, in the left column, click **Network**:

![Figure 5.35 – Network settings](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_35.jpg)

Figure 5.35 – Network settings

1.  In the **VPN** section, click on the *plus* (**+**) symbol to add a new connection.

If the workstation has the `NetworkManager-libreswan-gnome` package installed, the option to add an IPsec-based VPN will appear.
The following figure shows the difference. The `NetworkManager-libreswan-gnome` package installed, while the window on the right side does not have the package installed:
![Figure 5.36 – Different VPN connection types available](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_36.jpg)

Figure 5.36 – Different VPN connection types available
Let’s look at each case.
IPsec-based VPN
The following figure shows the window for adding the **IPsec-based** **VPN** connection:
![Figure 5.37 – IPsec-based VPN](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_37.jpg)

Figure 5.37 – IPsec-based VPN
In the **Identity** tab, add some general configuration properties for the connection:

*   **Name**: This will identify the **VPN** connection.
*   **General**:
    *   **Gateway**: The *name* or IP address of the **VPN** gateway
*   **Authentication**:
    *   **Type**: IPsec supports two different types of authentication:
        *   **IKEv2 (Certificate)**: This authentication uses a *certificate*, so it is the most secure option. This is the **default option** in the connection.
        *   **IKEv1 (XAUTH)**: The authentication uses a *username and password* or a *pre-shared key*.
*   **Certificate name**: The certificate’s filename
*   **Remote ID**: This is used if the remote server requires a local identifier for the IKE exchange

By clicking on the **Advanced** button, you can access more specific configuration details. If more details for the connection are needed, they can be set up in this window:
![Figure 5.38 – IPsec-based VPN – Advanced Properties](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_38.jpg)

Figure 5.38 – IPsec-based VPN – Advanced Properties
After filling in the required fields, click the **Apply** button. Returning to the previous **Add VPN** screen, click the **Add** button to add the VPN connection.
OpenVPN
Clicking on **OpenVPN** displays a window similar to the IPsec-based screen:
![Figure 5.39 – OpenVPN connection](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_39.jpg)

Figure 5.39 – OpenVPN connection
Filling in all the fields, as you did for the previous connection type, allows you to add the OpenVPN connection.
However, adding an OpenVPN connection is typically done by *importing a certificate*. Let’s take a look at this process.
In the **Add VPN** connection window, click on the last option, **Import** **from file…**:
![Figure 5.40 – Importing the .ovpn file certificate](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_40.jpg)

Figure 5.40 – Importing the .ovpn file certificate
Browse to the `.ovpn` file and click the **Open** button. The configuration window will display the connection information.
For example, in this case, we will import the `/``home/acallejas/sample.ovpn` file.
The following figure shows the **sample vpn** connection information:
![Figure 5.41 – Sample VPN connection](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_41.jpg)

Figure 5.41 – Sample VPN connection
Regarding the connection, note the following:

*   The file contains the connection’s **Name** and the remote access **Gateway** details.
*   Authentication requires **User name** and **Password.**
*   **CA certificate** is a **.pem** file. This file could request a *dynamic passphrase* or **one-time password** (**OTP**). This is generated by a physical, virtual, or hybrid device to allow access.

Click on the **Apply** button, if necessary. The OpenVPN connection will be added.
At the end of the VPN connection configuration, go to **Control Center** and select the VPN you want to access:
![Figure 5.42 – Choosing a VPN connection](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_42.jpg)

Figure 5.42 – Choosing a VPN connection
After successfully authenticating, the login icon will appear in the top-right corner:
![Figure 5.43 – Workstation connected to the VPN](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_43.jpg)

Figure 5.43 – Workstation connected to the VPN
Note
One of the best practices for secure communications is to have your own VPN. For more information on how to do this, visit the articles published in **Fedora Magazine** at [`fedoramagazine.org/tag/vpn/`](https://fedoramagazine.org/tag/vpn/).
Now that we have improved our network connection, be it wired or wireless, using tuning or a VPN, let’s learn about some tools that will help us track the connection’s behavior.
Network performance monitoring
There are different and varied network connection monitoring tools, but I prefer those that can be managed via the console. This allows for flexibility and, at the same time, can be used to run test batteries to verify the information that’s been received.
The first tool we’ll look at is basic but also has enough power to generate a quick analysis of the connection’s behavior.
nmon
`nmon`) was created for the **IBM AIX** operating system with *release 4.3* in 2006 and was ported to Linux in 2015 under the **GNU General Public License** for use.
`nmon` is a benchmark tool. It collects operating system statistics, including the following:

*   CPU (and CPU thread utilization or CPU frequency)
*   GPU stats
*   Memory use (physical and virtual, swap and paging)
*   Disk (read and write, transfers plus service time and wait times, groups)
*   Network (read and write and transfers), and more

This information can be displayed on the screen or saved in a *comma-separated file*. This allows for file backup and analysis to be performed with data extraction tools.
`nmon` is available in Fedora’s repositories. Install `nmon` using the `dnf` command:

[root@workstation ~]# nmon command:

nmon window:
![Figure 5.44 – nmon](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_44.jpg)

Figure 5.44 – nmon
In the case of network connection analysis, press `n` to display the connection statistics:
![Figure 5.45 – Network stats](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_45.jpg)

Figure 5.45 – Network stats
Note
**nmon** can collect a lot of useful information for workstation *performance benchmarking*. To learn more about its uses, refer to the **nmon for Linux** page at [`nmon.sourceforge.net/`](https://nmon.sourceforge.net/).
Another of my favorite tools is `bashtop`, but it is not included in the Fedora repositories at the time of writing. It must be installed by following the instructions on the project’s page on GitHub ([`github.com/aristocratos/bashtop#installation`](https://github.com/aristocratos/bashtop#installation)).
A `bashtop` that’s included in the official `bpytop`.
Let’s learn how to install and use it.
bpytop
`bpytop` is a *resource monitor* that displays usage and statistics for the processor, memory, disks, network, and processes.
It includes the most common features of resource monitors, such as ease of use and process manipulation at the touch of a key. However, it also has flexible features that allow data and processes to be manipulated simplistically. It can also be used to integrate themes that make it more attractive.
To install `bpytop`, follow these steps:

1.  As the **root** user, use the **dnf** command to install **bpytop**:

    ```

    [root@workstation ~]# dnf -y install bpytop

    ```

     2.  On the terminal, run **bpytop** as **root**:

    ```

    [root@workstation ~]# bpytop

    ```

The main monitor window will appear:
![Figure 5.46 – bpytop monitor](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_05_46.jpg)

Figure 5.46 – bpytop monitor
Note
To learn more about **bpytop** monitor usage, refer to the **bpytop GitHub page** at [`github.com/aristocratos/bpytop`](https://github.com/aristocratos/bpytop).
These excellent monitoring tools allow us to track the performance of network connections.
Summary
In this chapter, we reviewed how to connect our workstation to a network, by cable or wirelessly. Besides learning how to fine-tune this connection through best practices, we reviewed how to use VPNs in detail, as well as how to connect our workstation with an IPsec-based VPN and OpenVPN. Finally, we reviewed two monitoring tools that can help us track the performance of the network’s connection.
In the next chapter, we will start learning about applications. We will begin by looking at some modern ways of using them, such as using *sandbox containers*.
Further reading
To learn more about the topics that were covered in this chapter, take a look at the following resources:

*   *Fedora Linux 36 no longer supports the ifcfg files to* *configure networking*:

[`fedoraproject.org/wiki/Releases/36/ChangeSet#No_ifcfg_by_default`](https://fedoraproject.org/wiki/Releases/36/ChangeSet#No_ifcfg_by_default)

*   *nm-settings-nmcli*:

[`developer-old.gnome.org/NetworkManager/stable/nm-settings-nmcli.html`](https://developer-old.gnome.org/NetworkManager/stable/nm-settings-nmcli.html)

*   *Ask Fedora –* *Wi-Fi*:

[`ask.fedoraproject.org/search?q=wifi%20tags%3Af37%2Cwifi%20order%3Aviews`](https://ask.fedoraproject.org/search?q=wifi%20tags%3Af37%2Cwifi%20order%3Aviews)

*   *Fedora Docs:* *Using firewalld*:

[`docs.fedoraproject.org/en-US/quick-docs/firewalld/`](https://docs.fedoraproject.org/en-US/quick-docs/firewalld/)

*   *Fedora* *Wiki: OpenVPN*:

[`fedoraproject.org/wiki/OpenVPN`](https://fedoraproject.org/wiki/OpenVPN)





第三部分:生产力工具

在本部分中,您将学习如何使用在专业商务环境中最常见的生产力工具,从文本编辑器、办公应用程序、邮件客户端到网页浏览器,首先介绍了在 Fedora Linux 上安装应用程序的不同方法。

本部分包含以下章节:

  • 第六章沙盒应用程序

  • 第七章文本编辑器

  • 第八章LibreOffice 套件

  • 第九章邮件客户端和浏览器

第八章:沙箱应用程序

为了为孩子们提供一个安全的游玩场所,您会创建一个围墙区域,并将他们的玩具存放在一个小盒子(或容器)里。当这个概念应用到游乐场时,我们称之为沙箱。在应用程序开发中,沙箱这个术语来源于这个概念。

沙箱化方法中,每个应用程序都驻留在一个沙箱中,这是一个受控和限制的环境,用于运行其代码。这个环境帮助开发者隔离和保护系统资源。

开发者还使用沙箱化环境来识别应用程序的行为,并检测其行为中的错误或其他不良元素。

对于 Linux 环境,有几种方法可以沙箱化应用程序。在本书中,我们专注于其中的一些方法,包括被认为是最不具侵入性的(AppImage)以及由 Fedora Project 开发和支持的(Flatpak)。

在本章中,我们将涵盖以下主要主题:

  • 检查沙箱应用程序

  • 深入研究 AppImage 应用程序

  • 检查 Flatpak 应用程序

技术要求

对于沙箱化或沙箱应用程序的开发,您需要安装一些软件包。大部分软件包都包含在 Fedora 的官方仓库中。

在每一节中,您将看到所需的软件包以及如何安装每个软件包。

如果我们使用任何未包含在官方 Fedora 仓库中的软件包或代码,我们将提供获取链接并给出安装说明。

本章中创建的示例可以在本书的 GitHub 仓库中找到,您可以通过以下 URL 访问:github.com/PacktPublishing/Fedora-Linux-System-Administration/tree/main/chapter6

检查沙箱应用程序

在开始之前,我们必须做出一个区分。应用程序隔离一直被视为安全限制措施,无论是在发生入侵还是资源使用过度的情况下。

通过这一点,我们可以根据应用程序对资源的使用及其与主机系统的特权文件的互动关系来区分应用程序的开发。

然后,基于上述所需的抽象来托管系统中的代码,我们可以将应用程序类型化如下:

  • 本地编译:它在系统上运行,具有所有系统限制和特权。

  • 解释:解释器逐行执行应用程序的代码,并将每条指令运行到系统中。过去,使用解释性语言创建的应用程序比使用编译性语言创建的应用程序要慢得多。但随着即时编译的发展,这一差距得到了缩小。

  • Jailed:它允许应用程序代码在系统上运行,但在一个受限位置。这种限制可能包括系统资源或甚至系统文件。监禁的结构可能看起来像系统,但对监禁内文件的更改或修改不会影响主机系统的行为。

  • Sandbox:这是一种比监禁更受限的环境。沙箱只包括运行应用程序和与系统交互所需的文件。此特性使得应用程序的移植变得更加高效。主机系统的发行版不会影响沙箱应用程序的运行时,只要求发行版支持该沙箱类型。

沙箱应用程序的开发可以非常简单,从在受控环境中打开系统上安装的应用程序,到从其他发行版移植应用程序,而不需要进行所有涉及使其在每个发行版上运行的开发工作。

让我们看看如何创建一个简单的沙箱应用程序。然后,了解 Fedora 中常见的沙箱应用程序打包方式。

SELinux 沙箱

SELinux是一组允许我们为系统环境添加安全层的策略。基于这些策略,我们可以为应用程序设置沙箱。

注意

SELinux 将在本书的第十二章中详细介绍。

例如,让我们在与系统环境隔离的沙箱中运行Firefox浏览器。为此,请按照以下步骤操作:

  1. 作为root用户,安装 SELinux 沙箱工具。使用dnf命令安装policycoreutils-sandbox包:

    [root@workstation ~]# dnf install policycoreutils-sandbox
    
  2. 将 SELinux 配置为Permissive模式以允许 SELinux 沙箱。使用setenforcegetenforce命令验证更改:

    [root@workstation ~]# setenforce 0
    [root@workstation ~]# getenforce
    Permissive
    
  3. 作为一个非特权用户,打开终端并在 SELinux 沙箱内,以1280x1024的窗口运行firefox。使用sandbox命令:

    $ sandbox command runs its own *X server* (-X) and enables policy enforcement for network and browser usage (-t sandbox_net -t sandbox_web_t). Set up sandbox and run firefox in a *1280x1024 window* (-w 1280x1024 firefox):
    

图 6.1 – Firefox 在 SELinux 沙箱中的运行

图 6.1 – Firefox 在 SELinux 沙箱中的运行

让我们了解如何监控沙箱的性能

  1. 使用ps命令查找沙箱的 PID:

    $ ps auf | grep firefox
    acallej+   30511  0.0  0.9 ...output omitted...
    

图 6.2 – 查找沙箱的 pid

图 6.2 – 查找沙箱的 pid

  1. 使用top命令监控找到的PID的性能:

    $ top -p 30511
    

图 6.3 – 监控沙箱性能

图 6.3 – 监控沙箱性能

网络浏览器被隔离,因此它的行为不会影响系统的其他部分。让我们看看如何验证这一点。

  1. 在 Firefox 沙箱中,打开 Fedora 项目主页(start.fedoraproject.org/):

图 6.4 – 使用 Firefox 沙箱进行浏览

图 6.4 – 使用 Firefox 沙箱进行浏览

通过点击 Firefox 窗口右上角的X按钮来关闭 Firefox 沙箱。

  1. 再次在沙箱中运行 Firefox:

    $ sandbox -X -t sandbox_net -t sandbox_web_t -w 1280x1024 firefox
    

    使用 Ctrl + H 打开浏览历史:

图 6.5 – 检查浏览历史

图 6.5 – 检查浏览历史

  1. 再次关闭 Firefox 沙箱窗口。

请注意,没有关于访问过的网站的信息被保存。这些信息会保存在一个系统文件中。Firefox 在隔离模式下运行,这些信息存储在沙箱中。当沙箱关闭时,存储在其中的信息会被删除。

SELinux 沙箱有许多实际应用,例如,当一个 不可信的文件 需要被打开时。

对于一个 pdf 文件,作为示例,在沙箱中运行 evince 来阅读它。使用 sandbox 命令:

$ -i /home/acallejas/findings.pdf).
Note
For more information on the use of the **sandbox** command, refer to the **manual** by running **man sandbox**.
The SELinux sandbox is one of the most basic ways to isolate an application. Using this idea, it is now taken a step further using different frameworks or forms to package the applications to make them portable to different environments, easing their installation.
Let’s look at the most common examples of sandbox applications: **AppImages** and **Flatpaks**.
Diving deep into AppImage apps
Introduced in 2004 as **klik** by *Simon Peter* and renamed in 2011 as **PortableLinuxApp**, it was finally named in 2013 as we know it today: **AppImage**.
**AppImage** is a universal portable distribution format, also known as *upstream packaging*.
AppImages, as portable applications, do not need installation by the user. They also do not need administrator privileges to run. The user downloads the AppImage and assigns it execution permissions, and the application starts.
For a developer, creating AppImages is quite simple. An AppImage consists of a program package with dependency libraries and all the resources it needs during runtime.
AppImages are unique binaries, following the basic principle of *one application =* *one file*.
The tools to generate an AppImage from an **AppDir**. They are aware of possible incompatibilities between distributions and try to avoid them.
Once the AppImage is built, it runs on all major desktop distributions.
Let’s see how to run an AppImage as a user. Then, let’s walk through the development of an AppImage.
Running an AppImage
An AppImage is an image of the application.When you are running it, it mounts on the filesystem in the user space. Just give it *execution permissions* and double-click on it.
AppImages do not have an *application store* from which to download them, but there is a place to find and download hundreds of applications, known as **AppImageHub** ([`appimage.github.io/apps/`](https://appimage.github.io/apps/)).
For example, to download the developer version of **Firefox Nightly** and test new features without installing it, follow the subsequent steps:

1.  Open the Firefox browser and go to [`appimage.github.io/apps/`](https://appimage.github.io/apps/). Press the *Ctrl* + *F* key combination and search for the **firefox** string:

![Figure 6.6 – Searching for the Firefox Nightly AppImage](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_6.jpg)

Figure 6.6 – Searching for the Firefox Nightly AppImage

1.  Click on the **Firefox_Nightly** link:

![Figure 6.7 – Firefox_Nightly download window](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_7.jpg)

Figure 6.7 – Firefox_Nightly download window
Scroll down and click on the **Download** button.

1.  This button takes us to the GitHub repository where the AppImage resides. Click on the *latest version* to download it:

![Figure 6.8 – Latest version in AppImage format](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_8.jpg)

Figure 6.8 – Latest version in AppImage format
Wait for the download to finish.

1.  Open a terminal and switch to the **Downloads** directory. Review the downloaded AppImage by running a long listing. Use the **ls** command:

![Figure 6.9 – Checking the AppImage](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_9.jpg)

Figure 6.9 – Checking the AppImage

1.  Grant *execution permissions* to AppImage using the **chmod** command:

    ```

    $ chmod +x firefox-nightly-113.0.r20230321213816-x86_64.AppImage

    ```

     2.  Open the file browser and change parent directory to the **Downloads** directory. Right-click on the AppImage icon and click on **Properties**. Verify that the **Executable as program** switch button is enabled:

![Figure 6.10 – AppImage properties](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_10.jpg)

Figure 6.10 – AppImage properties

1.  Close the **Properties** window by clicking on **X** in the upper-right corner.
2.  Double-click on the AppImage icon. The Firefox Nightly window opens:

![Figure 6.11 – Firefox Nightly on AppImage](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_11.jpg)

Figure 6.11 – Firefox Nightly on AppImage
With these simple steps, the application runs without being installed. If we prefer, we could create a *launcher* and add it to the shortcuts of the taskbar.
Let’s see now how to develop AppImages.
Developing AppImages
Developing AppImages is very simple. The **AppImage** project provides **AppImageKit**,  an implementation of the AppImage format focused on the *tiny runtime* of each one.
Note
AppImageKit is available to download at [`github.com/AppImage/AppImageKit/releases/continuous`](https://github.com/AppImage/AppImageKit/releases/continuous).
The core components of AppImageKit are as follows:

*   **runtime**: The runtime provides an executable header for an AppImage. The runtime mounts the filesystem image to a temporary location. Such a filesystem image is called an *AppDir*. It then launches the payload application. Downloading it is not required.
*   **Appimagetool**: This creates the AppImages by embedding the runtime and attaching the AppDir inside it. This tool comes as an AppImage.
*   **AppRun**: This provides the entry point of the AppImage. The runtime executes this file inside the AppDir. It doesn’t need to be a regular file; it could be a symlink to the main binary.

Note
The project adds and deprecates extra tools in the AppImageKit all the time, even those from third parties. So, keep an eye out for updates to the GitHub repository.
To create an AppImage, according to the project documentation, follow these steps:

1.  Download **appimagetool** using the **wget** command:

    ```

    $ wget \

    > https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-x86_64.AppImage

    ```

     2.  Add execution permissions with the **chmod** command:

    ```

    $ chmod a+x appimagetool-x86_64.AppImage

    ```

     3.  To create the AppDir, use a high-level tool such as **linuxdeployqt**. Assuming that the AppDir already has everything needed to run the application, run the **appimagetool** command to generate the AppImage:

    ```

    $ ./appimagetool-x86_64.AppImage some.AppDir

    ```

With this simple procedure, the new AppImage is created and ready to distribute.
Note
For more information on the use of AppImageKit, refer to the GitHub repository at [`github.com/AppImage/AppImageKit`](https://github.com/AppImage/AppImageKit).
So, now we know how to create and use AppImages, it’s time to look at the proposal drawn up by the Fedora Project itself. Let’s take a walk through the **flatpaks**.
Examining Flatpak applications
**First** is one of Fedora’s founding principles, so **Fedora Linux** is always on the *cutting edge*.
Flatpak is a new distribution-independent format for packaging and distributing Linux desktop applications.
The main goals of Flatpak include the following:

*   Create a single installation file that could be distributed to users of all distributions
*   Run applications that are as isolated as possible from the rest of the system

The biggest benefit to users is the ability to run any application, regardless of the version of Fedora Linux they use.
Its development name was `xdg-app` and it was renamed **Flatpak** in 2016 to reflect the fact that it became ready for wider use.
Flatpak is a system for building, distributing, and running sandboxed desktop applications on Linux.
Flatpak applications are installed through the `flatpak` command or through a GUI such as **GNOME Software** or **KDE Discover**.
**Flatpak** introduces some basic concepts, such as the following:

*   **Runtime**: A platform that provides the basic utilities needed for a Flatpak application to run.
*   **BaseApp**: An integrated platform for frameworks such as *Electron*.
*   **Flatpak bundle**: A specific export format for a single file. It contains one application. It is also known as *Flatpak runtime*.
*   **Sandboxes**: Applications build and run in an isolated environment. Only the content of the sandbox can be accessed by the program. Access to other resources, such as processes other than the sandbox, is not possible.
*   **Portals**: The mechanism through which applications interact with the host environment from within a sandbox. It provides interaction with data, files, and services without the need to add permissions to the sandbox.
*   **Repositories**: Flatpak applications and runtimes get stored and published via repositories such as Git repositories, which in some cases are named *registries*.

Let’s now look at the mechanisms for running Flatpak applications, and then we’ll walk a little bit through the development of Flatpak applications.
Using Flatpak applications
Flatpak is installed by default on Fedora desktop variants.
Flatpak applications are *fully integrated* into the operating system’s package manager. We can install them using the graphical interface or the command line.
Let’s first look at the simplest form of installation using the GUI.
Using the GUI
The origin of installable applications might come as a package built on the OS, not sandboxed or built in a sandbox.
For the end user, there is no difference, but it is possible to find out the installation format of an application from the software manager. It is very simple. For example, let’s inspect the format that installed the Firefox browser and its installation options. Follow the subsequent steps:

1.  As a *non-root user*, open the **Activities Overview** menu and click on **Software**:

![Figure 6.12 – Activities Overview menu](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/6.12.jpg)

Figure 6.12 – Activities Overview menu

1.  Click on the *search* icon in the upper-left corner. In the search field, type **firefox** and press *Enter*:

![Figure 6.13 – Search for the application](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/6.13.jpg)

Figure 6.13 – Search for the application
Click on the **Firefox** icon, shown as **Installed**.

1.  Below the **Open** button, find the *installation source*. Click on the drop-down list:

![Figure 6.14 – Firefox installation source](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/6.14.jpg)

Figure 6.14 – Firefox installation source
Installation options for Firefox include the following:

*   **FLATPAK**: From the official Fedora registry ([registry.fedoraproject.org](http://registry.fedoraproject.org)) as a Flatpak application
*   **RPM**: From the official Fedora repositories ([fedoraproject.org](http://fedoraproject.org)) as RPM

In this case, it shows that Firefox was installed from the official Fedora repositories as a built RPM.
So, to install a Flatpak application with the software manager, just select the installation source.
Let’s install a *digital painting editor* as an example. Follow the subsequent steps:

1.  In the search field of the software manager, type **krita**:

![Figure 6.15 – Search for the application](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/6.15.jpg)

Figure 6.15 – Search for the application
Click on the first result.

1.  Verify the installation source as a Flatpak application. Below the **Install** button, click on the drop-down list:

![Figure 6.16 – Verifying the installation source](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/6.16.jpg)

Figure 6.16 – Verifying the installation source
Choose **Flatpak** as the installation source.

1.  Click the **Install** button. Wait until the installation is complete.
2.  Click the **Open** button:

![Figure 6.17 – Open the application](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/6.17.jpg)

Figure 6.17 – Open the application
The application opens and is ready for use:
![Figure 6.18 – Krita as a Flatpak application](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_18.jpg)

Figure 6.18 – Krita as a Flatpak application
As mentioned earlier, the end user experiences no difference with applications built on `rpm`. Through the command line, it is possible to inspect the resources used and the isolation of the application from the system along with other options such as adding other repositories of Flatpak applications.
Let’s now see how to use the command line to manage Flatpak applications.
Using the CLI
As mentioned in *Chapter 1*, the use of the command line expands the system administration capabilities. Let’s analyze the performance of the Flatpak application installed in the previous section. Follow the subsequent steps:

1.  As a **root** user, open a terminal and use the **ps** command to search for the **xdg-dbus-proxy** and **krita** processes:

    ```

    [root@workstation ~]# ps axf | egrep "krita|xdg-dbus-proxy"

    13340 ?  S   0:00  |   \_ bwrap --args 42 krita

    13359 ?  S   0:00  |       \_ bwrap --args 42 krita

    13360 ?  Sl  0:04 |           \_ krita

    13353 ?  S   0:00 \_ bwrap --args 42 xdg-dbus-proxy --args=44

    xdg-dbus-proxy 是一个用于 *D-Bus* 连接的过滤代理。它是 Krita 应用程序与系统交互的门户。

    ```

     2.  Using the method of the first section of this chapter, track the performance of both processes. Use the **top** command:

    ```

    [root@workstation ~]# top -p 13353,13360

    ```

![Figure 6.19 – Flatpak application performance](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_19.jpg)

Figure 6.19 – Flatpak application performance
As mentioned at the beginning of this section, `flatpak` comes installed by default in Fedora Linux. It is available as a command to build, install, and run applications and runtimes. It could operate at the local or wide level, as a `root` or *non-root* user.
Let’s start to explore the capabilities of the `flatpak` command line.

1.  Use the **flatpak** command to list the Flatpak applications installed:

    ```

    [root@workstation ~]# flatpak list --app

    ```

![Figure 6.20 – Listing Flatpak applications](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_20.jpg)

Figure 6.20 – Listing Flatpak applications

1.  To inspect the changes to Flatpak applications on the system, use the **flatpak** command with the **history** option:

    ```

    [root@workstation ~]# flatpak history

    ```

![Figure 6.21 – Flatpak history](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_21.jpg)

Figure 6.21 – Flatpak history
These changes include installation, update, or removal, covering applications and runtimes.

1.  To show the application details, use the **flatpak** command with the **info** option and the **Application ID** of the application:

    ```

    [root@workstation ~]# flatpak info org.kde.krita

    ```

![Figure 6.22 – Flatpak application info](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_22.jpg)

Figure 6.22 – Flatpak application info

1.  To display the running applications, use the **flatpak** command with the **ps** option:

    ```

    [root@workstation ~]# flatpak ps

    ```

![Figure 6.23 – Running Flatpak applications](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_23.jpg)

Figure 6.23 – Running Flatpak applications
Observe that the `root` user is not running any Flatpak applications.
Switch to the *non-root* user and run the same command:
![Figure 6.24 – Running Flatpak applications](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_24.jpg)

Figure 6.24 – Running Flatpak applications
Let’s now see how we can add Flatpak applications from the command line. But before that, it is important to identify the installation source.
As mentioned before, Flatpak applications in Fedora Linux are set by default and point to the official registry of the distribution.
Through the command line, it’s possible to add other repositories as installation sources and install more Flatpak applications because they are not in the Fedora registry, the version is different, or just to have more installation sources.
Let’s verify the general repository of Flatpak applications as an installation source. Follow the subsequent steps:

1.  As a **root** user, open a terminal and use the **flatpak** command to list the repositories:

    ```

    [root@workstation ~]# flatpak remotes

    ```

![Figure 6.25 – Flatpak repositories](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_25.jpg)

Figure 6.25 – Flatpak repositories

1.  Add more columns to show the details of the repositories. Use the **–** **–****columns=name,title,url,homepage** parameter:

![Figure 6.26 – Flatpak application repositories](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_26.jpg)

Figure 6.26 – Flatpak application repositories

1.  To list the applications available in a repository, use the **flatpak** command with the **remote-ls** option and the **Name** repository. To avoid *runtimes* and only list the applications, add the **–****app** parameter:

    ```

    [root@workstation ~]# flatpak remote-ls flathub --app

    ```

![Figure 6.27 – Applications available on Flathub](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_27.jpg)

Figure 6.27 – Applications available on Flathub
In *step 1*, *Figure 6**.25* shows that the `flathub` repository appears as *filtered*.
Note
This is true for versions before Fedora Linux 38, where the Flathub filter was removed.
This is because the Fedora Linux configuration points to `Fedora Flatpaks`. `Fedora Flatpaks` is the remote Flatpak repository of the Fedora Project.
The difference is that Flathub makes applications and tools *as accessible as possible*, no matter what distribution they’re used in.
On the other hand, `Fedora Flatpaks` pushes RPMs from the Fedora Project and makes them accessible through Fedora Linux as Flatpak applications.
To have access to all the Flathub applications, let’s add the repository again without restrictions.

1.  Add the **flathub** repository using the **flatpak** command with the **remote-add** option. Add the **--if-not-exists** parameter [to prevent *reg*](https://flathub)*istry overwriting* (the repository URL is [`flathub.org/repo/flathub.flatpakrepo`](https://flathub.org/repo/flathub.flatpakrepo)):

    ```

    # flatpak remote-add --if-not-exists flathub \

    > .org/repo/flathub.flatpakrepo

    ```

![Figure 6.28 – Adding the Flathub repository](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_28.jpg)

Figure 6.28 – Adding the Flathub repository

1.  List the repositories again using the **flatpak** **remotes** command:

    ```

    [root@workstation ~]# flatpak remotes

    ```

![Figure 6.29 – Flatpak repositories](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_29.jpg)

Figure 6.29 – Flatpak repositories
Note that Flathub is no longer *filtered*. Let’s now see what Flatpak applications offer us.

1.  List the applications available in the **flathub** repository. Use the **flatpak remote-ls flathub –****app** command:

![Figure 6.30 – Flathub applications](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_30.jpg)

Figure 6.30 – Flathub applications
If we get the number of applications, we find that it has increased:

flatpak remote-ls flathub --app | wc -l


*   **2164**

Let’s install an application.

1.  Switch to a *non-root* user and search for the **Telegram Desktop** app. Use the **flatpak remote-ls flathub –app** command and filter the **Telegram** **Desktop** string:

    ```

    $ flatpak remote-ls flathub --app | grep "Telegram Desktop"

    ```

![Figure 6.31 – Searching for the Telegram Desktop Flatpak application](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_31.jpg)

Figure 6.31 – Searching for the Telegram Desktop Flatpak application
Before installing it, let’s get the details of the Flatpak application.

1.  Use the **flatpak remote-info** command with the **flathub** repository option and the **Application ID** from **Telegram Desktop**:

    ```

    $ flatpak remote-info flathub org.telegram.desktop

    ```

![Figure 6.32 – Telegram Desktop info](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_32.jpg)

Figure 6.32 – Telegram Desktop info

1.  To install the application, use the **flatpak install** command with the **flathub** repository option and the **Telegram Desktop** **Application ID**:

    ```

    $ flatpak install flathub org.telegram.desktop

    ```

    Here’s the output:

    ```

    正在寻找匹配项…

    org.telegram.desktop/x86_64/stable 所需的运行时 (runtime/org.freedesktop.Platform/x86_64/22.08) 已在远程 flathub 中找到

    您想要安装它吗?[Y/n]:Y 来安装。等待安装完成:

    ```

![Figure 6.33 – Telegram Desktop installation](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_33.jpg)

Figure 6.33 – Telegram Desktop installation

1.  Once installed, use the **flatpak run** command with the **Application ID** to run the application:

    ```

    $ flatpak run org.telegram.desktop

    ```

![Figure 6.34 – Telegram Desktop Flatpak application](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/6.34.jpg)

Figure 6.34 – Telegram Desktop Flatpak application

1.  After installation, it is possible to find it in the **Activities** menu and/or add it to **Favorites** to display the icon in the top bar:

![Figure 6.35 – Searching for Telegram Desktop in the Activities menu](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/6.35.jpg)

Figure 6.35 – Searching for Telegram Desktop in the Activities menu
Both procedures, graphical and CLI, provide an intuitive way to use Flatpak applications, regardless of the distribution or version installed.
Note
For more information on the use of Flatpak applications, [refer to th](https://docs)e *Using Flatpak* section of Flatpak’s documentation at [`docs.flatpak.org/en/latest/using-flatpak.html`](https://docs.flatpak.org/en/latest/using-flatpak.html).
Now, let’s walk through the process of creating Flatpak applications.
Building Flatpak applications
Building Flatpak applications is relatively simple. As a prerequisite, you must install, as a `root` user, the `flatpak-builder` package, either as an RPM or Flatpak application:

*   RP

    # **dnf** **list flatpak-builder**

*   Flatpak application

    # **flatpak install** **flathub org.flatpak.Builder**

As an example, let’s create a Flatpak application based on a simple `bash` script. To create our Flatpak application, follow the subsequent steps:

1.  Identify the runtime using the **flatpak remote-ls** command with the **-a** (*all*) parameter. To search both runtimes and applications in the **flathub** repository, filter the **freedesktop platform** string to find the latest version. As an optional filter, add a parameter to avoid **translations**:

    ```

    # flatpak remote-ls flathub -a | grep -i \

    > "freedesktop platform" | grep -v translations

    ```

![Figure 6.36 – Identifying the runtime](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_36.jpg)

Figure 6.36 – Identifying the runtime
From the preceding figure, we can see that the last version is `22.08`. Next, we’ll search for the same version of the SDK.

1.  Use the **flatpak remote-ls** command with the **-a** (*all*) parameter in the **flathub** repository. Filter the **freedesktop SDK** string and the **22.08** version:

    ```

    # flatpak remote-ls flathub -a | grep -i \

    > "freedesktop SDK" | grep 22.08

    ```

![Figure 6.37 – Searching the SDK](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_37.jpg)

Figure 6.37 – Searching the SDK

1.  Use the **flatpak install** command to install both:

    ```

    # flatpak install flathub \

    org.freedesktop.Platform//22.08 \

    org.freedesktop.Sdk//22.08

    ```

    *   Looking for matches…

        ```

        跳过:org.telegram.desktop/x86_64/22.08 已安装

        ```

    ```

    您想要安装它吗?[Y/n]:Y

    ```

    Wait for the installation to finish:

![Figure 6.38 – SDK installation](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_38.jpg)

Figure 6.38 – SDK installation

1.  Create a **bash** script. For example, as a *non-root* user, copy the following and save it as **script.sh**:

    ```

    #!/bin/sh

    echo "Hello world, I'm a flatpak application"

    ```

     2.  Add the *manifest*. Each Flatpak application includes a file with basic build information known as the *manifest*. Create a file with the following and save it as **org.flatpak.script.yml** in the same directory as the **bash** script:

    ```

    app-id: org.flatpak.script

    runtime: org.freedesktop.Platform

    runtime-version: '22.08'

    sdk: org.freedesktop.Sdk

    command: script.sh

    modules:

    - name: script

    buildsystem: simple

    build-commands:

    - install -D script.sh /app/bin/script.sh

    sources:

    - type: file

    path: script.sh

    ```

     3.  Build the Flatpak application using the **flatpak-builder** command with the target directory and the *manifest*:

    ```

    $ flatpak-builder build-dir org.flatpak.script.yml

    ```

![Figure 6.39 – Building the Flatpak application](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_39.jpg)

Figure 6.39 – Building the Flatpak application

1.  Install the Flatpak application using the **flatpak-builder** command. Add the **--user** option to install dependencies on the user’s local installation. With the **--force-clean** option, delete the previously created directory. This removes the contents of the directory and creates new build content:

    ```

    $ flatpak-builder --user --install --force-clean \

    > build-dir org.flatpak.script.yml

    ```

![Figure 6.40 – Installing the Flatpak application](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_40.jpg)

Figure 6.40 – Installing the Flatpak application

1.  Run the Flatpak application using the **flatpak** **run** command:

    ```

    $ flatpak run org.flatpak.script

    ```

![Figure 6.41 – Running the Flatpak application](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_06_41.jpg)

Figure 6.41 – Running the Flatpak application
This way, we have a Flatpak application based on a simple script with all the necessary sandbox and isolation features.
In a more complex case, the *manifest* should include all the necessary dependencies and files, declared as *modules*.
Note
For more information on the build of Flatpak applications, refer to the *Building* section of Flatpak’s documentation at [`docs.flatpak.org/en/latest/building.html`](https://docs.flatpak.org/en/latest/building.html).
This concludes our tour of sandbox applications. In the following chapters, we will discuss the installation options for different applications. Some should be installed via RPM and some others should be available as AppImages or Flatpak applications.
Summary
In this chapter, we learned how sandbox applications work, from a very illustrative example using SELinux and Firefox, to portable application formats widely used today.
We explored AppImage apps, from how to run them to using the AppImageKit to generate AppImages.
Finally, we took a deep look at Flatpak applications, a format that supports the Fedora Project and even maintains its own version of Fedora Flatpaks based on RPMs.
We also learned how to use the command line to extend the administration and use of sandboxed applications.
In the next chapter, we will take a walk through the most popular terminal-based text editors, plus look at some usage tricks and customizations.
Further reading
To learn more about the top[ics covered in this](https://flatpak.org/) chapter, you can visit the foll[owing links:](https://fedoramagazine.org/getting-started-flatpak/)

*   [*Flatpak*: https://flatpak.org/](https://fedoramagazine.org/getting-started-flatpak/)
*   [*Getting*](https://fedoramagazine.org/getting-started-flatpak/) *Started with* *Flatpak*: https://fedoram[agazine.org/getting-started-flatpak/](https://fedoramagazine.org/an-introduction-to-fedora-flatpaks/)
*   [*An introduction to Fedora*](https://fedoramagazine.org/an-introduction-to-fedora-flatpaks/)*Flatpaks*: https://fedoramagazine.org/an-introductio[n-to-fedora-flatpaks/](https://fedoramagazine.org/comparison-of-fedora-flatpaks-and-flathub-remotes/)
*   [*Comparison of Fedora Flatpaks and Flathub* *remotes*: http](https://fedoramagazine.org/comparison-of-fedora-flatpaks-and-flathub-remotes/)s://fedoramagazi[ne.org/comparison-of-fedora-flatpaks-and-flathub-remotes/](https://developer.fedoraproject.org/deployment/flatpak/flatpak-usage.html)
*   [*Flatpak* *Usage*:](https://developer.fedoraproject.org/deployment/flatpak/flatpak-usage.html) https://de[veloper.fedoraproject.or](https://flathub.org/home)g/deployment/flatpak/flatpak-usage.html
*   *Flathub*: https://flathub.org/home

第九章:文本编辑器

第一章 中,我们建议你始终使用命令行来编辑文件。由于实践是提升我们控制台技能的唯一途径,我们应该利用每一个机会来这样做。文件编辑是配置系统时最基本也是最常用的技能。

一些文本编辑器甚至允许我们在操作系统上执行命令。这使我们能够练习这些命令。我们还可以通过安装插件或定制配置来扩展它们的功能。这些修改使我们能够格式化文件、验证语法并执行任务。

除了它们的用途外,我们还将讨论最流行文本编辑器的基本配置技巧和定制。

在本章中,我们将覆盖以下主要主题:

  • 文本编辑器和命令行

  • Emacs 概述

  • Nano 基础

  • 强大的 Vim

让我们开始吧!

技术要求

为了执行本章中提到的配置、定制和示例,需要安装vimnano软件包的基础版本emacs和其他一些软件包可能需要额外安装。大多数软件包都包含在 Fedora 官方仓库中。

在每个部分,你将看到所需的软件包以及如何安装每一个。

在使用未包含在官方 Fedora 仓库中的软件包或代码时,我们会提供获取链接并附上安装说明。

本章中创建的示例可以在书籍的 GitHub 仓库中下载:github.com/PacktPublishing/Fedora-Linux-System-Administration/tree/main/chapter7

文本编辑器和命令行

UNIX 操作系统具有一些独特的思想和概念,这些思想和概念塑造了其设计。UNIX 是第一个将所有 I/O 操作抽象化并统一概念的操作系统。UNIX 的创始人将这一概念称为文件。每个文件通过相同的应用程序编程接口API)暴露自己。这种抽象化提供了许多优点,例如防止重复代码并提高可重用性。

要读取/写入磁盘、键盘、文档或网络设备,可以使用相同的一组基本命令(例如 catmoregrepsedecho 命令)。

这个原则被概括为:“一切皆文件”。

这个基本概念有两个方面:

  • 在 UNIX 中,一切皆字节流

    关于文件的引用,称为文件描述符,I/O 访问使用相同的一组操作和相同的 API(无论设备类型和底层硬件是什么)。作为字节流,它允许以下操作:

    • 阅读

    • 写入

    • 搜索

    • 关闭

  • 在 UNIX 中,文件系统充当一个通用的命名空间

    文件不仅仅是具有一致 API 的字节流,它们还可以以统一的方式相互引用。UNIX 文件系统的路径提供了一种一致的、全局的资源标签方案,不管它们的性质如何。这个全局命名空间通常被视为文件和目录的层次结构。这是一个方便的抽象,文件路径可以指向几乎任何东西:一个文件系统、一个设备、一个网络共享或一个通信通道。

这意味着在 UNIX 中,进而在 Linux 中,任何东西都可以作为文件在文件系统空间中访问。所有这些的目的是使得不同的工具能够用于不同的事情。

用于更改文本文件内容的工具是文本编辑器。这些文件包含纯文本源代码,无论是程序的代码还是文本格式化程序的代码,后者读取输入并生成可打印的输出。

UNIX/Linux 系统上文本编辑器的主要优点是能够在没有图形环境的情况下使用它们,在终端中操作。这个优点使得它们可以在远程机器上使用。由于不需要传输整个图形环境,使用文本编辑器可以提高网络的速度。作为系统管理员,你需要像专家一样从终端编辑文件。

nanovim 是任何 Linux 发行版的基础包。在某些情况下,这些软件包是开箱即用的,还有一些发行版也包含了 emacs

这些软件包实现的是相同的功能(编辑文本文件),但它们以非常不同的方式实现:

  • nano:最易于使用,但功能最少。

  • vim:学习曲线最陡峭,但一旦掌握,功能强大。

  • emacs:使用难度介于 nanovim 之间。功能强大且可定制。并不总是作为基础包提供。

让我们深入探讨每一个编辑器。我们将从历史上最有分量的一个开始——GNU Emacs,由 Richard Stallman 创建。在 Fedora Workstation 中,需要安装它才能使用。让我们先了解一下它的概况。

Emacs 概述

早期的文本编辑器非常原始,以至于无法打开整个文本文件。为了解决这个问题,文本编辑器变成了一种命令,它能够生成单词并将其写入文件、查找和替换文件中的单词、删除文件中的行等。

以这种方式编辑大型文档可能会变得相当无聊。因此,人们开始开发 来执行常见的相关任务。

1983 年,Richard Stallman 发布了一个名为 Editing Macros 或简称 Emacs 的宏包。当 Stallman 博士启动 GNU 项目时,GNU Emacs 成为了他最成功的应用。

GNU Emacs 是为 POSIX 操作系统设计的文本编辑器。由于它是在现代计算机惯例和术语出现之前开发的,因此通常被认为是复杂的。

然而,GNU Emacs 提供了高效的命令,用于执行常见但复杂的操作,并且有着超过 40 年的插件和配置 hack 开发历史。一旦你掌握了 GNU Emacs 的基本使用,它将是一个强大、高效且可定制的编辑器,适用于一生。

最常见的 Emacs 版本是 GNU Emacs。

要在我们的 Fedora 工作站上安装它,可以通过主菜单中的软件应用选择 RPMFLATPAK 作为安装源,就像我们在 第六章 中讲解的那样:

图 7.1 – 安装 Emacs

图 7.1 – 安装 Emacs

使用控制台也可以列出可用的插件。切换到 root 用户并运行以下命令:

# dnf list emacs*

图 7.2 – Emacs 和插件

图 7.2 – Emacs 和插件

列出并统计插件,总数不到 100 个:

# dnf list emacs-* | wc -l
85

按照你喜欢的方法安装 GNU Emacs,我们从基础开始。

基础知识

一开始,程序的可用性看起来似乎很复杂。但这只是因为它是在另一个时代创建的,所以稍有不同。按照这些指示开始适应用户界面吧。

在搜索框中打开 Emacs

图 7.3 – 从菜单中打开 Emacs

图 7.3 – 从菜单中打开 Emacs

作为 非 root 用户,也可以通过终端使用 emacs 命令打开:

$ emacs

在这两种情况下,GNU Emacs 窗口都会打开:

图 7.4 – GNU Emacs

图 7.4 – GNU Emacs

在欢迎屏幕上,第一个链接,Emacs 教程,将带你进入应用程序的帮助教程:

图 7.5 – Emacs 教程

图 7.5 – Emacs 教程

你可以通过按键组合返回该界面,随时需要时都可以。

大多数按键组合使用 Ctrl 键,并结合其他所需的按键组合。例如,打开教程时,按键组合是 C-h + C-t,意味着先按 Ctrl 键加 h 键,再按 Ctrl 键加 t 键;也就是你可以按住 Ctrl 键,先按 h 键,再按 t 键。

在某些情况下,Alt 键也用于按键组合。但其表示方法不同,用 M 表示。例如,使用 Alt-x 组合时,表示方法是 M-x

这是因为 Alt 键过去被称为 Meta 键。请记住,在 GNU Emacs 开发时,许多当前的术语还没有被使用。

注意

字母 M 代表 Meta 键,在 x86 计算机上是 Alt 键。Emacs 也有其他平台的版本,比如在 macOS 上,Meta 键指的是 Cmd 键。

使用图形界面有助于快速上手 GNU Emacs。菜单中包括了每个任务所用的按键组合:

图 7.6 – GNU Emacs 菜单

图 7.6 – GNU Emacs 菜单

以下表格展示了最常见的任务及其相应的按键组合:

文件操作
打开文件
保存文件
另存为
关闭文件
退出
复制与粘贴
剪切
复制
帮助
显示所有快捷键绑定
教程

表 7.1 – GNU Emacs 常见的键盘组合

GNU Emacs 非常灵活,且在没有图形界面的终端中运行。这使得它成为系统管理中的一个必不可少的工具。

现在,让我们来看一下 GNU Emacs 的高级用法概述。

精通 GNU Emacs

乍一看,GNU Emacs 看起来像是一个非常复杂的文件编辑方式,因为它使用的键盘组合与其他文本编辑器不同。但对于系统管理员的日常任务,从终端运行时,GNU Emacs 是一个强大的解决方案。

让我们通过一个小示例来概览一下这个过程。按照以下步骤操作:

  1. 在终端中通过运行 emacs 命令并带上 --no-window-system 参数来打开 GNU Emacs

    $ emacs --no-window-system
    

图 7.7 – 终端中的 GNU Emacs

图 7.7 – 终端中的 GNU Emacs

  1. 从启动屏幕,浏览到要修改的测试文件。

  2. 按下 Ctrl 键加 x 键 (C-x) 进入命令模式,然后按 Ctrlf 键 (C-f) 查找要编辑的文件。按 Tab 键显示目录的内容:

图 7.8 – 查找要编辑的文件

图 7.8 – 查找要编辑的文件

  1. 要编辑的文件是 test 文件。在底部的 查找文件 中输入 test 来打开该文件。

图 7.9 – 打开测试文件

图 7.9 – 打开测试文件

注意

前面提到的文件可以在本书的 GitHub 仓库中找到,链接:github.com/PacktPublishing/Fedora-Linux-System-Administration/tree/main/chapter7

这个 test 文件是 /etc/group/etc/passwd 文件的组合。

图 7.10 – 测试文件

图 7.10 – 测试文件

  1. 假设你需要查看 test 文件中两个文件的内容。在其中一个文件结束的地方,分割屏幕并在它们的内容之间移动。

    要分割屏幕,按下 Ctrl 键加 x 键和 2 键 (C-x-2):

图 7.11 – 分割屏幕

图 7.11 – 分割屏幕

  1. 在 Emacs 中,缓冲区包含一个进程或一个文件。窗口是缓冲区的视图。框架是桌面窗口。

    以下图示展示了这些概念:

图 7.12 – 缓冲区、窗口和框架

图 7.12 – 缓冲区、窗口和框架

这意味着我们将框架分割成两个窗口。每个缓冲区都编辑同一个 test 文件。

如果需要,每个缓冲区可以打开不同的文件,分割框架为更多的水平和垂直窗口,或者打开其他框架。

  1. 要更改缓冲区,按 Ctrl 键和 x 键加 o 键 (C-x-o)。

  2. 通过在文件内容中添加一行加号(+)来分隔内容。通过按 Ctrl 键和 x 键(C-x)加上 Ctrl 键和 s 键(C-s)来保存文件。

图 7.13 – 保存更改

图 7.13 – 保存更改

  1. 通过按下 Ctrl 键和 x 键(C-x)加上 Ctrl 键和 c 键(C-c)来退出保存的文件。

下表显示了高级任务及其对应的键组合:

文本选择
开始选择
取消选择
撤销
撤销
惊慌!
取消正在进行的命令或操作
复制和粘贴
粘贴
光标后剪切行
光标后剪切单词
导航
上一行
下一行
后退一个字符
后退一个单词
前进一个字符
前进一个单词
转到行首
转到行末
框架、窗口和缓冲区
垂直分割
水平分割
无分割
移除窗口
访问其他窗口
打开新框架

表 7.2 – GNU Emacs 高级键组合

现在让我们看看如何为文本编辑器添加增强功能。

GNU Emacs 的动力

GNU Emacs 具有一些基本特性。并非所有实现,但是已经构想:

  • 它有很多插件。并非所有文本编辑器都有插件,也并非所有都需要插件。有时唯一需要的功能是简单性。对于每天使用几小时的文本编辑器,插件提供了宝贵的增强和改进。

  • 它是一个可定制的环境,可以成为一个完整的桌面环境,例如,跟踪项目、任务和事件,组织电子邮件和聊天,管理远程服务器。

  • 提供强大和灵活的键组合和可为 特定模式 *配置的键映射。

  • 环境的每一个方面都是可定制的。它允许修改所有应用程序的用户界面:帮助系统,文件和工作区选项卡,以及用户界面元素的字体和颜色。

通过 GNU Emacs 安装这些增强功能。按照以下步骤进行:

  1. 通过按下 MetaAlt)键和 x 键列出 GNU Emacs 中的可用软件包。进入软件包命令模式,窗口底部显示 M-x 组合键:

图 7.14 – 软件包命令模式

图 7.14 – 软件包命令模式

  1. 输入 list-packages 并按 Enter 键:

图 7.15 – 列出的软件包

图 7.15 – 列出的软件包

窗口显示了可安装的软件包:

图 7.16 – 可用软件包列表

图 7.16 – 可用软件包列表

  1. 要查找一个包,例如,yaml-mode,以帮助我们处理 YAML 语法。在列表中,按下 Ctrl 键和 s 键(C-s)。在底部输入 yaml-mode 查找所选包:

图 7.17 – 搜索待安装的软件包

图 7.17 – 搜索待安装的软件包

注意

这需要安装emacs-yaml-mode.noarch软件包才能正常工作。

  1. 要查看软件包描述,按下MetaAlt)键和x键(M-x),然后输入describe-package

图 7.18 – 描述软件包

图 7.18 – 描述软件包

  1. 这将窗口水平分割。要切换到描述窗口,请按下Ctrl键和x键,然后按o键(C-x-o):

图 7.19 – 描述窗口

图 7.19 – 描述窗口

  1. 通过按下Ctrl键和x键,然后按0键(C-x-0)来关闭窗口。

  2. 通过按下MetaAlt)键和x键(M-x),然后输入package-menu-mark-install来标记yaml-mode软件包进行安装。

图 7.20 – 标记待安装的软件包

图 7.20 – 标记待安装的软件包

这将在软件包的左侧添加一个I

图 7.21 – 标记待安装的软件包

图 7.21 – 标记待安装的软件包

  1. 要安装软件包,按下MetaAlt)键和x键,然后输入package-install-selected-packages

图 7.22 – 安装标记的软件包

图 7.22 – 安装标记的软件包

  1. 使用C-x + C-c退出 Emacs。

    安装过程非常快速。打开一个 YAML 文件,查看软件包的帮助菜单和语法:

图 7.23 – YAML 文件

图 7.23 – YAML 文件

如前图所示,许多软件包和插件可以安装,帮助我们实现特定的功能。

注意

在互联网上,你会找到很多软件包和插件的例子。作为个人推荐,可以查看这个 Reddit 帖子,里面是社区推荐的必备软件包:www.reddit.com/r/emacs/comments/w4gxoa/what_are_some_musthave_packages_for_emacs/

GNU Emacs,尤其是对于新手用户,似乎使用起来非常复杂。这导致了更简单文本编辑器的普及。最受欢迎的文本编辑器之一是Nano,它成为了 Fedora Linux 的默认文本编辑器。

现在我们来回顾一下它使用的基本原则。

Nano 基础

GNU nano 是作为 Pico 文本编辑器的免费替代品设计的,而 Pico 是华盛顿大学的 Pine 电子邮件套件的一部分。

1999 年底,Chris AllegrettaSlackware转到Debian。他想念一个包含PinePico的软件包。所以,他决定制作一个 Pico 的克隆程序,当时叫做Tip Isn’t PicoTIP)。

在 2000 年 1 月,TIP 因与另一个名为tip的程序发生冲突(该程序包括在许多旧版 Unix 系统中)而更名为nano

2001 年 2 月,nano 获得了 Richard Stallman 的正式 GNU 声明。nano也在 2001 年 3 月 22 日达到了第一次生产版本。

要创建新文件,作为非 root用户,可以从终端运行nano命令:

$ nano

图 7.24 – Nano 文本编辑器

图 7.24 – Nano 文本编辑器

在窗口的底部任务栏上,有编辑器的帮助命令:

图 7.25 – Nano 帮助命令

图 7.25 – nano 帮助命令

emacs不同,nano使用脱字符符号(^)表示Ctrl键,因此按下Ctrl键和G键会显示帮助菜单。要退出,请按下CtrlX键。

注意

有关为什么使用脱字符符号(^)表示Ctrl,请参考retrocomputing.stackexchange.com/questions/10925/why-do-we-use-caret-as-the-symbol-for-ctrl-control

除了Ctrl键,nano还使用Alt键和一些符号作为特定任务命令的一部分。

下表显示了最常见的任务及其对应的快捷键组合:

文件管理
显示帮助
退出
保存当前文件
将另一个文件插入当前文件
编辑
在光标位置插入新行
删除光标下的字符
删除光标左侧的字符
在光标位置插入制表符
对齐当前段落
对齐整个文件
统计单词数、行数和字符数
查找与替换
查找字符串或正则表达式
替换字符串或正则表达式
重复上次搜索

表 7.3 – GNU nano 常见快捷键组合

GNU nano 以其简洁著称,无论是界面还是使用方式。

另一个文本编辑器,既不如nano那么简单,也不如emacs那么复杂,就是 vim 编辑器。

让我们逐步了解 Vim 编辑器及其自定义功能。

强大的 vim

VimVi IMproved的缩写)是Vi文本编辑器的增强版,存在于所有 UNIX 系统中。Bram Moolenaar 在 1991 年推出了第一个版本。

Vim 是一个功能强大的编辑器,提供了许多开箱即用的功能。通过使用插件,可以增加新的功能,使得 Vim 更容易适应特定的工作流程。许多插件专注于常见功能,例如定位文件、提供信息、处理不同的文件语法或 Git 版本控制。

Vim 包含在 Fedora Linux 的基础安装中,尽管不是默认的文本编辑器。要将其更改为默认文本编辑器,可以将其添加到 shell 配置文件中。在 Bash 中,这个文件是~/.bashrc~/.bash_profile

添加以下全局变量,将 vim 编辑器设置为默认编辑器:

export VISUAL=vim
EDITOR variable.
A `VISUAL` editor is a full-screen editor such as vim or emacs.
An `EDITOR` editor works without using the terminal’s functionality (such as the old `ed` or `ex` mode of vi).
For example, when invoking an editor through Bash, Bash tries the `VISUAL` editor first. If `VISUAL` fails (because the terminal doesn’t support a full-screen editor), it tries `EDITOR`.
With both variables set to `vim`, grant their use as the default text editor.
Unlike emacs or nano, vim does not use key combinations. The vim editor works in two modes: *Command* and *Insert*. Now let’s review its basic usage.
The basics
The vim modes are essential in its use. To switch between modes, use the *Esc* key. The following figure shows the editing flow for a file with vim:
![Figure 7.26 – Vim workflow](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_26.jpg)

Figure 7.26 – Vim workflow
To create or open a file with vim, run the `vim` command in the terminal. This opens vim to its splash screen, in command mode:
![Figure 7.27 – Vim splash screen](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_27.jpg)

Figure 7.27 – Vim splash screen
To start typing, and to switch to insert mode, press the *Esc* key and then the *I* key. The screen changes. In the upper-left corner the cursor appears, and in the bottom taskbar, the mode appears as **INSERT**:
![Figure 7.28 – Insert mode](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_28.jpg)

Figure 7.28 – Insert mode
After typing, to return to command mode, press the *Esc* key. This clears the mode in the bottom bar.
![Figure 7.29 – Command mode](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_29.jpg)

Figure 7.29 – Command mode
The normal mode contains a command-line mode. Here, a variety of tasks, such as saving or quitting the file, can be run.
To enter command-line mode, press the colon key (`:`) and type the command for the desired task. The following table shows the commands for the most common tasks:

  Save/Quit
 |

  Write buffer
 |
  `:w`
 |

  Quit
 |
  `:q`
 |

  Write and quit
 |
  `:``wq`
 |

  Abandon buffer
 |
  `:``q!`
 |

  Other
 |

  Find down
 |
  `:/[``string]`
 |

  Set numbers
 |
  `:``se nu`
 |

  No numbers
 |
  `:``se nonu`
 |

Table 7.5 – Commands for common tasks
In normal mode, also, file editing tasks can be run. No need to press the colon key (`:`). Just run the command for the editing task needed. The following table shows the most common editing commands:

  Insert
 |

  After cursor
 |
  `a`
 |

  At the end of line
 |
  `A`
 |

  Before cursor
 |
  `i`
 |

  At beginning of line
 |
  `I`
 |

  Open line below
 |
  `o`
 |

  Open line above
 |
  `O`
 |

  Yank word
 |
  `yw`
 |

  Yank line
 |
  `yy`
 |

  Put (paste)
 |
  `p`
 |

  Change
 |

  Word
 |
  `cw`
 |

  Line
 |
  `cc`
 |

  Rest of line
 |
  `C`
 |

  Under cursor
 |
  `s`
 |

  Replace character
 |
  `r`
 |

  Delete
 |

  Word
 |
  `dw`
 |

  Line
 |
  `dd`
 |

  Rest of line
 |
  `D`
 |

  Under cursor
 |
  `x`
 |

  Before cursor
 |
  `X`
 |

  Transpose
 |
  `xp`
 |

Table 7.6 – Commands for editing tasks
Note
Vim includes a *tutor* to learn how to use it. To start the tutor, run the **vimtutor** command.
The flexibility and power of Vim make it an essential tool in system administration.
Let’s now get an overview of the advanced use of Vim.
Mastering vim
For many people, `vim` seems as complex as `emacs`. But in practice, with reference to *Figure 7**.26*, the editing flow is simpler. The only thing to get used to is switching between modes.
As proof of this, let’s use the same example used earlier with `emacs` to edit the `test` file, composed of the contents of the `/etc/passwd` and `/etc/group` files. Follow these steps:

1.  In the terminal, as a *non-root user*, run the **vim** command. To browse for the file, type a colon (**:**) and the letter **e** with a space. Type the letter **t** to browse for the **test** file and then press the *Tab* key. Vim shows in the bottom bar the files whose names begin with **t**:

![Figure 7.30 – Browse for the test file](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_30.jpg)

Figure 7.30 – Browse for the test file

1.  To split the screen, as in **emacs**, type a colon (**:**) and then the **split** command:

![Figure 7.31 – Split screen](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_31.jpg)

Figure 7.31 – Split screen

1.  To switch screens, press the *Ctrl* key plus the *w* key twice (*Ctrl+ww*). Scroll down until you find the plus sign (**+**) characters added in the **emacs** example:

![Figure 7.32 – Search for plus characters](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_32.jpg)

Figure 7.32 – Search for plus characters

1.  Let’s replace these plus signs (**+**) with asterisks (*****). Type a colon (**:**) and type the percent sign (**%**) plus the **s** letter. This indicates to **vim** that this is a substitution. Next, type a slash (**/**) to state the string to be replaced and again another slash (**/**) to type the characters that will replace the original string. Type another slash (**/**) and then the letter **g** to tell **vim** that the substitution must take place *globally*:

![Figure 7.33 – Replacing characters](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_33.jpg)

Figure 7.33 – Replacing characters

1.  Press the *Enter* key and **vim** performs the substitution. The bottom bar displays a message of the completed task:

![Figure 7.34 – Modifying the test file](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_34.jpg)

Figure 7.34 – Modifying the test file

1.  To save and quit the file, type a colon (**:**) followed by the letters **w** and **q**, and then press the *Enter* key.

Note
On the internet, you can find more commands and advanced uses of **vim**. A very useful resource is the post *50 Useful Vim* *Commands*: [`vimtricks.com/p/50-useful-vim-commands/`](https://vimtricks.com/p/50-useful-vim-commands/).
Pimp my vim
Like `emacs`, Vim expands its capabilities with the installation of plugins and packages, in addition to customizing the interface to provide information or help edit the file. Beyond the look and feel, it adds features that increase the usability of the editor.
Unlike `emacs`, Vim uses a variety of initialization files where settings and customizations are included. Some of the most relevant files are as follows:

*   `:help doc-file-list` to get the complete list.
*   `tags` file used to find information in the documentation files
*   **/****usr/share/vim/vim90/syntax/syntax.vim**

    System-wide syntax initializations

*   **/****usr/share/vim/vim90/syntax/*.vim**

    Syntax files for various languages

*   **/****usr/share/vim/vimrc**

    System-wide Vim initializations

*   **~/.****vimrc**

    Personal Vim initializations

*   `:options` command. A nice way to view and set options.

The file that contains the Vim initializations is `vimrc`. The `vimrc` file is a collection of the following:

*   Configurations
*   Custom commands
*   Custom functions
*   Mappings
*   Plugins

Let’s take a look at some examples of each of them.
Configurations
In normal mode, it is possible to configure Vim to add some features. For example, to add numbers to lines to identify them, type a colon (`:`) followed by `set nu`:
![Figure 7.35 – Numbered lines](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_35.jpg)

Figure 7.35 – Numbered lines
This setting only remains enabled as long as the editor is open. Closing the editor disables it.
Keep this setting enabled by adding it to the `.``vimrc` file.
These settings could help with syntax while editing the file. For example, when editing YAML files, you could configure the `.vimrc` file in a basic way as follows:

set ts=2 ← [1]

set et ← [2]

set ai ← [3]


*   [**1**] A tab is two spaces
*   [**2**] Expand tabs
*   [**3**] Auto-indenting

Custom commands
In Vim, we can create custom commands to perform tasks. For example, we could create a command that tells us today’s date in various formats:

*   In the **.vimrc** file, add the following line:

    ```

    :command! TellDate echo call("strftime", [<args>])

    ```

     *   In Vim command mode, run the **TellDate** command as follows:

    ```

    :TellDate "%F"

    ```

![Figure 7.36 – Custom date](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_36.jpg)

Figure 7.36 – Custom date

*   To get the current time, run the **TellDate** command as follows:

    ```

    :TellDate "%H:%M"

    ```

![Figure 7.37 – Current time](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_37.jpg)

Figure 7.37 – Current time
Custom functions
As seen previously, Vim allows us to run a built-in or custom command from command mode. But it is not limited to one; it could run a set of commands or series of instructions within the same command mode. Or they get written to a file and generated in a running Vim instance (`:source path/to/file`). Some of these files come up as part of the configuration from the `.``vimrc` file.
These scripts use **Vimscript** (also called **VimL**). This **Vim** ex-command subset provides many features that could be expected from a scripting language, such as values, variables, functions, and loops.
It is very common for a script to mix features of a scripting language and raw ex-commands.
The `.vimrc` file includes some files describing functions that extend the capabilities of the text editor. These functions get called using the native Vimscript `let` function, which assigns a value to a variable.
The function structure uses the following syntax:

{functionName}() 函数

{some_tasks}

endfunction


 The following table shows the most common functions of Vimscript:

  Buffer
 |
  Marks
 |

  Current line number
 |
  `line('.')`
 |
  Position of a mark
 |
  `getpos("'a")`
 |

  `col('.')`
 |
  `setpos("'a",...)`
 |

  `col('$')`
 |
  Position of selection start
 |
  `getpos("'<")`
 |

  Current line as string
 |
  `getline('.')`
 |
  Cursor
 |

  Get line 1
 |
  `getline(1)`
 |
  Moves cursor
 |
  `cursor(line,col)`
 |

  Get lines 1-5
 |
  `getline(1, 5)`
 |
  `cursor(line,col,off,curswant)`
 |

  Next blank line, returns line number
 |
  `search('^$')`
 |
  Returns [bufnum, line, col, off, curswant]
 |
  `getcurpos()`
 |

  Search but don’t move cursor
 |
  `search('^$','n')`
 |
  Date/time
 |
  `strftime('%c')`
 |

   |   |  `strftime('%c', getftime('file.c'))`
 |

Table 7.7 – Vimscript common functions
Mappings
Vim allows us to customize key mappings. It is useful to change the definition of the keys typed on the keyboard. The most common use is to define a sequence of commands for a function key. For example, if want to map the `x` key to the `dd` command to delete lines, in normal mode, in the `.vimrc` file, add the following line:

nmap x dd


 Mapping applies in four modes: *normal*, *visual*, *selection*, and *operator-pending*.
For help with mapping, commands, and modes, run the following in command mode:

:help map.txt


 ![Figure 7.38 – Mapping help](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_38.jpg)

Figure 7.38 – Mapping help
Plugins
Vim plugins are installed via packages. A Vim package is a *directory* or *repository* that contains one or more plugins that depend on each other. Among its main features are the following:

*   Downloaded as a compressed file and unzipped in its own directory, which avoids it mixing with others and makes it easy to update and remove
*   Contain plugins loaded at startup and others that are only loaded when needed with **:packadd**

The directory recommended to install Vim packages inside is the user’s `.``vim` directory.
To exemplify the process, let’s install the most used plugin: **Vim Airline**.
Vim Airline is a plugin that replaces the standard Vim status line with a useful status bar, and provides useful information about the working file, including the following:

*   Filename
*   Save status
*   File type
*   Encoding
*   Position
*   Word count

**Vim mode**
To install Vim Airline, follow these steps:

1.  In the home directory of the *non-root user*, as the *non-root user*, create the **.****vim** directory:

    ```

    [acallejas@workstation ~]$ mkdir -p .vim

    ```

     2.  Change to the **.vim** directory. Download the Vim Airline package from its repository on GitHub ([`github.com/vim-airline/vim-airline`](https://github.com/vim-airline/vim-airline)):

    ```

    $ cd .vim

    $ wget https://github.com/vim-airline/vim-airline/archive/refs/tags/v0.11.tar.gz

    ```

![Figure 7.39 – Download Vim Airline](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_39.jpg)

Figure 7.39 – Download Vim Airline

1.  Decompress the Vim Airline package:

    ```

    $ tar xzf v0.11.tar.gz

    ```

![Figure 7.40 – Decompressing the package](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_40.jpg)

Figure 7.40 – Decompressing the package

1.  Move the Vim Airline version directory as **vim-airline**

    ```

    $ mv vim-airline-0.11 vim-airline

    ```

![Figure 7.41 – Renaming a directory](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_41.jpg)

Figure 7.41 – Renaming a directory

1.  Create the **pack/dist/start** path. Move the **vim-airline** directory into the **pack/dist/start** path:

    ```

    $ mkdir -p pack/dist/start

    $ mv vim-airline pack/dist/start/

    ```

![Figure 7.42 – Relocating the vim-airline directory](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_42.jpg)

Figure 7.42 – Relocating the vim-airline directory
Vim Airline is a Vimscript version of Powerline ([`github.com/powerline/powerline`](https://github.com/powerline/powerline)) written in Python.
Powerline includes a font with pretty symbols. To get these symbols with Vim Airline, install the patched font.

1.  As the **root** user, or with **sudo** if a non-root user has the privileges, install the **powerline-fonts** package using the **dnf** command:

    ```

    $ sudo dnf install powerline-fonts

    ```

![Figure 7.43 – Installing the Powerline font](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_43.jpg)

Figure 7.43 – Installing the Powerline font

1.  Switch to the home directory and edit the **.vimrc** file to enable the font use. Add the line **let g:airline_powerline_fonts =** **1**:

    ```

    $ cd

    $ vim .vimrc

    ```

![Figure 7.44 – Enabling font usage](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_44.jpg)

Figure 7.44 – Enabling font usage

1.  Edit the **test** file and check the new status bar:

    ```

    $ vim test

    ```

![Figure 7.45 – vim-airline status bar](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_45.jpg)

Figure 7.45 – vim-airline status bar

1.  When changing modes, the bar also changes its color to differentiate between the modes:
    *   This is what it looks like in Insert mode:

![Figure 7.46 – Insert mode](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_46.jpg)

Figure 7.46 – Insert mode

*   The following is what it looks like in Command mode:

![Figure 7.47 – Command mode](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_07_47.jpg)

Figure 7.47 – Command mode
Note
Changing or customizing the color scheme is available. For further reference, visit the **vim-airlines-themes** repository on GitHub: [`github.com/vim-airline/vim-airline-themes`](https://github.com/vim-airline/vim-airline-themes).
This way, the plugin installation and configuration work in our editor.
There are many plugins, some very specialized, that complement Vim, with a similar installation.
Note
On the internet, you’ll find many examples of packages and plugins. As a personal recommendation, visit these posts with specialized recommendations to improve Vim:

*   *5 useful Vim tplugins for* *developers*: [`opensource.com/article/19/1/vim-plugins-developers`](https://opensource.com/article/19/1/vim-plugins-developers)
*   *Top five Vim plugins for* *sysadmins*: [`www.redhat.com/sysadmin/five-vim-plugins`](https://www.redhat.com/sysadmin/five-vim-plugins)

This concludes our walk-through of the text editors. In the following chapters, we’ll make use of them, adding plugins or packages for specialized tasks to help us with the system administration of our Linux systems.
Summary
In this chapter, we learned in depth about text editors, beginning with the fundamental understanding that in Unix/Linux, *everything is a file*. Thus, the use of the terminal as a tool to edit files eases the tasks of system configuration.
We took our first steps in **Emacs**. While it is known to be a very complex editor for novices, we offered a basic guide to get started with it.
We also reviewed the use of **Nano**, the default text editor in several distributions, including Fedora Linux.
We also deep-dived into the use of **Vim**, a well-known and recommended file editing tool for file management.
The decision is up to you, dear reader, to choose an editor of your preference.
In the next chapter, we will review the **LibreOffice** suite and its different programs for creating and editing commonly used file formats.
Further reading
To learn more about the topics covered in this chapter, you can visit the following links:

*   The Linux Documentation Project: Text editors

    [`tldp.org/LDP/intro-linux/html/sect_06_01.html`](https://tldp.org/LDP/intro-linux/html/sect_06_01.html)

*   *9 Best Text Editors for the Linux* *Command Line*

    [`itsfoss.com/command-line-text-editors-linux/`](https://itsfoss.com/command-line-text-editors-linux/)

*   *Linux skills: 9 tutorials to get more from your* *text editor*

    [`www.redhat.com/sysadmin/text-editor-roundup-2022`](https://www.redhat.com/sysadmin/text-editor-roundup-2022)

*   *A beginner’s guide to text editing* *with Emacs*

    [`www.redhat.com/sysadmin/beginners-guide-emacs`](https://www.redhat.com/sysadmin/beginners-guide-emacs)

*   *How to get started with the* *Vi editor*

    [`www.redhat.com/sysadmin/get-started-vi-editor`](https://www.redhat.com/sysadmin/get-started-vi-editor)

第十章:LibreOffice 套件

GNU/Linux 支持 办公套件 组织的标准化、优化和改进。办公套件是任何行业中最常见的工具之一,其目标是使工作更加实用和动态。办公套件是一组程序,允许你创建、修改、组织、存储、发送、接收、扫描和打印文件。办公套件的基本程序包括文字处理器、电子表格和用于项目的图像和/或演示文稿编辑器。

LibreOffice 是一款开源的办公套件。最初名为 OpenOffice,但当 Apache 收购它时,一些用户创建了一个分支,成为了 LibreOffice。

所有发行版和用户都采用了 LibreOffice,加速了其开发。LibreOffice 出现在最流行的 GNU/Linux 发行版中,除了 Flatpak 包之外,其安装非常简单。

本章我们将涵盖以下主要内容:

  • 探索 Fedora Linux 上的办公工具

  • 适应 Writer 和 Calc

  • 创建幻灯片和图像管理

让我们开始吧!

技术要求

LibreOffice 默认安装在 Fedora Linux 工作站版本中,我们在第二章中安装了该版本。本章的内容开发仅要求你确认是否按照前述章节的步骤操作。如果没有,请确保在 Fedora Linux 工作站上安装 LibreOffice。

本章中创建的示例可在本书的 GitHub 仓库中下载,网址为:github.com/PacktPublishing/Fedora-Linux-System-Administration/tree/main/chapter8

探索 Fedora Linux 上的办公工具

Fedora 项目 并没有开发自己的办公套件,但它提供了一套默认的办公应用程序,旨在具有广泛的吸引力并提供有用的功能。官方软件库包含了 LibreOffice 组件。

除了 LibreOffice,Fedora Linux 还为你提供了安装其他办公套件的可能性,包括模板或字体等附加组件。

在深入了解 LibreOffice 的组件之前,让我们简要了解一下这些软件包。

WPS Office

WPS Office 是一个 跨平台(Windows、Linux、Android 和 iOS)生产力套件,适用于计算机和移动设备。它被认为是一个高性能但 专有 的解决方案,与 Microsoft Office 兼容且可相提并论。它的 WriterPresentationSpreadsheet 组件是强大的解决方案,类似于微软的 PowerPointExcelWord

WPS Office 套件由 WPS Office 软件公司生产,WPS Office 软件公司是金山公司旗下的一家公司,金山公司是中国领先的互联网服务和软件公司。它与 Microsoft Office 格式完全兼容,并提供多种语言(超过 10 种)支持的拼写检查器。

在 Fedora Linux 上,它可以通过 Flathub 提供的 Flatpak 包安装。

注意

Flatpak 包附带警告,说明该 Flatpak 包未经 Kingsoft Office Corporation 验证、授权或支持。

ONLYOFFICE

ONLYOFFICE Desktop Editors 是一个开源办公套件,适用于 Linux、Windows 和 macOS,依据AGPLv3协议自由分发。它由三个编辑器组成,分别用于文本文件、电子表格和演示文稿,并且与 Microsoft Office 格式本地兼容。

ONLYOFFICE Desktop Editors 提供了连接云平台的可能性,如 ONLYOFFICE、Nextcloud、ownCloud、Seafile、Liferay 和 kDrive,并允许在团队文档中进行协作,包括实时共同编辑、审阅、评论和通过聊天互动。

注意

Affero 通用公共许可证是一个copyleft 许可证,源自GNU 通用公共许可证。它旨在确保在网络上运行的软件能够得到社区的合作,并且增加了一个义务:如果软件提供网络服务,就必须分发该软件。

自由软件基金会建议任何在网络上运行的软件都应使用GNU AGPLv3许可证。你可以在www.gnu.org/licenses/agpl-3.0.html找到更多信息。

在 Fedora Linux 上,ONLYOFFICE Desktop Editors 可以通过 Flathub 提供的 Flatpak 包安装。

Calligra

Calligra 是一个由 KDE 创建的办公套件。你无需安装 Plasma 桌面环境即可使用它,因为它在其他桌面环境中也能正常工作,例如使用 GNOME 的 Fedora 工作站。

Calligra 采用 OASIS 开放文档格式作为其原生文件格式。开放文档格式ODF)是一种基于 XML 的开源办公应用文件格式,用于包含文本、电子表格、图表和图形元素的文档。

注意

如需了解更多关于OASIS 开放文档格式的信息,请参考 www.oasis-open.org/committees/tc_home.php?wg_abbrev=office

Calligra 的主要组件如下:

  • Calligra Words:一个文字处理器,围绕主窗口组织,用于编辑和书写文本,右侧面板提供访问最常用工具的功能。

  • Calligra Stage:一个演示文稿编辑器,突出其模板,尽管模板不多,但为定制演示文稿提供了一个良好的基础。

  • Calligra Sheets:用于电子表格编辑。如同之前的例子一样,它提供针对特定情况的模板。

  • Calligra Plan:一个帮助你组织和层级化任务的工具。

  • Karbon:一个支持多页文档和导入 PDF 文件的矢量图形编辑器。

在 Fedora Linux 上,每个组件都可以单独作为 RPM 包或 Flatpak 包进行安装,由 Fedora Flatpaks 提供。

字体

Fedora Linux 默认安装了超过 70 种字体。然而,在其软件仓库中,提供了超过 1,000 种字体。要从官方仓库安装更多字体,请按照以下步骤操作:

  1. 作为非 root 用户,在左上角打开菜单并点击软件

图 8.1 – 打开菜单

图 8.1 – 打开菜单

  1. 点击左上角的搜索图标。在搜索框中输入fonts,然后按回车键

图 8.2 – 搜索字体

图 8.2 – 搜索字体

  1. 选择所需的字体并安装它——例如,搜索字体julietaula Montserrat Alternates,然后点击字体图标。点击安装按钮:

图 8.3 – 安装字体

图 8.3 – 安装字体

等待安装完成。

安装结束后,该字体可以在办公套件中使用。

注意

有关Montserrat 字体项目的更多信息,请参考其在 GitHub 上的仓库:github.com/JulietaUla/Montserrat

现在我们已经了解了 LibreOffice 的替代选项以及如何安装更多字体,接下来我们来看看 Fedora Linux 的默认办公套件。

LibreOffice

LibreOffice 是OpenOffice.org的继任项目,通常被称为OpenOffice

OpenOffice.org于 2000 年开始,在 Sun Microsystems 发布 StarOffice 5.2 的源代码并允许个人免费使用后。

2010 年,Oracle 公司收购了 Sun Microsystems。这让OpenOffice.org社区的成员感到担忧,因为 Oracle 对待开源软件的众所周知的行为(例如Java 诉讼案Google对 Oracle 提起诉讼)。

同年,OpenOffice.org社区的成员宣布了文档基金会,这是一个旨在为办公套件的开发提供持续性的非盈利基金会。该基金会的主要项目是 LibreOffice,这是OpenOffice.org的一个分支。

LibreOffice 增加了额外的功能,并通过定期发布和安全更新改善了与 Microsoft Office 的兼容性。

LibreOffice 的主要组成部分如下:

  • Writer:一个文字处理器

  • Calc:一个电子表格编辑器

  • Impress:一个幻灯片编辑器

Fedora Linux 在其软件仓库中包含了 LibreOffice 套件的附加组件:

  • Base:一个数据库管理器

  • Draw:一个绘图工具

任何已安装的 LibreOffice 组件程序都可以通过搜索栏中的Libre访问,如下图所示:

图 8.4 – 搜索 LibreOffice 组件

图 8.4 – 搜索 LibreOffice 组件

LibreOffice 组件也在菜单中作为Office类别进行分组。要访问它们,请按照以下步骤操作:

  1. 作为一个非管理员用户,在左上角打开菜单,然后点击所有 应用图标:

图 8.5 – 菜单中的所有应用

图 8.5 – 菜单中的所有应用

  1. 在菜单提供的类别中,点击Office类别:

图 8.6 – Office 类别

图 8.6 – Office 类别

  1. Office类别中,找到已安装的 LibreOffice 组件:

图 8.7 – 安装的 LibreOffice 组件

图 8.7 – 安装的 LibreOffice 组件

  1. 要访问所需的组件,请点击其图标。

无论哪种访问方式,都可以打开 LibreOffice 组件并使用它们来创建和编辑文档。

让我们更仔细地看看每一个 LibreOffice 组件。

适应 Writer 和 Calc

文本编辑器和电子表格是办公套件中最常用的工具。通过它们,关于管理系统的文档得以呈现,我们将在后续章节中讨论Linux系统管理时涉及的内容。

让我们来看看这些 LibreOffice 组件的每一个。

Writer

LibreOffice Writer是该套件中的文字处理软件。Writer是一个文字处理软件,类似于Microsoft WordCorel’s Word Perfect,具有相似的功能和文件格式兼容性。Writer 的默认文件格式是OpenDocumentODT),但它也能够打开和编辑 Microsoft Word 文件,如DOCDOCXRTFXHTML

LibreOffice Writer 类似于 Microsoft Word(如下图所示),因为它有许多可以在微软产品中找到的选项。

打开 Writer 来创建或编辑文本文件时,你将看到活动页面以及各种编辑资源。

图 8.8 – LibreOffice Writer

图 8.8 – LibreOffice Writer

编辑资源包括以下内容:

  • 一个菜单,包含不同的标签:

    • 文件,包括打开或关闭现有文档或创建新文档的命令,此外还可以关闭应用程序。

    • 编辑,包括编辑活动文档的命令。

    • 视图包括显示界面视图和工具栏的命令。

    • 插入,包括将元素插入活动文档的命令。这些元素包括图像来自其他应用的对象超链接注释符号脚注章节

    • 格式,包括格式化活动文档内容的命令。

    • 样式,包括应用、创建、编辑、加载和管理文本文件中的样式的命令。样式有几种类别,如段落字符框架页面列表

    • 表格,包含插入、编辑和删除表格及其元素的命令,用于文本文档中。

    • 表单包含激活表单设计模式的命令,允许在文档中启用或禁用控件向导或控件表单。

    • 工具包含多种工具,包括拼写检查器、修订选项、邮件合并向导和扩展管理器,以及用于配置或自定义程序菜单和偏好的其他工具。

    • 窗口包含显示和操作文档窗口的命令。

    • 帮助提供访问 LibreOffice 帮助 资源的途径。

  • 各种工具栏

    • 标准工具栏在每个LibreOffice 套件应用程序中都可用。其功能包括以下内容:

      • 新建(文档)

      • 打开文件

      • 保存另存为

      • 电子邮件文档

      • 编辑模式

      • 导出 为 PDF

      • 直接打印文件打印预览

      • 拼写

      • 剪切复制粘贴克隆格式

      • 撤销重做

      • 超链接

    • 格式包含多种文本格式化功能。

  • 属性 侧边栏

    • 侧边栏位于活动文档显示区域的左侧或右侧。它提供了上下文属性样式管理文档导航媒体 画廊功能

上述功能和可访问的命令在 Writer 打开时默认加载,但可以通过启用其他工具栏来添加更多功能和命令。可以通过视图菜单下的工具栏选项启用这些工具栏:

图 8.9 – Writer 中可用的工具栏

图 8.9 – Writer 中可用的工具栏

注意

有关 LibreOffice Writer 中可用工具栏选项的更多信息,请参阅 LibreOffice Writer 帮助,网址为help.libreoffice.org/latest/zh-CN/

Writer 帮助我们在 Linux 系统管理中记录我们的流程。它可以通过多种适用的样式来改善文档。它还可以创建自己的样式和/或更改或自定义现有样式。

让我们来看一个如何在 LibreOffice Writer 中使用样式的例子。

在 LibreOffice Writer 中应用样式

在创建文档时,我们会格式化文本和段落以达到特定的外观和感觉。然而,这可能变成一项繁琐且耗时的任务,可以通过应用样式来解决。

LibreOffice Writer 通过应用样式将段落、字符和其他类似元素的格式属性链接起来。这不仅可以创建统一且专业的文档,还能通过计算文档的目录节省时间。

我们以一些小文本为例,命名为Lorem Ipsum Doc.txt,你可以在本书的 GitHub 仓库中找到该文件,位于chapter 8目录下(github.com/PacktPublishing/Fedora-Linux-System-Administration/blob/main/chapter8/Lorem%20Ipsum%20Doc.txt)。

图 8.10 – GitHub 仓库中的示例文本

图 8.10 – GitHub 仓库中的示例文本

按照以下步骤为文本应用样式:

  1. 打开 LibreOffice Writer。将文本复制到新文件中。将文件保存到一个已知位置,例如$HOME/Documents

图 8.11 – Lorem Ipsum 文档

图 8.11 – Lorem Ipsum 文档

注意

为了避免在此练习中高亮显示拼写错误,禁用自动拼写检查。从工具菜单中,取消勾选自动拼写检查复选框,或者按Shift + F7键。

  1. 增强屏幕上文本的可见性。在查看菜单中,点击缩放,然后选择页面宽度。注意,某些文本部分,如行或单词,似乎与段落分开。这表示段落之间有分隔符:

图 8.12 – 段落部分分隔符

图 8.12 – 段落部分分隔符

在 LibreOffice Writer 中,章节标题称为标题。标题按层级顺序排列——一级标题叫做Heading 1,二级标题叫做Heading 2,三级标题叫做Heading 3,依此类推。现在让我们告诉 Writer 示例文本中包含哪些标题和小节标题。

  1. 按下F11键以显示样式侧边栏:

图 8.13 – 样式侧边栏

图 8.13 – 样式侧边栏

因为我们将纯文本粘贴到文档中,所以重要的是要告诉 Writer 该文本是文档的正文

  1. 使用Ctrl + A键选择所有文本。双击文本正文样式侧边栏中的项目:

图 8.14 – 应用文本正文样式

图 8.14 – 应用文本正文样式

请注意,段落会自动分隔,因为该样式在段落之间有0.10”的间距。如果你想更改此默认间距,右键单击文本正文样式并选择修改。然后,在缩进和间距选项卡中,修改下方段落值,最后点击确定按钮。

图 8.15 – 修改“下方段落”值

图 8.15 – 修改“下方段落”值

  1. 将第一行设置为文档的标题。将光标放在第一行,然后点击样式侧边栏中的标题部分。双击标题

图 8.16 – 格式化标题

图 8.16 – 格式化标题

现在,格式化文本中的各个章节标题和小节标题。

引言理由讨论章节标题优缺点后续跟进讨论章节中的副标题。

  1. 将光标放在引言行上,并在样式侧边栏中双击标题 1

图 8.17 – 应用标题 1 样式

图 8.17 – 应用标题 1 样式

使用Ctrl + 1 键,将光标放在理由讨论的文字上,应用相同的样式。

  1. 将光标放在优缺点上,然后在样式侧边栏中双击标题 2。然后,在后续跟进副标题下添加一句话,并重复此操作,或者使用Ctrl + 2 键。

图 8.18 – 应用标题 2 样式

图 8.18 – 应用标题 2 样式

  1. 优缺点子部分,一些行以破折号-)开头,这意味着这些行构成一个列表。选择这些行,并在样式侧边栏中双击列表 1样式。此样式为这些行添加缩进,如果你添加更多行,它们将成为列表的一部分。

图 8.19 – 创建列表

图 8.19 – 创建列表

最后,让我们为格式化后的文档添加目录

  1. 将光标放在标题的末尾,按下Enter。点击插入菜单,选择目录和索引。点击目录、索引或参考书目目录、索引或参考书目窗口将会出现:

图 8.20 – 目录、索引或参考书目窗口

图 8.20 – 目录、索引或参考书目窗口

  1. 如有需要,编辑标题。在窗口的选项卡中,对表格的外观进行更改。点击确定按钮,将目录插入到文档中,完成示例。

图 8.21 – 添加目录

图 8.21 – 添加目录

通过这些简单的样式更改,你可以制作出更专业和统一的文档。所选样式可以自动应用,并且你可以通过右键单击目录并选择更新 索引选项来更新目录。

注意

欲了解更多有关在 LibreOffice Writer 中应用样式的信息,请参阅文档基金会 Wiki中的第三方资源,路径为Writer | Libre Office 样式教程,网址:wiki.documentfoundation.org/Documentation/Third_Party_Resources

其他LibreOffice组件中的属性侧边栏、一些工具栏应用样式的过程保持不变。

现在,让我们概览其他组件及其可用编辑工具栏的差异。

Calc

LibreOffice Calc 是该套件的电子表格组件,用于计算、分析和管理数据。它支持导入和修改 Microsoft Excel 电子表格。LibreOffice Calc 的界面与 Microsoft 相应的界面类似:

图 8.22 – LibreOffice Calc

图 8.22 – LibreOffice Calc

它的主要功能包括以下内容:

  • 计算功能:LibreOffice Calc 提供包括统计函数和银行函数在内的多种函数,用于创建公式并对数据进行复杂计算。它还包括一个函数向导,帮助你创建公式。

  • 假设计算:可视化对由多个因素组成的计算因子进行更改后的即时结果,以及使用不同预定义场景管理大表格。

  • 数据库函数:使用电子表格组织、存储和过滤数据。LibreOffice Calc 支持从数据库导出数据,支持拖放,或使用电子表格作为数据源,或作为插入的对象在 LibreOffice Writer 中使用。

  • 组织数据:重新排序电子表格,以显示或隐藏特定的数据范围,根据特殊条件格式化范围,或快速计算小计和总计。

  • 动态图表:LibreOffice Calc 通过动态图表展示电子表格数据,图表会在数据变化时更新。

  • 打开和保存 Microsoft Excel 文件:将 Microsoft Excel 文件转换并保存为多种其他格式。LibreOffice Calc 的默认格式是ODF 电子表格.ods)。

LibreOffice Calc 界面与 LibreOffice Writer 界面类似。其主要区别在于,LibreOffice Calc 提供了一个额外的数据菜单。标准格式化工具栏包括一些单元格格式化选项。Calc 还包括一个公式栏,可以从中添加计算公式

图 8.23 – LibreOffice Calc 界面

图 8.23 – LibreOffice Calc 界面

让我们来看看Calc界面中每个差异的详细信息:

  • 数据菜单,包含编辑当前工作表数据的命令——即定义范围、排序和过滤数据、计算结果、勾画数据和创建数据透视表:

    • 插入图表:在当前电子表格中创建一个图表。

    • 插入或编辑数据透视表:使你能够组合、比较和分析大量数据。数据可以根据不同的视角进行组织、重新排序或汇总。

    • 定义打印区域:定义电子表格中需要打印的单元格范围。

    • 冻结行和列:将活动单元格或列的左上角区域分割,该区域变得不可滚动。

  • 格式化工具栏包括 Calc 的一些功能,如下所示:

    • 合并并居中或取消合并单元格,取决于当前的切换状态:使用这些选项选择相邻的单元格,然后将其合并为一个居中的单元格。相反,大单元格可以拆分为独立的单元格。

    • 格式化为货币:单元格将获得在 工具 | 选项 | 语言设置 | 语言 中设置的默认货币格式。

    • 格式化为百分比:将百分比格式应用于选定单元格。

    • 格式化为数字:将默认数字格式应用于选定单元格。

    • 格式化为日期:根据 LibreOffice 区域设置,将默认日期格式应用于选定单元格。

    • 添加或移除小数位数:向选定单元格中的数字添加或移除小数位数。

  • 使用 公式栏输入公式。按钮用于访问命令:

    • 名称框显示活动单元格的引用、所选单元格的范围或活动区域的名称。

    • 函数向导打开向导以帮助创建函数。

    • 选择函数从单元格范围中插入一个函数到活动单元格。该函数包括求和平均值最小值最大值计数

    • 函数将公式添加到活动单元格中。

注意

有关 LibreOffice Calc 中可用的命令和选项的更多信息,请参阅 LibreOffice 帮助,网址为 help.libreoffice.org/latest/en-US/

所有这些电子表格编辑工具有助于我们记录 Linux 系统管理过程,创建和/或编辑管理元素(如服务器、路由器、用户和密码)的清单或数据库。

现在,让我们来看看 LibreOffice 套件中管理幻灯片和图像的组件。

创建幻灯片和图像管理

在 Writer 协助下记录过程,以及在 Calc 支持下进行可管理元素的清单编制后,是时候展示我们项目的状态、进展或绩效总结了。帮助我们创建专业演示文稿的 LibreOffice 组件是 LibreOffice Impress

LibreOffice Impress 使我们能够创建专业的幻灯片演示文稿,其中可以包括图形、绘图对象、文本、多媒体和许多其他元素,并且可以导入和更改 Microsoft PowerPoint 演示文稿。

对于屏幕上的幻灯片演示文稿,LibreOffice Impress 包括动画、幻灯片过渡和多媒体播放等功能。

与 Writer 和 Calc 类似,Impress 界面与其微软产品同行 Microsoft PowerPoint 相似。

为数不多的差异之一是在打开 LibreOffice Impress 时发生的。Impress 显示 选择模板 窗口:

图 8.24 – 打开 LibreOffice Impress

图 8.24 – 打开 LibreOffice Impress

LibreOffice 包含一套内置模板,用于创建文档、演示文稿、电子表格或绘图。通过模板管理器中的可用模板,你可以创建自己的模板或在线搜索额外的模板。

模板通过打开内容和格式已经完成的新文档来节省编辑时间。模板管理器 提供对模板的访问和组织功能。

可用模板的预览会出现在模板管理器的主窗口中,根据搜索和过滤选项进行显示。双击任何模板图标即可打开一个包含模板内容和格式的新文档。

图 8.25 – 模板管理器

图 8.25 – 模板管理器

在左下角选择 缩略图视图列表视图 来更改模板的显示方式。

你可以通过点击 启动时显示此对话框 复选框来禁用模板管理器窗口的显示。

WriterCalc 界面之间的主要区别在于 LibreOffice Impress 提供了一些额外的菜单 – 幻灯片幻灯片放映。在 Impress 中,格式化 工具栏不会在启动时加载,而是显示 绘图 工具栏。它还包括 标准 工具栏、访问展示幻灯片的命令和 演示文稿 工具栏:

图 8.26 – LibreOffice Impress 界面

图 8.26 – LibreOffice Impress 界面

属性 边栏也有所不同,因为它专注于幻灯片。

让我们来看看 Impress 界面中每个差异的细节:

  • 幻灯片 菜单提供导航和幻灯片管理命令 – 你可以创建编辑复制删除 幻灯片,还可以插入来自 另一个演示文稿 的幻灯片。

  • 幻灯片放映 菜单包含播放演示文稿的命令和选项。包括开始幻灯片放映、定义设置、添加计时器,以及定义对象交互动画过渡效果

  • 标准 工具栏包括 Impress 的功能,例如以下内容:

    • 显示视图,在 编辑模式母版模式 中。

    • 母版幻灯片 切换到 母版幻灯片视图,我们可以在此添加所需的元素,这些元素将出现在使用相同母版幻灯片的所有幻灯片中。

    • 从第一张幻灯片开始从当前幻灯片开始 设定展示幻灯片放映的起始点。

    • 使用 幻灯片 工具栏按钮来管理幻灯片 – 新建幻灯片复制幻灯片删除幻灯片幻灯片布局

    • 绘图 工具栏包含常用的编辑工具。绘图 工具栏在文本文档或电子表格中也可用。可见图标的集合可能会根据活动文档的类型而有所不同。编辑工具包括以下内容:

      • 选择 工具(一个白色箭头)允许你在活动幻灯片上选择一个对象。

      • 缩放 & 平移

      • 线条颜色

      • 填充颜色

      • 插入线条

      • 插入不同形状:基本形状、矩形、椭圆、线条和箭头、符号、块箭头、连接器、曲线和多边形、流程图、标注、星形和横幅、以及 3D 对象

      • 对象管理:旋转、对齐、排列和分布

      • 图像管理:添加阴影、裁剪、过滤、点和粘接点功能(连接线可设置的点)

      • 切换挤出

注意

要了解 LibreOffice Impress 中可用的命令和选项,请参考LibreOffice 帮助文档help.libreoffice.org/latest/en-US/

LibreOffice 组件帮助我们改进管理系统的文档。在接下来的章节中,我们将使用它们来建立标准化的系统基准。

总结

在本章中,我们提供了 Linux 办公套件的概述。它们中的一些已集成到 Fedora Linux 的官方仓库中,我们还添加了一些字体,以帮助我们改善使用办公套件制作的文档的外观和感觉。

我们浏览了 LibreOffice 的主要组件,它们是 Fedora Linux 工作站版本中的默认组件。Writer文本编辑器,帮助我们创建流程文档,我们学习了如何为文档应用样式,从而改善文档的外观和组织。

使用Calc电子表格编辑器,我们可以创建我们的管理设备的清单和数据库。我们回顾了编辑电子表格的可用工具与文本编辑器之间的主要区别。

最后,我们学习了如何使用Impress制作我们的文档幻灯片。我们还讨论了它的编辑工具与 LibreOffice 其他组件之间的区别。

在接下来的章节中,我们将回顾邮件客户端和浏览器,结束 Fedora Linux 提供的生产力工具之旅。这些工具将帮助我们使用 Linux 系统组织管理过程的文档。

进一步阅读

要了解更多本章中讨论的主题,您可以访问以下链接:

第十一章:邮件客户端和浏览器

互联网最初是作为 ARPAnet 出现的,这是一种由美国政府赞助的分组交换网络,始于 1969 年 9 月。林纳斯·托瓦兹(Linus Torvalds)直到同年 12 月才出生。1991 年,当 Linux 出现时,只有学者、研究人员和军方在使用互联网。对于大多数人来说,上网意味着使用 PineElm 来收发电子邮件,使用命令行程序如 ftpArchie 来搜索和共享文件。可用的最先进工具是 Gopher,类似于 Yahoo 的互联网资源指南。随后,蒂姆·伯纳斯-李(Tim Berners-Lee)发明了万维网,一切都改变了。

如今,互联网接入有了显著改善,速度也更快。这促使了许多不同工具的出现,用于阅读电子邮件和浏览网页。

在本章中,我们将讨论 Fedora Linux 中最常见的工具,涵盖以下主要内容:

  • 使用 Evolution 收发邮件

  • 使用 Thunderbird 收发邮件

  • 忠实的老 Firefox

  • 使用 Google Chrome 扩展浏览

技术要求

为了开展本章的主题,需要安装每个章节中所指示的软件包。Firefox 浏览器是 Fedora Linux 工作站版默认安装的唯一软件包。

在每个章节中,你会找到关于每个软件包不同安装类型的说明,按需要进行选择。

使用 Evolution 收发邮件

2000 年,Linux 发行版没有一款能够为企业用户提供所需功能和互操作性的电子邮件客户端。于是,Ximian 决定开发 Evolution。Evolution 1.0 版本于 2001 年 12 月发布。它为 Ximian 提供了一个 专有插件,使用户能够与 Microsoft Exchange 服务器连接。Evolution 从一开始就是开源软件,但 Ximian 的连接器作为专有软件出售。因此,Ximian 可以通过这一方式创收。当 Novell 在 2003 年 8 月收购 Ximian 后,Novell 决定从 2004 年 5 月起将 Exchange 插件作为开源软件整合进 Evolution 2.0

Novell 在 2011 年被 Attachmate Group 收购。结果,Novell 的开发人员转移到了 SUSE。2012 年,SUSE 决定停止为 Evolution 的开发提供资金。

红帽公司继续开发 Evolution,这促使了 Evolution 的积极开发,并对 Microsoft Exchange 提供良好支持的电子邮件客户端的需求。

Evolution 作为大多数使用 GNOME 的 Linux 发行版中的默认个人信息管理器,尤其是在 Debian 和 Fedora Linux 中表现突出。

2018 年 12 月,Fedora 项目决定从工作站版的默认安装中移除 Evolution,从 Fedora Linux 30 开始。然而,Evolution 仍然可以在官方仓库中找到,你可以安装并使用它作为个人信息管理器。

现在,让我们来看一下如何在工作站上安装和配置 Evolution。按照以下步骤操作:

  1. 要安装 Evolution,Fedora Linux 提供了几种选项。可以从 软件 应用程序中选择 Evolution 进行安装,选择你喜欢的源:

    • RPM

    • Fedora Flatpak

    • Flathub

图 9.1 – 从软件应用程序安装 Evolution

图 9.1 – 从软件应用程序安装 Evolution

  1. 使用终端,使用 dnf 命令,你可以查看可用的包和插件。切换到 root 用户并运行以下命令:

    # dnf list *evolution*
    

图 9.2 – Evolution 和插件

图 9.2 – Evolution 和插件

使用你喜欢的方式安装 Evolution,然后我们将开始邮件客户端的配置。

  1. 要打开 Evolution,从菜单中启动它。作为 非 root 用户,在左上角打开菜单并在搜索框中输入 Evolution

图 9.3 – 从菜单启动 Evolution

图 9.3 – 从菜单启动 Evolution

在搜索框中输入 evolution

图 9.4 – 从活动概览菜单启动 Evolution

图 9.4 – 从活动概览菜单启动 Evolution

  1. Evolution 第一次打开时,如果你愿意,可以将其设置为默认邮件客户端:

图 9.5 – 将 Evolution 配置为默认邮件客户端

图 9.5 – 将 Evolution 配置为默认邮件客户端

  1. 欢迎 屏幕中,它会提示你设置一个邮件账户。点击 下一步 按钮开始配置:

图 9.6 – Evolution 配置向导

图 9.6 – Evolution 配置向导

  1. 提供的第一个选项是恢复 Evolution 配置的备份,以设置邮件账户。

图 9.7 – 从备份恢复窗口

图 9.7 – 从备份恢复窗口

如果你有备份,请勾选 从备份文件恢复 选项框,然后点击 浏览 图标选择文件并恢复备份。

如果没有 Evolution 备份,请点击 下一步 按钮继续配置。

  1. 在接下来的窗口中,填写所需的资料 – 全名电子邮件地址

图 9.8 – 身份窗口

图 9.8 – 身份窗口

可以添加更多信息,例如 组织名称由账户管理的别名

完成后,点击 下一步 按钮继续配置。

  1. 如果 Evolution 激活,它将搜索已输入账户的邮件服务信息。如果未找到,它会提供接收邮件的配置选项:

图 9.9 – 收取邮件选项

图 9.9 – 收取邮件选项

选项如下:

  • Exchange Web Services:连接到 Microsoft Exchange 服务器,进行电子邮件、日历和联系人信息的同步。

  • IMAP:在服务器上保持电子邮件,以便可以通过 IMAP 协议从多个系统访问电子邮件。IMAP 代表 互联网消息访问协议

  • POP:作为一种选择,将电子邮件下载到硬盘进行永久存储,从而释放邮件服务器上的空间。POP 代表 邮局协议

  • USENET 新闻:连接到新闻服务器并下载可用的新闻摘要列表。

  • 本地投递:将邮件从卷中移动并存储在主目录中。

  • MH 格式邮件目录:使用 mh邮件处理系统)或 mh 风格 程序下载电子邮件。mh 由几个不同的程序组成,旨在从命令行运行。与将许多邮件存储在一个文件中不同,每封邮件都有自己单独的文件,保存在一个特殊的目录中。

  • Maildir 格式邮件目录:使用 Maildir 风格 程序下载电子邮件。Maildir 邮件格式是一种常见的邮件存储方式。每封邮件都存储在一个独立的文件中,并且每个邮件文件夹都是一个文件系统目录。

  • 标准 Unix mbox 卷目录:以 Unix 主机用于存储邮件消息的格式下载电子邮件。mbox 文件通常位于 系统邮件卷 中,以各种名称存储在用户的邮件目录下,并以 mbox 名称存储在用户的主目录中。

  • 标准 Unix mbox 卷文件:与前一个选项相同,但 邮件卷 被创建为一个单独的文件。

注意

确认通用邮件访问数据,如 服务器类型安全性,与相关部门的工作人员确认,以避免连接问题。

  1. 选择接收邮件选项后,输入访问数据,如下图所示:

图 9.10 – 接收电子邮件窗口

图 9.10 – 接收电子邮件窗口

点击 下一步 按钮继续配置。

  1. 在以下窗口中,配置邮件接收选项,例如检查新邮件的时间间隔,或者是否将邮件副本保留在服务器上,以及启用或禁用功能:

图 9.11 – 接收选项窗口

图 9.11 – 接收选项窗口

点击 下一步 按钮继续配置。

  1. 在以下窗口中,配置 发送 电子邮件 选项:

图 9.12 – 发送电子邮件窗口

图 9.12 – 发送电子邮件窗口

默认情况下,向导会将 发送电子邮件 字段填充为为邮件接收提供的登录信息。如果这些信息正确并足以发送电子邮件,请点击 完成 按钮结束配置。

如果你想查看所有提供的信息,请点击 下一步 按钮。

  1. 以下窗口显示了为账户配置的数据摘要:

图 9.13 – 账户摘要窗口

图 9.13 – 账户摘要窗口

如果需要更改任何信息,点击返回按钮返回到需要更改数据的窗口。

如果信息正确,点击下一步按钮。

  1. 以下窗口确认邮件账户配置已完成:

图 9.14 – 完成的邮件配置窗口

图 9.14 – 完成的邮件配置窗口

  1. 点击应用按钮以保存设置。

账户设置完成后,可以检查和发送电子邮件。

在 Evolution 的主窗口中,左上角有按钮可以发送/接收邮件以及创建新邮件。

要发送或接收邮件,点击发送/接收按钮:

图 9.15 – Evolution 主窗口

图 9.15 – Evolution 主窗口

要创建电子邮件,点击新建按钮:

图 9.16 – 撰写邮件窗口

图 9.16 – 撰写邮件窗口

新邮件撰写窗口中,填写收件人和邮件主题。此外,还可以选择邮件格式(纯文本富文本HTML),并使用右下角的按钮添加附件。

当电子邮件准备好发送时,点击发送按钮。

Evolution 目前是多个 Linux 发行版中的默认客户端。虽然有一些替代的邮件客户端,但它们没有 Evolution 那么多的选项。

作为 Evolution 的替代品,具有更多选项的客户端之一是 Mozilla Thunderbird。

现在让我们一起走进这个邮件客户端,看看它与 Evolution 的主要区别。

使用 Thunderbird 发送邮件

Mozilla Thunderbird是一个跨平台的、免费的开源电子邮件、新闻、RSS 和聊天客户端,由 Mozilla 基金会开发。它使用 XUL 界面语言。XML 用户界面语言(XUL)是 Mozilla 开发的一种基于 XML 的用户界面语言。XUL 作为一种 XML 方言,提供图形用户界面,如网页。XUL 不是公共标准。

2004 年 12 月 7 日,1.0 版本发布,并在发布后的前三天内获得了超过 50 万次下载,在 10 天内下载量达到 100 万次。

2012 年 7 月 6 日,Mozilla 宣布,由于扩大 Thunderbird 功能集的努力未能成功,公司将不再将 Thunderbird 开发作为优先事项。2015 年 12 月 1 日,Mozilla 的首席执行官宣布,Thunderbird 的开发应与 Firefox 分开。

最近,Thunderbird 在其官方博客上宣布,由同为 Mozilla 子公司的MZLA Technologies Corporation将继续该项目,提供新的更新、新的设计以及移动应用程序。

Thunderbird 在多个 Linux 发行版的桌面系统上默认安装。在 Fedora Linux 中,Thunderbird 不是默认安装的,但可以在官方仓库中找到。

让我们看看如何在我们的 Fedora Linux 工作站上安装和配置 Thunderbird。请按照以下步骤操作:

  1. 要安装 Mozilla Thunderbird,Fedora Linux 提供了几种选择,但它仅作为 Flatpak 包提供。从软件应用程序中,要安装 Mozilla Thunderbird,选择你偏好的选项:

    • Fedora Linux(Flatpak)

    • Flathub

图 9.17 – 从软件应用程序安装 Thunderbird

图 9.17 – 从软件应用程序安装 Thunderbird

  1. 使用终端,使用 dnf 命令,你可以查看可用的软件包和插件。切换到 root 用户并运行以下命令:

    # dnf list *thunderbird*
    

图 9.18 – Thunderbird 和插件

图 9.18 – Thunderbird 和插件

按照你偏好的方法安装 Mozilla Thunderbird,然后我们将开始邮件客户端的配置。

  1. 要打开 Mozilla Thunderbird,从菜单启动它。作为 非 root 用户,在左上角打开菜单并在搜索框中输入 Thunderbird

图 9.19 – 从菜单启动 Thunderbird

图 9.19 – 从菜单启动 Thunderbird

Mozilla Thunderbird 还可以通过搜索框中的 thunderbird 访问:

图 9.20 – 从活动概览菜单启动 Thunderbird

图 9.20 – 从活动概览菜单启动 Thunderbird

  1. 当 Mozilla Thunderbird 首次打开时,它会要求你设置一个电子邮件账户:

图 9.21 – 账户设置窗口

图 9.21 – 账户设置窗口

填写所需数据,然后点击 继续 按钮。

注意

在我的例子中,我输入了一个 Google 电子邮件账户。如果你的工作站连接到互联网,Mozilla Thunderbird 将尝试从邮件服务的服务器获取信息。它只会在服务器可访问时获取到这些信息。

以下窗口显示了已下载的可用配置数据。选择你偏好的配置,并点击 完成 按钮:

图 9.22 – 下载的可用配置

图 9.22 – 下载的可用配置

Mozilla Thunderbird 将提供连接账户中找到的服务,如通讯录或日历:

图 9.23 – 自动设置完成

图 9.23 – 自动设置完成

要完成配置,点击 完成 按钮。

注意

如果 Mozilla Thunderbird 找不到服务配置信息,你需要手动配置邮件账户。

这结束了 自动配置 邮件账户。若要手动指定邮件账户设置,请按照以下步骤操作:

  1. 账户设置 窗口中,点击 手动配置 链接:

图 9.24 – 账户设置窗口

图 9.24 – 账户设置窗口

注意

与相应区域的工作人员确认一般的邮件访问数据,如服务器类型、服务端口和安全设置,以避免连接问题。

Mozilla Thunderbird 支持通过 IMAP 或 POP3 协议或本地邮件连接到邮件服务器,如前一节所述。

  1. 选择要使用的协议以及接收发送邮件服务器的访问数据:

图 9.25 – 手动账户设置窗口

图 9.25 – 手动账户设置窗口

  1. 如果需要更多配置选项,请点击高级 配置链接。

图 9.26 – 高级账户设置窗口

图 9.26 – 高级账户设置窗口

这个窗口包含账户连接详细信息的细节设置。

配置完成后,点击账户设置标签中的x符号以关闭该标签。

  1. 完成账户设置后,Mozilla Thunderbird 的主窗口会出现:

图 9.27 – Mozilla Thunderbird 主窗口

图 9.27 – Mozilla Thunderbird 主窗口

  1. 从主窗口,可以配置更多的邮件、日历、通讯录、聊天、文件链接、订阅源或新闻组账户。它还可以从其他电子邮件客户端导入这些账户,并提供对支持资源的访问。要检索邮件消息,请点击获取 邮件按钮:

图 9.28 – 检索邮件消息

图 9.28 – 检索邮件消息

现在,让我们来看看如何发送电子邮件:

  1. 要创建新的电子邮件消息,请点击 Mozilla Thunderbird 工具栏上的写邮件按钮:

图 9.29 – Mozilla Thunderbird 主窗口

图 9.29 – Mozilla Thunderbird 主窗口

  1. 写邮件窗口中,填写收件人和主题。如果需要发送附件,请点击工具栏上的附加按钮:

图 9.30 – 写邮件窗口

图 9.30 – 写邮件窗口

  1. 当电子邮件准备好发送时,发送按钮会被启用。点击发送按钮以发送电子邮件。

根据 opensource.com,这两个邮件客户端,EvolutionMozilla Thunderbird,代表了企业环境中最常用的桌面邮件客户端。

然而,在 Linux 中,最常见的获取电子邮件的方法是通过网页浏览器。

现在,让我们来看一下在 Fedora Linux 中最常用的两个网页浏览器。

可靠的老 Firefox

1998 年 1 月,网景通信公司NSCP)宣布计划在互联网上发布其Netscape Communicator 软件的源代码。公司在 1998 年第一季度发布了Netscape Communicator 5.0的第一个开发者版本的源代码。这一激进的举措使网景能够通过吸纳成千上万互联网程序员的创意力量,不断提升 Netscape 软件的未来版本。

这一公告促成了Mozilla 项目的创建。

Phoenix 0.12002 年 9 月发布。那是后来被称为Firebird,然后改名为Firefox的浏览器的第一个版本。Firefox 1.0于 2004 年发布,并取得了巨大成功。

2003 年 11 月,Red Hat 宣布发布Fedora Core 1,即 Fedora 项目的第一个软件版本。GNOME 是 Fedora Linux 的默认桌面。当时 Mozilla Suite 是首选的网页浏览器。Mozilla 尚未启动 Firefox 浏览器项目,因此该套件包括一个电子邮件客户端和一个Usenet 新闻阅读器

Fedora Core 3于 2004 年发布,是第一款包含 Mozilla Firefox 网页浏览器的版本。

2019 年,Fedora Linux 31 工作站配备了 Firefox 后端,默认从X11迁移到Wayland,并引入了新的显示服务器。这是朝着全面过渡到Wayland的又一步。这只影响了 GNOME。

Firefox 作为默认 RPM 包与安装为 Flatpak 应用的 Firefox 一同预装在 Fedora Linux 工作站中,还有一个包可以让你在 Wayland 中运行 Firefox。

让我们来看一下这些安装选项之间的区别:

  1. 从软件应用中,搜索Firefox,然后点击已安装选项:

图 9.31 – 软件应用中的 Firefox

图 9.31 – 软件应用中的 Firefox

  1. 点击安装源后,找到 RPM 和 Fedora Flatpak 应用:

图 9.32 – Firefox 安装源

图 9.32 – Firefox 安装源

  1. 在终端中,也可以使用dnf命令列出可用的 Firefox 包:

    # dnf list *firefox*
    

图 9.33 – 终端中的 Firefox 包

图 9.33 – 终端中的 Firefox 包

  1. 默认情况下,要打开Firefox浏览器,启动器会出现在活动****概览窗口的收藏夹中:

图 9.34 – 活动概览中的 Firefox 启动器

图 9.34 – 活动概览中的 Firefox 启动器

从菜单中,你还可以在收藏夹中找到Firefox启动器:

图 9.35 – 菜单中的 Firefox 启动器

图 9.35 – 菜单中的 Firefox 启动器

第三章中,我提到过安装Gnome Frippery 面板收藏扩展extensions.gnome.org/extension/4/panel-favorites/)会将收藏夹添加到任务栏中,因此Firefox启动器会出现在桌面上:

图 9.36 – 桌面上的 Firefox 启动器

图 9.36 – 桌面上的 Firefox 启动器

  1. 打开 Firefox 时,显示Fedora 项目页面作为主页

图 9.37 – Firefox 浏览器

图 9.37 – Firefox 浏览器

  1. 你可以选择更改主页,这可以通过 Firefox 设置来完成。为此,点击如下图所示的三条横线图标:

图 9.38 – Firefox 设置

图 9.38 – Firefox 设置

  1. 在下拉菜单中,选择设置以打开 Firefox 首选项窗口。此窗口也可以通过地址栏导航到about:preferences来打开:

图 9.39 – Firefox 首选项

图 9.39 – Firefox 首选项

  1. Firefox 设置分为多个部分,每个部分下还有若干小节:

    • 常规:常规配置选项,例如:

      • 语言和外观:更改网站的外观,包括自动、亮色或暗色主题、颜色、字体、缩放以及语言。它还会更改 Firefox 菜单、消息和通知的显示。

      • 文件和应用程序:更改文件的处理方式,例如下载,包括保存位置和使用的应用程序。

      • Firefox 更新:验证浏览器版本。

      • 性能:更改或验证推荐的性能设置。

      • 浏览:更改或添加浏览行为的选项,例如滚动显示或媒体文件处理。它还控制扩展和功能。

      • 网络设置:配置 Firefox 如何连接互联网。

    • 主页:控制打开 Firefox 时的启动选项:

      • 新窗口和标签页:主页、新窗口和标签页的配置。

      • Firefox 主页内容:主页内容的配置。

    • 搜索:搜索选项的配置,包括以下内容:

      • 搜索框:用于执行搜索的工具。

      • 默认搜索引擎:首选的搜索引擎(默认是 Google)。

      • 搜索建议:搜索中的建议行为。

      • 搜索快捷方式:替代搜索引擎。

    • 隐私与安全:隐私和安全设置,包括以下内容:

      • 增强的跟踪保护:一个用于阻止恶意跟踪器和脚本的工具。它允许不同级别的安全性。

      • Cookies 和网站数据:Cookies 和保存的网站数据的管理。

      • 登录和密码:网站访问数据和密码的管理。

      • 历史记录:浏览历史记录的管理。

      • 地址栏:地址栏视图的自定义。

      • 权限:自定义对资源的访问权限,如位置、设备或通知。

      • Firefox 数据收集和使用:管理 Firefox 收集的数据。

      • 安全性:浏览安全性的一般自定义。

    • 同步:同步选项的配置—书签、历史记录、标签、密码和附加组件。如果需要,请创建同步帐户。

    • 来自 Mozilla 的更多内容:让您尝试其他 Mozilla 产品:

      • Firefox 移动版:用于手机的浏览器

      • Firefox Relay:电子邮件屏蔽保护在线身份

    • 扩展和主题:Firefox 自定义选项。也可以通过在地址栏中输入about:addons来打开此窗口:

      • 扩展:扩展包括扩展 Firefox 功能的程序,如弹出窗口阻止器在线翻译cookie 处理

      • 主题:更改浏览器的颜色方案或添加自定义背景

      • 插件:插件为浏览器添加功能,例如播放某些视频或图像格式。

    • Firefox 支持:打开Mozilla 支持窗口(support.mozilla.org)。也可以在Firefox 选项、偏好设置和 设置部分访问此页面。

  2. 图 9.38所示的下拉菜单中,可以找到一些快捷方式,跳转到 Firefox 设置或偏好设置的各个部分。例如,要查看 Firefox 的版本,点击下拉菜单,选择帮助选项,然后点击关于

图 9.40 – 关于 Mozilla Firefox

图 9.40 – 关于 Mozilla Firefox

Firefox 在外观和手感方面最好的选项之一就是自定义浏览器主题。

让我们看看如何将主题添加到浏览器中。

自定义 Firefox

主题更改 Firefox 的外观,例如工具栏的颜色方案和背景图像。并非所有的 Firefox 主题都是浅色(背景),在某些情况下,它们不兼容。安装主题之前,请验证其兼容性和大小。

现在,让我们看看如何在工作站上安装 Firefox 主题。按照以下步骤操作:

  1. 打开 Firefox 的设置窗口,然后点击扩展和 主题部分:

图 9.41 – Firefox 扩展和主题

图 9.41 – Firefox 扩展和主题

  1. Firefox 的附加组件管理器窗口显示一个子菜单,提供推荐扩展主题插件的自定义选项。

图 9.42 – Firefox 附加组件

图 9.42 – Firefox 附加组件

点击主题

  1. 主题窗口中,浏览 Firefox 提供的主题推荐:

图 9.43 – Firefox 主题

图 9.43 – Firefox 主题

也可以搜索主题。在名为dark space的搜索栏中搜索。

  1. Firefox 会显示一个窗口,展示主题搜索的结果:

图 9.44 – 搜索主题

图 9.44 – 搜索主题

点击第一个主题。

  1. 要添加主题,请安装并启用它,然后点击安装 主题按钮:

图 9.45 – 安装主题

图 9.45 – 安装主题

  1. 一个通知窗口将出现,确认附加组件已安装:

图 9.46 – 安装附加组件

图 9.46 – 安装附加组件

点击添加按钮。

  1. Firefox 的外观将在标签栏工具栏上发生变化:

图 9.47 – 添加主题

图 9.47 – 添加主题

一个通知窗口将提醒您关于主题应用的情况。点击确定按钮以接受更改。

一旦完成了 Firefox 的个性化设置,它就可以通过网页访问我们的电子邮件了。

按照上一节中的示例,打开 Google 邮箱网址(mail.google.com):

图 9.48 – 通过网页访问邮件

图 9.48 – 通过网页访问邮件

输入您的电子邮件地址和密码。点击下一步按钮。Firefox 将打开电子邮件文件夹,以便您查看和发送邮件:

图 9.49 – 通过网页访问电子邮件

图 9.49 – 通过网页访问电子邮件

Firefox 是在 Linux 上最好的、最稳定的浏览器选项之一。

另一个最佳且最受欢迎的选项是使用谷歌的浏览器——谷歌 Chrome。让我们看看如何通过使用谷歌的 Chrome 浏览器,将浏览体验提升到一个新层次。

扩展浏览体验:使用谷歌 Chrome

谷歌 Chrome 是由谷歌开发的专有网页浏览器,尽管它源自开源项目。Chrome 建立在 Chromium 开源浏览器项目的基础上,使用了来自谷歌及其他第三方(如 Netscape)的不同代码库。Chrome 的版本发布来源于 Chromium。

尽管如此,谷歌 Chrome 仍然可以免费使用。

Google Chrome 于 2008 年发布,伴随发布的还有 Chromium 源代码的公开。这使得这套代码为开发其他浏览器提供了基础。该代码的初版包含了 Windows、macOS 和 Linux 的构建版本,且在开发的初期阶段功能不完全。Chromium 1.0 于 2008 年 12 月发布,使得 Windows 版本的 Chrome 结束了测试阶段。

2009 年 5 月,Chromium 的 Linux 版首个alpha 版本发布。到了 7 月,Chromium 为 Linux 引入了本地主题,使用 GTK+工具包,这使得它能够与 GNOME 桌面环境集成。

Chromium 5.0 于 2010 年 1 月发布。谷歌 Chrome 5.0 于 2010 年 5 月发布,并为所有平台提供了一个稳定的(非测试版)版本。

Chrome 包含额外的功能,例如支持专有媒体文件(如H.264AAC)和播放受版权保护的媒体(Netflix 等)。Chrome 还包括对其他 Google 服务的支持,如浏览器同步和不受 Chromium 支持的定位服务。

Google Chrome 可通过第三方仓库在 Fedora Linux Workstation 上获取。启用该仓库后,可以通过软件应用程序或命令行安装 Chrome。

让我们验证一下在工作站上安装 Chrome 的选项:

  1. 打开软件应用程序,在搜索选项中输入Google Chrome

图 9.50 – 在软件应用程序中搜索 Chrome

图 9.50 – 在软件应用程序中搜索 Chrome

  1. 点击第一个结果并验证安装源:

图 9.51 – Google Chrome 源安装选项

图 9.51 – Google Chrome 源安装选项

  1. 要启用 Google 的第三方仓库,返回软件应用程序的主窗口。点击关闭(x)按钮旁边的三个水平线折叠菜单图标:

图 9.52 – 软件应用程序中的软件仓库

图 9.52 – 软件应用程序中的软件仓库

  1. 点击软件仓库

  2. 向下滚动,直到找到google-chrome仓库,并切换按钮以启用它:

图 9.53 – 软件仓库窗口

图 9.53 – 软件仓库窗口

  1. 再次运行搜索,并验证安装源是否为 RPM:

图 9.54 – Google Chrome 源安装选项

图 9.54 – Google Chrome 源安装选项

  1. 打开终端,以root用户身份,使用dnf命令列出google-chrome浏览器软件包:

    # dnf list *google-chrome*
    

图 9.55 – Google Chrome 软件包

图 9.55 – Google Chrome 软件包

按照你偏好的方法安装 Google Chrome,然后我们将开始浏览器配置。

  1. 要打开 Google Chrome,从菜单启动它。作为非 root 用户,打开左上角的菜单,并在搜索框中输入Google Chrome

图 9.56 – 从菜单启动 Google Chrome

图 9.56 – 从菜单启动 Google Chrome

在搜索框中输入Google Chrome

图 9.57 – 从活动概览菜单启动 Google Chrome

图 9.57 – 从活动概览菜单启动 Google Chrome

  1. 如有需要,将Chrome添加到收藏夹标签。将Chrome图标从活动概览菜单拖放到收藏夹仪表盘。

    一个通知窗口会确认 Google Chrome 现在已被固定到收藏夹仪表盘:

图 9.58 – Google Chrome 固定到收藏夹

图 9.58 – Google Chrome 固定到收藏夹

图标也可以放置在您首选位置的收藏夹面板中。从现在开始,它将作为收藏夹项目出现:

图 9.59 – 收藏夹面板

图 9.59 – 收藏夹面板

因此,Google Chrome 图标也出现在任务栏中,这是由先前安装的扩展程序启用的:

图 9.60 – 任务栏中的 Google Chrome

图 9.60 – 任务栏中的 Google Chrome

  1. 当 Chrome 第一次打开时,您可以将其设置为默认浏览器:

图 9.61 – 将 Google Chrome 设置为默认浏览器

图 9.61 – 将 Google Chrome 设置为默认浏览器

  1. 如果您同意设置,请勾选复选框并点击确定按钮。否则,关闭窗口。

    要配置 Google Chrome,我们可以利用当前使用的邮箱账户是 Google Mail 的事实。

  2. 当 Chrome 窗口打开时,点击已经是 Chrome 用户?登录链接,使用电子邮件账户并同步 Chrome 设置。

图 9.62 – Google Chrome 欢迎界面

图 9.62 – Google Chrome 欢迎界面

  1. 在以下对话框中输入电子邮件地址,并点击下一步按钮:

图 9.63 – Google Chrome 访问界面

图 9.63 – Google Chrome 访问界面

  1. 在接下来的界面中,输入您的密码,并点击下一步按钮:

图 9.64 – Google Chrome 访问界面

图 9.64 – Google Chrome 访问界面

  1. 如果您在另一台设备上自定义了 Chrome 并启用了同步,Chrome 将自动进行配置。

图 9.65 – Google Chrome 同步界面

图 9.65 – Google Chrome 同步界面

  1. 点击是的,我同意按钮开始同步自定义设置。

    自动应用您的主题安装扩展和插件,以及配置和下载您的历史记录书签将开始运行:

图 9.66 – 正在运行 Google Chrome 同步

图 9.66 – 正在运行 Google Chrome 同步

  1. 同步完成后,Google Chrome 将显示已完成的自定义设置:

图 9.67 – Google Chrome 自定义完成

图 9.67 – Google Chrome 自定义完成

像 Firefox 一样,Google Chrome 提供了设备间的配置和同步功能。Chrome 利用 Google 服务进行配置同步。Chrome 的配置和自定义与 Firefox 非常相似。

要访问 Chrome 设置,请点击带有三点图标(即垂直省略号)的按钮,位于x按钮下方:

图 9.68 – Chrome 设置

图 9.68 – Chrome 设置

Chrome 设置分为几个部分,每个部分下又有多个小节,包括以下内容:

  • 你和 Google:配置访问帐户并与Google服务同步。在此,你可以自定义个人资料,并导入书签和设置。

  • 自动填充和密码:管理密码管理器、支付方式和存储的地址及数据。

  • 隐私与安全:包括一个安全检查选项,用于检测安全漏洞、恶意扩展等。同时也管理隐私和安全选项。

  • 性能:为未使用的打开标签页添加内存节省选项,这是一种省电模式

  • 外观:配置浏览器的外观设置,例如主题、主页,以及如何显示书签栏或侧边面板。

  • 搜索引擎:配置浏览器的搜索引擎(可能与Google不同)及其行为。

  • 默认浏览器:检测系统默认浏览器。

  • 启动时:配置浏览器启动时的行为。

  • 语言:配置显示网站内容的首选语言,并在你在网站上输入时提供拼写检查功能。

  • 下载:管理文件下载。

  • 辅助功能:配置浏览器的辅助功能选项。

  • 系统:配置浏览器在操作系统中的行为选项。

  • 重置设置:将浏览器设置恢复到出厂默认值。

  • 扩展程序:管理浏览器扩展和插件。

  • 关于 Chrome:显示浏览器版本。

以下图显示了 Chrome 设置的各个部分:

图 9.69 – Chrome 设置部分

图 9.69 – Chrome 设置部分

与 Firefox 类似,你可以在地址栏中访问chrome://settings/help

图 9.70 – Chrome 版本

图 9.70 – Chrome 版本

在这种特殊情况下,Chrome 使用了Google 邮件检查器扩展(chrome.google.com/webstore/detail/google-mail-checker/mihcahmgecmbnbcchbopgniflfhgnkff),该扩展为直接访问 Google Mail 添加了一个按钮:

图 9.71 – Google 邮件检查器扩展

图 9.71 – Google 邮件检查器扩展

要在 Chrome 中管理和添加扩展程序,请点击地址栏中的chrome://extensions/

图 9.72 – Chrome 扩展

图 9.72 – Chrome 扩展

通过搜索栏,你可以一键找到并安装扩展程序。

这些应用程序是 Fedora Linux Workstation 中最常用于电子邮件和互联网浏览的工具。

使用这些应用程序,我们涵盖了所有基本工具,为标准化管理系统文档奠定了基础。

在接下来的章节中,我们将讨论生成基线的流程和最佳实践,以及如何进行标准化的系统管理。

总结

在这一章中,我们学习了如何安装、配置和使用最常见的 Linux 选项,用于电子邮件和网页浏览。

Evolution,由 Red Hat 和 GNOME 支持,在企业环境中是最常见的邮件客户端之一(根据 opensource.com)。这是因为它允许您连接到 Microsoft Exchange 邮件服务。

Mozilla 项目为我们提供了两种选择,用于这两项服务。Thunderbird 电子邮件客户端是电子邮件管理的一个很好的选择,而Firefox,最常用的网页浏览器,已包含在所有 Linux 发行版中。

Google Chrome 作为网页浏览器是一个动态的选择。基于开源项目,它结合了专有插件的优点。在 Google 提供的服务中使用邮件帐户可以在设备之间提供移动性,并在访问时提供灵活性。

在接下来的章节中,我们将深入探讨 Linux 系统管理的概念和流程基础,充分利用与兼容操作系统的工作站。

进一步阅读

要了解本章涵盖的主题,请访问以下链接:

第四部分:系统管理工具

在本部分中,您将学习执行系统管理员任务的最佳实践,从基本原则开始。您还将获得有关如何调整系统、使用强化安全以及使用虚拟化和容器的技巧。

本部分包含以下章节:

  • 第十章系统管理

  • 第十一章性能调优最佳实践

  • 第十二章SELinux

  • 第十三章虚拟化与容器

第十二章:系统管理

与许多职业不同,成为一名系统管理员并没有单一的路径。许多系统管理员拥有不同领域的学位:计算机科学系统工程信息技术软件工程机械工程气象学等等。

此外,鉴于系统管理的实践性质,以及开源服务器软件的可用性,许多系统管理员是自学成才的。通常,这需要一些管理预期系统的先前经验。在某些情况下,系统管理员候选人必须持有证书才能被考虑聘用。

如果你没有采取最佳实践的习惯,这项职业会变得更加困难。

但是你从哪里学到这些呢?它们在哪里写下来?

在本章中,我们打算解答这些疑惑,并帮助你成为一名Linux 系统管理员

我们将讨论以下主要主题:

  • 系统管理员的三条法则

  • 一点 Git 和编程

  • 不要忘记备份

  • 使用 Ansible 进行自动化

  • 永无止境的学习

技术要求

为了完成本章中的主题,你需要安装每个部分中指示的软件包。在每个部分,你将找到有关你需要安装的不同类型软件包的说明。

本章的示例可以从本书的GitHub仓库下载:github.com/PacktPublishing/Fedora-Linux-System-Administration/tree/main/chapter10

系统管理员的三条法则

因为,如果你停下来思考,机器人学的三条法则是许多世界伦理系统的基本指导原则。 [...] 简而言之,如果拜尔利遵守了所有的机器人学法则,他可能是一个机器人,或者只是一个非常 好的人。”

来自 艾萨克·阿西莫夫的《证据》中,苏珊·卡尔文博士

20 多年来,我一直是不同公司系统管理员。在那些年里,一些年轻人曾问我,最好的实践是什么,是否可以将它们应用于自己,认为自己是一个合格的系统管理员。在这些年里,我读过不同的文章,讨论了这些最佳实践系统管理员伦理规范系统管理员的十诫,甚至计算机基本法则

成为平台系统管理员这项工作是艰巨的任务,如果我们没有养成应用最佳实践的习惯,它就会变得复杂。但是你从哪里学到这些呢?它们在哪里写下来?

根据我的经验,最好的哲学之一是遵循KISS 原则,并基于此将建议简化为有助于我们技能发展的基本原则。

让我们概述一下这个原则。

KISS 原则

KISS 原则(Keep It Simple, Stupid! 的缩写)是美国海军于 1960 年提出的一项设计原则。它指出,大多数系统如果保持简单而不是复杂,工作效果会更好。因此,简洁性应当成为设计的关键目标,应避免不必要的复杂性。

一段归于莱昂纳多·达·芬奇的文字为我们概述了这一原则:

简洁就是 终极的复杂性。”

一旦你体验过飞行,你将永远仰望天空行走在地球上,因为你曾经在那里,且总会渴望 回到那里。

学习永远不会耗尽 思维。”

莱昂纳多 达·芬奇

在大多数情况下,简单的解决方案更有效。多年来,我看到许多年轻的系统管理员在应用复杂的解决方案时,反而导致了更多的问题。

注意

想要了解更多关于如何在代码创建中应用 KISS 原则的信息,请参考The Kiss Principle,网址是people.apache.org/~fhanik/kiss.html

基于此,让我分享一下作为系统管理员,指导我工作的基本原则。

让我们看看这些基本原则是如何起源的,然后深入探讨它们。

了解基本任务

这是来自维基百科的内容(en.wikipedia.org/wiki/System_administrator):

系统管理员是负责实施、配置、维护、监控、文档记录并确保计算机系统或其某一方面正常运行的人。”

他们的目的是保证他们所管理的服务器的正常运行时间性能资源使用安全性

他们有不同的角色,比如服务器管理员数据库管理员网络管理员邮件服务器管理员Web 服务器管理员安全管理员备份管理员等。

根据角色的不同,任务会有所变化,但它们都集中于一些基本点。

让我们从最基本的一点开始,那就是始终使用命令行界面。正如在第一章中提到的,终端是操作系统管理的主要工具。它的使用扩展了系统管理和操作的能力。

根据我个人的经验,任何生产系统终究会失败,无论我们采取了多少预防措施或进行过多少维护。最佳解决方案是为那一天做好准备,确保有有效的备份。

如果我们不知道系统中哪些是关键内容,那么我们应该调查哪些是关键内容,并因此规划备份。我们如何知道备份在灾难发生时会起作用?以下是您在规划时应该提出的一些问题:

  • 使用什么软件(或脚本)进行备份?

  • 我们有多少空间用于备份(包括内部和外部,磁盘或磁带)?

  • 我们应该多久轮换一次备份?

  • 无论是完整备份(full backup),我们是否需要增量备份?

一旦我们清楚了如何进行备份,我们必须花时间来验证它。尽管我们有一个良好的备份计划,但这并不意味着备份不会遭到破坏。

备份以及我们备份的频率是所有类型的系统管理中最基本和最通用的任务

大多数备份解决方案允许我们通过命令行来处理这个任务。得益于这一功能,任务可以像许多其他任务一样自动化。自动化任务使系统管理员的工作更加高效。

语句懒惰的系统管理员是最好的系统管理员广为人知。这意味着如果你足够积极地去自动化所有的流程,你将拥有大量的自由时间,并且被认为是懒惰的。最好的系统管理员看起来似乎总是不太忙,喜欢放松并让系统为他们工作。更聪明地工作, 而不是更努力地工作

但是我们从自动化流程中获得的自由时间应该被有效利用——也许可以通过学习一些我们还未掌握的新语言或新兴技术(如 Perl、Python、Kubernetes 等),或者优化某些脚本,将它们转变为编排

下图展示了这些原则的演变:

图 10.1 – 基本原则

图 10.1 – 基本原则

遵循这些基本原则帮助我提升了许多系统管理技能。尽管它们并没有涵盖系统管理的所有方面,但它们创造了良好的习惯,能够培养许多这项工作所需的技能。

这些原则构成了我的有效管理的基本法则——即我的三条系统管理员法则:

  • 备份:系统管理员必须备份整个系统,并始终验证备份。

  • 自动化:系统管理员应尽可能自动化所有工作,除非这与第一条法则相冲突。

  • 学习:系统管理员必须有自由时间来学习,只要这段自由时间不与第一条或第二条法则冲突。

在我们深入探讨系统管理的每一条法则之前,先快速看一下实施这些法则所必需的几个工具。

首先,我们将探讨编程和版本管理。

一点点 Git 和编程

典型的系统管理任务涉及命令行工具。更复杂的任务通常需要将多个命令串联起来,并相互共享结果。Linux 命令可以组合使用,通过 Bash shell 环境和基本编程功能来解决重复性和复杂的任务。

作为命令解释器和一种独立的编程语言,Bash shell 环境不仅可以运行例程和使用表达式,还可以运行其他编程语言。

这些例程简化了命令列表,这些命令可以包含在一个名为Bash 脚本的文件中。

接下来,我们将学习如何创建 Bash 脚本。

Bash 脚本

Bash 脚本编写 是使用命令作为程序指令的编程方式。此策略通过使用逻辑门、条件、循环等表达式,将重复性任务自动化,简化为一行代码。因此,它便于将多个长命令合并成一段可执行代码

它提供了一个结构化、格式化和模块化的活动序列,并通过命令行参数使用动态值的命令。

在最简单的形式下,Bash 脚本由一个可执行文件组成,该文件包含一系列命令,并通过编程逻辑控制任务中的决策。掌握 Shell 脚本编写技巧对于在任何操作环境中管理系统至关重要。

关于如何编写 Bash 脚本的最佳方式,有大量的文献和参考资料。然而,按照本章开始时提到的简洁原则,可以总结如下:

#!/bin/bash  ← [1]
#
# IDENTIFICATION  ← [2]
#
# VARIABLES  ← [3]
# COMMANDS  ← [4]

让我们仔细看看:

  • [1] she-bangsh-bang:脚本的第一行以 #! 符号开头。这是一个两字节魔术数字,表示解释性脚本。接下来的语法是运行此脚本所需的命令解释器的名称。

注意

要了解魔术数字如何指示 Linux 中的文件类型,请参考file(1)magic(5)的手册页。

  • [2] IDENTIFICATION:脚本的标识部分至关重要且不可或缺。它是脚本的文档,包含了运行的常规操作的描述、版本信息,甚至是作者的详细信息和使用许可。

  • [3] VARIABLES:该部分声明了在脚本执行过程中使用的变量。变量存储系统内存中的信息。脚本使用局部变量,这些变量在短时间内存储信息。局部变量仅在脚本运行的 Shell 或会话中存在和有效。

  • [4] COMMANDS:该部分列出了要执行的常规操作和命令。它还包括用于在特定情况下指定执行命令的函数。

让我们看一个 Bash 脚本的例子。创建一个包含以下常规操作的脚本:

  1. 将文件命名为mytasks.sh

  2. 创建一个名为class工作目录

  3. 将以下文件连接为工作目录中的data文件:

    • /****etc/passwd

    • /****etc/group

    • /****etc/shadow

  4. 创建以下用户:

    • 约翰

    • 彼得

    • 马克

  5. data文件复制到用户的主目录。

  6. 生成一个活动日志

这些常规操作很简单:创建一个工作目录可以让管理员集中管理工作文件,这样备份就变得更加容易。从系统文件中,我们创建了一个数据文件,这个文件必须存在于每个用户的主目录中——这些用户如果在系统中不存在,就必须先创建它们。所有 Bash 脚本执行的常规操作和活动都应记录在日志文件中,以备文档记录。

由于 Bash 脚本涉及创建用户,因此运行脚本的用户必须具备适当的权限。一个最好的选择是使用sudo命令。这样,你可以授予非 root 用户创建用户的权限,并为涉及的文件分配所需的权限。

为了简化这个示例,我们将使用root用户。切换到root用户,请使用sudo命令:

$ sudo -i

创建 Bash 脚本的步骤如下:

  1. 创建并切换到class工作目录:

    # mkdir class
    # cd class
    
  2. 使用你喜欢的文本编辑器来创建mytasks.sh脚本文件。在本例中,我们将使用vim编辑器:

    # vim mytasks.sh
    
  3. 作为she-bang,添加#!/****bin/bash这一行:

图 10.2 – 创建 mytasks.sh 脚本

图 10.2 – 创建 mytasks.sh 脚本

  1. 填写IDENTIFICATION部分,描述脚本常规操作的信息,包括脚本的版本和作者:

图 10.3 – 脚本标识部分

图 10.3 – 脚本标识部分

  1. VARIABLES部分,我们需要声明静态信息,这些信息不会改变:

    1. class 工作目录的路径和名称:
    date command gets formatted as 2023-06-18 20:50.
    
    1. DATA_FILE的路径和名称:
    DATA_FILE=$WDIR/data
    
  2. 在命令部分,测试变量的使用,以及如何通过创建数据文件来记录和执行任务:

    1. 开始记录一个包含符号的头部,以便在日志中分隔活动:
    log levels (info, error, warn, and so on) allows you to distinguish the outputs of the activities.
    

注意

若要了解更多内核日志级别的相关信息,请参阅syslog(2)手册页。

  1. 记录第一个活动——即创建数据文件:
echo "$TIMESTAMP [INFO] Create data file" >> $LOG
cat /etc/passwd >> $DATA_FILE
cat /etc/group >> $DATA_FILE
cat /etc/shadow >> $DATA_FILE

以下截图可以看到同样的第一个活动:

图 10.4 – mytasks.sh 的第一个版本

图 10.4 – mytasks.sh 的第一个版本

现在,让我们测试一下这个 Bash 脚本的第一个版本:

  1. 设置运行权限mytasks.sh脚本:

    # chmod +x mytasks.sh
    

图 10.5 – 设置 mytasks.sh 的运行权限

图 10.5 – 设置 mytasks.sh 的运行权限

  1. 运行mytasks.sh脚本:

    # ./mytasks.sh
    
  2. 验证数据文件的创建:

图 10.6 – 验证数据文件

图 10.6 – 验证数据文件

  1. 验证活动是否记录在日志文件中:

图 10.7 – 验证日志文件

图 10.7 – 验证日志文件

如我们所见,data文件已创建,并且活动已记录在log文件中。

下一步是创建用户。但是,请注意,每次运行 Bash 脚本时,都必须创建data文件。在继续之前,现在是添加验证的好时机:

  • 如果data文件存在,则继续执行以下指令。

  • 如果data文件不存在,则可以创建它。

  1. 为了验证,使用COMMANDS部分,添加以下if-else语句:

    echo "$TIMESTAMP [INFO] Verify the data file" >> $LOG
    if [ -f $DATA_FILE ];
    then
      echo "$TIMESTAMP [OK] The data file exists" >> $LOG
    else
      echo "$TIMESTAMP [INFO] Create data file" >> $LOG
      cat /etc/passwd >> $DATA_FILE
      cat /etc/group >> $DATA_FILE
      cat /etc/shadow >> $DATA_FILE
    data file can be turned into a basic for loop:
    
    

    for i in passwd group shadow

    do

    cat /etc/$i >> $DATA_FILE

    Bash 中的for循环是一条允许代码反复运行的语句。COMMANDS部分如下所示:

    
    

图 10.8 – mytasks.sh 的 COMMANDS 部分

图 10.8 – mytasks.sh 的 COMMANDS 部分

保存更改并进行测试。

  1. 验证log文件记录中的运行:

图 10.9 – 验证日志文件的运行

图 10.9 – 验证日志文件的运行

  1. 删除data文件并重新运行mytasks.sh脚本,以确认创建了data文件:

    # rm -rf data
    

图 10.10 – 验证数据文件的创建

图 10.10 – 验证数据文件的创建

由于此更改有效,Bash 脚本的版本可能会更改:

# Version: 0.2 → Add data file validation

图 10.11 – 修改 mytasks.sh 版本

图 10.11 – 修改 mytasks.sh 版本

让我们继续添加例程活动。

该例程包括创建用户并将data文件复制到他们的主目录。这里使用了语句和循环的组合,以便每次添加新用户时,脚本都能被重用:

  1. 从记录活动开始,记录到log文件中:

    echo "$TIMESTAMP [INFO] Verify users" >> $LOG
    
  2. 使用for循环确认用户和他们主目录中的data文件。不要将用户名添加到脚本中,而是指示循环读取users文件作为输入。因此,将users文件添加到VARIABLES部分:

    USER_FILE=$WDIR/users
    
  3. 创建包含所需用户名的users文件:

图 10.12 – 用户文件

图 10.12 – 用户文件

  1. data文件复制到用户的主目录中。或者,您可以将data文件复制到其主目录,以防用户存在但data文件不存在。此语句采用嵌套 if 语句的形式:

图 10.13 – 嵌套 if 语句

图 10.13 – 嵌套 if 语句

  1. 运行mytasks.sh脚本并确认用户已创建,且data文件已复制到他们的主目录:

图 10.14 – 测试用户创建和数据文件复制

图 10.14 – 测试用户创建和数据文件复制

  1. 重新运行测试,确认用户创建和数据文件复制的条件:

图 10.15 – 创建和复制条件确认

图 10.15 – 创建和复制条件确认

  1. lucas 用户添加到 user 文件,并运行 mytasks.sh 脚本以确认用户已创建并且 data 文件已复制:

图 10.16 – 添加 lucas 用户

图 10.16 – 添加 lucas 用户

  1. 运行脚本并查看 log 文件:

图 10.17 – 审核用户创建并复制数据文件

图 10.17 – 审核用户创建并复制数据文件

  1. 嵌套的 if 语句起作用,并导致脚本的版本发生变化。由于这是满足常规需求的工作版本,因此它成为了脚本的第一个版本:

    # Version: 1.0 → Add user creation and customization
    

注意

你可以在本书的 GitHub 仓库中找到 mytasks.sh 脚本,网址是 github.com/PacktPublishing/Fedora-Linux-System-Administration/tree/main/chapter10/

这个小脚本展示了如何简化日常任务。如果你对某个常规操作是否可以用 Bash 脚本表示有所疑问,请记住系统管理员中最著名的语句之一:

如果你输入了两次,你应该只写一次脚本。”

当你修改脚本并管理其版本时,问题就会出现。

当我们需要管理不同版本的脚本或配置文件时,一个可以帮助我们的工具是 Git。

让我们来了解一下这个工具。

Git

Git 是由 Linus Torvalds 设计并于 2007 年发布的一种版本控制软件。它维护应用程序的版本,优先考虑 效率可靠性兼容性,并提供文件变更记录。它还协调多个人在共享代码库中的文件上所做的工作。

Git 在近年来支持了开源软件的增长,使全球程序员之间的协作变得更加容易。

版本控制允许你记录在一段时间内对文件或文件集所做的更改,以便以后可以检索到特定版本。

Git 是一个 分布式版本控制系统。除了中央仓库外,客户端还会复制整个仓库,包括其完整历史。因此,如果服务器发生故障,任何一个仓库客户端都可以将数据复制回服务器以恢复它。每个克隆都是所有数据的完整备份。

GitLabGitHub 提供了最重要的 基于 Git 的 web 服务。它们都允许我们共享项目的代码库,无论是 私有的 还是 公开的

要访问 GitHub,请使用你喜欢的浏览器并导航到 github.com/login。登录或按照页面上的说明创建账户:

图 10.18 – GitHub 登录页面

图 10.18 – GitHub 登录页面

要访问 GitLab,请使用您喜欢的浏览器并导航到gitlab.com/users/login。按照页面上的说明登录或注册以创建帐户。此外,您还可以考虑使用其他 Web 服务进行身份验证,例如GoogleGitHubTwitterBitbucketSalesforce。每个服务都会询问您是否允许 GitLab 访问您的帐户:

图 10.19 – GitLab – 登录页面

图 10.19 – GitLab – 登录页面

现在,让我们学习如何使用 Git。我们使用本地仓库,但它也可以托管在专用服务器或 Web 服务平台上。

注意

以下示例展示了如何使用 Git,因此 Web 上的公共仓库可以使用这两种免费的服务之一。明智选择

让我们从基础开始。

基础

Git 将文件视为迷你文件系统的一组快照。Git 中的大多数操作只需要本地文件和资源即可工作。Git 中的所有内容在存储之前都要经过验证。因此,它通过校验和(作为识别号)来标识。Git 通常会添加信息。

Git 有三种主要状态:modified(已修改)staged(已暂存)committed(已提交)

  • Modified(已修改)意味着文件已更改,但更改尚未提交到数据库。

  • Staged(已暂存)意味着已修改的文件在当前版本中被标记为准备进入下一个提交实例。

  • Committed(已提交)意味着更改已存储在本地数据库中。

下图展示了 Git 中一个文件的状态变化:

图 10.20 – Git 中文件状态更改流程

图 10.20 – Git 中文件状态更改流程

第二章中,我们在工作站上安装了 Git。现在让我们设置会话并创建一个示例仓库。请按照以下步骤操作:

  1. 作为非根用户,设置用户名并将其与电子邮件地址关联。此数据将在进行更改时保存。使用git config命令:

    $ git config --global user.name "username"
    $ git config --global user.email user@your-mail.com
    
  2. 创建一个目录并切换到该目录:

    $ mkdir git-basics ; cd git-basics
    
  3. 初始化目录为git仓库:

    $ git init .
    Initialized empty Git repository in /home/username/git-basics/.git/
    

    git-basics目录现在是一个git仓库,因此它可能包含之前提到的某个状态:

图 10.21 – git-basics 目录

图 10.21 – git-basics 目录

要查看仓库的状态,请在用户的配置文件中添加自定义配置。

  1. 将以下行添加到用户的.bashrc文件中:

    #
    # Lines added for git-prompt
    #
    git_prompt_sh='/usr/share/git-core/contrib/completion/git-prompt.sh'
    if [ -f ${git_prompt_sh} ]; then
      source ${git_prompt_sh}
      export GIT_PS1_SHOWDIRTYSTATE=true
      export GIT_PS1_SHOWUNTRACKEDFILES=true
      export PS1='[\u@\h \W$(declare -F __git_ps1 &>/dev/null && __git_ps1 " (%s)")]\$ '
    fi
    

图 10.22 – 将 git-prompt 添加到.bashrc 文件

图 10.22 – 将 git-prompt 添加到.bashrc 文件

  1. 保存此更改到.bashrc文件后,返回git-basics目录:

图 10.23 – git-basics

图 10.23 – git-basics

注意,提示符现在显示的是仓库的初始分支,名为master

master是默认的初始分支名称。在开始添加文件之前,让我们先重命名分支。

  1. 使用git branch命令将分支名称从master更改为main

    $ git branch –m master main
    

图 10.24 – 重命名初始分支

图 10.24 – 重命名初始分支

现在,是时候将文件添加到仓库中。

  1. 创建一个名为my-script.py的 Python 脚本,内容如下:

    name = input("What's your name? ")
    print(name + " Welcome!")
    

    运行此脚本后,你将被要求输入姓名,并看到欢迎信息。

  2. 保存文件后,通过运行git status命令审查仓库的状态:

    $ git status
    

图 10.25 – 审查仓库状态

图 10.25 – 审查仓库状态

命令的输出显示仓库已经发生了变化。请注意,某些文件在状态发生变化之前是未跟踪的。

提示符中,你现在应该能看到%符号,表示文件已被添加到仓库中。

让我们看看提示符中关于仓库状态的其他符号:

  • 未暂存(*****)

  • 已暂存(+

注意

要了解更多关于git-prompt的信息,请参考git-prompt.sh脚本的文档,路径为/usr/share/git-core/contrib/completion/git-prompt.sh

现在,让我们更改仓库的状态。

  1. 使用git add命令将文件添加到仓库:

    $ git add my-script.py
    

图 10.26 – 更改仓库状态

图 10.26 – 更改仓库状态

现在,仓库的状态显示为已暂存,提示符显示相应的符号(+)。

在我们将更改存储到数据库之前,需要测试文件并审查是否不需要进行其他更改。

  1. 运行my-script.py脚本并验证它是否正常工作:

    $ python my-script.py
    

图 10.27 – 测试 my-script.py 脚本

图 10.27 – 测试 my-script.py 脚本

由于脚本成功运行,我们来提交将文件添加到仓库中。

  1. 使用git commit命令将分支的状态更改为已提交,以便你可以应用对仓库所做的更改。此命令支持添加评论。该评论有助于通过简要描述更改,通知你仓库中所做的修改:

    $ git log command:
    
    

    $ git log

    
    

图 10.28 – 提交并审查仓库历史

图 10.28 – 提交并审查仓库历史

注意

如果你对如何添加一个好的提交信息有疑问,请访问在线资源Conventional Commitswww.conventionalcommits.org/

通过这些简单的步骤,我们的脚本仓库拥有了版本控制,并由 Git 管理。

拥有一个包含日常任务脚本的仓库,除了充当备份之外,还可以将其转变为我们领域的协作资源。如果我们希望将其发布到网页平台,我们需要 移除敏感数据,以便将其转变为社区的宝贵资源。

现在,让我们学习如何将两个脚本目录发布到互联网上的公共仓库中。

注意

在这个示例中,我使用的是 GitHub 上的仓库,但可以在任何 基于 Git 的网页平台 上以相同方式创建。

要在任何网页平台上创建仓库,请按照以下步骤操作:

  1. 登录到网页平台:

图 10.29 – 网页平台仪表板

图 10.29 – 网页平台仪表板

点击加号(+)按钮,然后点击 新建仓库

  1. 创建新仓库 界面将出现:

图 10.30 – 创建新仓库界面

图 10.30 – 创建新仓库界面

输入仓库的名称和简要描述其用途。将仓库设置为 公开 并选择 添加 README 文件。此文件提供欢迎消息或详细说明仓库的使用。

完成后,点击 创建仓库

  1. 仓库将被创建。点击 Code 按钮并复制仓库的 HTTPS 地址:

图 10.31 – 公共仓库

图 10.31 – 公共仓库

让我们将远程仓库复制为本地仓库。

  1. 创建一个工作目录并切换到该目录。使用 git clone 命令下载仓库的副本:

    $ git clone https://github.com/alex-sysadmin/daily-tasks-scripts.git
    

图 10.32 – 克隆仓库

图 10.32 – 克隆仓库

  1. 切换到该仓库,验证 主分支,并检查其中包含的文件:

图 10.33 – 验证仓库

图 10.33 – 验证仓库

  1. 将 Bash 脚本示例目录和前面的示例脚本复制到该目录中:

图 10.34 – daily-tasks-scripts 仓库

图 10.34 – daily-tasks-scripts 仓库

在使用 Git 添加文件之前,请注意 Bash 脚本中的 class 目录存在一些所有权问题。同时,注意 log 文件,它在脚本运行时会创建,但不需要包含在仓库中。users 文件包含有关已创建用户的 敏感信息。这两个文件必须保留,但其内容必须重置(这被称为 清空)。修复这些问题:

图 10.35 – 修复问题

图 10.35 – 修复问题

前面的示例目录 git-basics 也是一个仓库。删除其中的隐藏 .git 目录,以防 Git 将其视为主仓库的 子模块。我们只需要备份我们的脚本:

图 10.36 – 删除 .git 目录

图 10.36 – 删除 .git 目录

文件准备好后,让我们将它们添加到仓库中。

  1. 使用git add命令将所有文件添加到仓库中:

    $ git add .
    
  2. 通过添加相应的注释来提交更改到仓库。然后,运行git commit命令:

图 10.37 – 提交更改到仓库

图 10.37 – 提交更改到仓库

  1. 让我们将本地更改远程仓库同步。将原始仓库设置为上游仓库以引用它。使用git push命令设置上游仓库:

    $ git push --set-upstream origin main
    

图 10.38 – 推送更改到仓库

图 10.38 – 推送更改到仓库

注意

仓库的身份验证取决于所使用的网络平台。以 GitHub 为例,它会要求一个令牌。要生成令牌,请前往设置 | 开发者设置 | 个人访问令牌 | 生成新令牌并将其保存在安全的地方。

  1. 修改后的文件将出现在网络平台上:

图 10.39 – GitHub 上的仓库

图 10.39 – GitHub 上的仓库

这样,我们就有了一个协作的脚本库,用于我们日常任务中的使用。

与此同时,仓库也作为我们脚本的备份。

让我们了解一下可能有助于我们进行备份的其他选择。

不要忘记备份

备份:系统管理员必须备份整个系统,并始终验证 备份。

备份是最基本的任务。

好的实践始于良好的习惯。多年来,我养成的一个习惯是,在编辑文件之前先备份文件,无论是直接备份在文件路径中,还是通过创建备份目录:

图 10.40 – 修改前备份

图 10.40 – 修改前备份

表面上看,这似乎不是一项大任务。然而,如果你把这个步骤融入到你的任务中,那么你就拥有了一个简单的备份,保护你工作的文件。

除了复制文件进行更改,另一个好习惯是创建一个包含归档或压缩文件的包。

让我们看看这个是如何工作的。

归档与压缩

首先,请注意归档文件与压缩文件之间的区别:

  • 归档文件由文件和目录存储在一个文件中。归档文件保持未压缩状态——它占用的磁盘空间与所有单独文件和目录的总和相同。

  • 压缩文件同样由文件和目录组成,它们被存储在一个文件中。然而,它们的存储方式使得它们占用的磁盘空间比所有单独文件和目录的总和要少。

归档文件本身没有压缩,但压缩文件可能包含一个归档文件。

Linux 提供了多个用于压缩和解压缩文件的工具。下表显示了最常用的工具:

工具 语法 示例
gzip gzip <file>``gunzip <file> `
--- --- ---
gzip test``$ gunzip test.gz
bzip2 bzip2 <file>``bunzip2 <file> `
--- --- ---
bzip2 test``$ bunzip2 test.bz2
xz xz <file>``xz -``d <file> `
--- --- ---
xz test``$ xz -``d test.xz
zip zip .``zip-file <file>``unzip <.zip-file> $ zip test.zip test``$ unzip test.zip

表 10.1 – 压缩和解压缩工具

让我们来看一下各压缩工具所使用的压缩算法的不同之处:

  • gzip工具通过Lempel-ZivLZ77)编码来压缩文件大小。每个文件会被替换成一个扩展名为.gz的文件。

  • bzip2使用Burrows-Wheeler块排序文本压缩算法和哈夫曼编码来压缩文件。每个文件都会被替换成另一个扩展名为.bz2的文件。

  • xz使用Lempel-Ziv-Markov 链算法LZMA)进行压缩/解压缩。

  • .zip格式使用32 位 CRC 算法。它为每个条目包含两份元数据副本,以提供更好的防止数据丢失的保护。

注意

要了解更多压缩工具的使用,请参考gzipbzip2xzzip的 man 手册。

在实际使用场景中,压缩工具的共同目标是:减少文件的空间占用。除了算法之外,还需要考虑文件减少的空间大小。这是因为便携性原因。以下示例实际展示了它们之间的差异。不过需要注意的是,更多的压缩或解压缩操作会消耗更多的资源(CPU 和内存)。

另一个最佳实践是压缩备份文件,以避免所占用的空间成为问题。如果我们压缩文件归档,这一做法还能进一步提高效果。

在 Linux 中,我们可以使用tar工具来创建、管理和提取归档文件。通过这个命令,许多存储在单一归档文件中的文件变得可以移植。一个tar归档是一个结构化的元数据和文件数据序列,并且带有索引。

在创建归档文件时,归档可能会被压缩,使用的是支持的某种压缩算法。除了创建归档文件外,tar命令还提供了其他选项,例如列出归档内容而不提取,或者从压缩和未压缩的归档中提取文件。

tar命令的选项为我们提供了三种 不同的风格

  • 传统风格中,第一个参数是一组选项字母,后面的参数为那些需要的选项提供对应的值。

  • UNIX 或简短选项风格中,每个选项字母前面都会加上一个单一的连字符(-)。如果选项需要参数,参数紧跟在选项后面,可能是命令行中的独立词,也可能紧接在选项后面。

  • GNU 或长选项样式中,每个选项以两个连字符(--)开头,并有一个有意义的名称,由小写字母和连字符组成。长选项可以缩写为其首字母。长选项的参数作为单独的单词出现在命令行中,紧接在选项后面,或通过等号(=)与选项分隔,中间没有空格。可选参数应始终使用后一种方法。

tar命令需要至少一个选项的操作。以下表格显示了最常用的操作和选项:

描述 传统样式 简短样式 长样式
创建归档文件 c -c --``create
列出归档文件的内容 t -t --``list
提取归档文件 x -x --``extract
显示当前归档或提取的文件 v -v --``verbose
使用此选项并指定文件名以创建或打开 f -f --``file
提取时保持文件的原始权限 p -p --``preserve-permissions
使用文件后缀来确定压缩时使用的算法 a -a --``auto-compress
使用gzip压缩算法 z -z --``gzip
使用bzip2压缩算法 j -j --``bzip2
使用xz压缩算法 J -J --``xz
使用 LZ 变体算法 Z -Z --``compress
启用扩展属性支持并存储文件扩展属性 NA NA --``xattrs
启用 SELinux 上下文支持并存储 SELinux 上下文 NA NA --``selinux

表 10.2 – 常见 tar 命令操作与选项

让我们分析一个例子,看看如何使用两种不同的压缩算法创建压缩文件归档,以便我们比较使用的空间。

作为非根用户,在我们的备份目录中,使用tar命令创建日常脚本仓库的归档文件。

按照以下步骤操作:

  1. 首先,使用gzip压缩算法选项。运行以下tar命令:

    $ tar czvf daily-tasks-scripts.bkp.tar.gz ../wdir/daily-tasks-scripts
    

图 10.41 – 使用 tar 命令创建归档文件

图 10.41 – 使用 tar 命令创建归档文件

  1. 现在使用bzip2压缩算法,并通过运行以下tar命令从相同目录创建文件归档:

    $ tar cjvf daily-tasks-scripts.bkp.tar.bz2 ../wdir/daily-tasks-scripts
    

图 10.42 – 使用 bzip2 压缩算法与 tar 命令

图 10.42 – 使用 bzip2 压缩算法与 tar 命令

  1. 检查两个文件归档的大小,以比较压缩率:

图 10.43 – 比较文件大小

图 10.43 – 比较文件大小

如我们所见,仓库目录的大小是42272字节:

$ du -sb ../wdir/daily-tasks-scripts
gzip algorithm, is 18947 bytes:

$ du -sb daily-tasks-scripts.bkp.tar.gz

bzip2 算法的文件大小为 18594 字节:

$ du -sb daily-tasks-scripts.bkp.tar.bz2
18594 daily-tasks-scripts.bkp.tar.bz2

让我们更详细地看看这意味着什么:

  • gzip压缩算法将仓库大小减少了 44.82%

  • bzip2压缩算法将仓库大小减少了 43.98%

区别很小,这些是最常用的备份格式。由你决定使用哪种格式 —— 只要记住:不要忘记 备份!

注意

要了解更多关于归档文件的信息,请参考tar手册页。

如前一节所述,Git 仓库还帮助我们备份其中包含的信息。它还为我们提供了数据在获取时的快照。

所以,让我们学习如何使用 Git 进行版本管理。

使用 Git 进行版本管理

要了解 Git 如何处理版本控制,我们将使用上一节中的示例。作为非根用户,切换到daily-tasks-scripts仓库目录并按照以下步骤操作:

  1. 作为一个好习惯,在开始之前确保本地仓库与远程仓库保持更新和同步。使用git fetchgit pull命令来完成:

    $ git fetch --all
    $ git pull --all
    

图 10.44 – 保持仓库更新和同步

图 10.44 – 保持仓库更新和同步

git fetch命令从远程项目中提取所有本地不存在的数据。git fetch命令只将数据下载到本地仓库 —— 它不会合并数据或更改你当前正在处理的内容。必须手动合并这些更改。

git pull命令将远程仓库的更改合并到本地仓库的当前分支中。

作为另一个好习惯,Git 为你提供了分支仓库的选项。分支意味着偏离开发的主线并继续工作,而不改变主线。

要更改仓库而不改变其原始内容,让我们创建一个分支。

  1. 创建daily-tasks-scripts仓库的增强分支。使用git branch命令和适当的分支名称:

    $ git branch acallejas/repo-enhancement
    

注意

将更改的作者包含在分支名称中是一个良好的实践。

git branch命令仅创建分支。要切换到该分支,请使用git switch命令并输入分支名称。

这两个命令可以组合使用来创建分支并切换到该分支。为此,使用git switch命令并加上-c选项:

$ README.md file that serves to document the use of the repository. Let’s update this file so that we can document our repository.

1.  Update the **README.md** file so that it documents the repository:

![Figure 10.45 – Updating the README.md file](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_45.jpg)

Figure 10.45 – Updating the README.md file
Note that the `README.md` file format uses the Markdown language. Markdown is a *markup language* that appears to human readers when it’s in its source code form.
Note
To learn more about the basic syntax of the Markdown language, refer to the *Markdown guide* at [`www.markdownguide.org/basic-syntax`](https://www.markdownguide.org/basic-syntax).
Save the changes and compare the differences between this branch and the main branch.

1.  Use the **git diff** command to display the differences between the branches:

    ```

    $ git diff

    ```

![Figure 10.46 – Displaying the differences between the branches](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_46.jpg)

Figure 10.46 – Displaying the differences between the branches
The lines that begin with plus (`+`) signs state the differences in the `README.md` file between the versions of the branches.

1.  Add and commit the changes to the file to update the branch:

    ```

    $ git add README.md

    $ git commit –m "更新 README.md 文件"

    ```

     2.  Update the remote repository with the local enhancement branch. Use the **git push** command and the branch’s name to do so:

    ```

    $ git push –u origin acallejas/repo-enhancement

    ```

![Figure 10.47 – Updating the remote repository](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_47.jpg)

Figure 10.47 – Updating the remote repository

1.  For the local repository, use the **git branch** command to show the branches of the local repository. The **-r** option lists the remote branches while **-a** lists all branches:

    ```

    $ git branch

    $ git branch -r

    $ git branch -a

    ```

![Figure 10.48 – Listing the branches](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_48.jpg)

Figure 10.48 – Listing the branches

1.  The web platform also displays the branches. Navigate to the repository and click on the **Branches** link:

![Figure 10.49 – Branches](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_49.jpg)

Figure 10.49 – Branches
The changes haven’t been applied to the *main branch* yet. To do this, we need to *merge* the branch with the enhancements. This should be done through a pull request.
Let’s integrate the enhancements into the *main branch*.

1.  In the **Branches** window, click on the **New pull** **request** button:

![Figure 10.50 – The Branches window](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_50.jpg)

Figure 10.50 – The Branches window

1.  In the **Open a pull request** window, fill in the title and description fields of the change:

![Figure 10.51 – The Open a pull request window](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_51.jpg)

Figure 10.51 – The Open a pull request window

1.  In this window, at the bottom, find and verify the details of the change.
2.  In the right column, in the **Reviewers** section, select members of the team to *review the changes before merging them*, as good practice.
3.  Once the request is ready, click on the **Create pull** **request** button.
4.  The **Pull requests** window will appear, providing an overview of the change:

![Figure 10.52 – The Pull requests screen](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_52.jpg)

Figure 10.52 – The Pull requests screen
The **Commits** tab lists the changes committed, while the **Files changed** tab displays the difference between versions of the changed files.

1.  If you agree with the merger of the branches, click on the **Merge pull** **request** button.
2.  On confirming the merge, the repository on the web platform will display the changes that have been made:

![Figure 10.53 – Remote repository updated](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_53.jpg)

Figure 10.53 – Remote repository updated
Let’s update the local repository.

1.  In the local repository, switch to the main branch and update it:

![Figure 10.54 – Local repository updated](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_54.jpg)

Figure 10.54 – Local repository updated
By using the branches of the repository, a backup of our script directory of day-to-day tasks becomes available.
By archiving files to Git repositories, a backup could exist. Of course, there are software solutions that ease this task, even IT areas in charge of it. But the best practice is, regardless of these solutions, as the first law of the SysAdmin says: *always back up*. These activities, like many others, can be run in an automated fashion using scripts and/or scheduled tasks.
Now, let’s look at a tool that might make it easier to automate tasks in Linux.
Automating with Ansible
*Automate: A SysAdmin should automate as much as possible, except if it conflicts with the* *first law.*
Previously, we discussed one of the most well-known statements among SysAdmins:
“*If you typed it twice, you should have scripted* *it once.*”
If a task gets scripted, it could be scheduled and, with this, automated. This means that the task must run without the SysAdmin’s intervention.
However, not all scheduled tasks might be automated. The easiest way to determine this is by applying the *principles* *of automation*:

*   The rule of **algorithmic thinking**

    *Everything is a system*. Algorithmic thinking is a way of getting to a solution through a clear definition of the steps needed – *nothing happens* *by magic*.

*   The rule of **bottlenecks**

    *Bad decisions propagate*. Every system, regardless of how well it works, has at least one constraint (*a bottleneck*) that limits performance.

*   The rule of **autonomy**

    *Humans always play a role*. This challenges the idea that automation means completely eliminating the human element. Using the rule of algorithmic thinking, humans are another layer of abstraction in the system.

Taking these principles into account and applying them allows us to define an ideal automation solution.
The **Ansible project** is an open source community sponsored by Red Hat, the developer of the automation tool. Ansible is available in the official Fedora Linux repositories.
**Ansible** is an automation tool that focuses on simplicity and ease of use as its primary goals. Ansible enables us to configure systems, manage packages, and, in an advanced way, orchestrate tasks that support the continuous delivery cycle.
Ansible works in two separate layers:

*   *Control plane* (in the cloud): Generate instances and manage their resources
*   *On instance*: Syart and stop services, push configuration files, install packages, and more

Through OpenSSH, an agentless service, Ansible manages systems and performs the tasks described previously.
Let’s start with the basics of Ansible.
The basics
The Ansible use cases approach deals with automating processes. Let’s look at some of the most common options:

*   Provisioning
*   Configuration management
*   Application deployment
*   Continuous delivery
*   Security and compliance
*   Orchestration

Ansible divides process automation into a variety of work unit levels:

*   **Tasks**: A task is the smallest unit of work. It could consist of an action such as *install a database*, *install a web server*, or *copy this configuration file to* *the server*.
*   **Plays**: A play consists of several tasks. For example, the play *Prepare a database for a web service* could consist of the following tasks:
    1.  Install the database package.
    2.  Set the password for the database administrator.
    3.  Create the database.
    4.  Define the access to the database.
*   **Playbook**: A playbook consists of several plays. For example, the playbook *Install a website with a database backend* could consist of the following tasks:
    1.  Configure the database server.
    2.  Configure the web server.

The task automation process within Ansible requires two types of nodes to be defined: *control* and *managed*. The control node is the machine from where Ansible is running and the managed nodes are the machines where the tasks take place:
![Figure 10.55 – Ansible nodes](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_55.jpg)

Figure 10.55 – Ansible nodes
Let’s learn how to prepare our workstation as the Ansible control node.
First steps
So, let’s start with installing and configuring Ansible. Follow these steps:

1.  Ansible is available in the official Fedora Linux repositories. Install the **ansible** package using the **dnf** command:

    ```

    $ /etc/ansible/ansible.cfg 文件,但作为最佳实践,请创建一个自定义文件以更好地控制自动化任务。

    ```

     2.  Inside the working directory, create a directory named **ansible** and switch to it:

    ```

    $ mkdir ansible; cd ansible

    ```

     3.  Inside the **ansible** directory, create a configuration file called **ansible.cfg**. Only add the **defaults** section and set up the name of the **inventory** file, something like this:

    ```

    [defaults]

    inventory = inventory

    ```

    Ansible does not install an agent on the managed nodes. Instead, it bases its communication with them through **SSH keys**. So, let’s create a key that we’ll use to manage the nodes.

     4.  Create an **ssh** key pair and specify **rsa** with **2048** bits. Use the **ssh-keygen** command to do so:

    ```

    $ ssh-keygen -t rsa -b 2048

    ```

![Figure 10.56 – Generating the ssh key pair](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_56.jpg)

Figure 10.56 – Generating the ssh key pair
The `ssh-keygen` command will ask you to confirm the path where the key should be generated. Note that the path must not be the *default path* but inside the working directory in the `ansible` directory.
After this, the command prompts you to enter a passphrase instead of the user’s login password. As the connection must run *unattended*, leave this field empty and hit *Enter* twice to confirm the creation of the key pair.
Note
To learn more about creating SSH keys, refer to the **ssh-keygen** man page.
This command generates the SSH key pair, which consists of a *public key* and a *private key*.
Let’s test the communication with the key without using the user’s access password.

1.  To test communication with the SSH key, copy the key to the same workstation. Use the **ssh-copy-id** command to do so:

    ```

    $ ssh-copy-id -i id_rsa.pub localhost

    ```

![Figure 10.57 – Copying the ssh public key](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_57.jpg)

Figure 10.57 – Copying the ssh public key
The `ssh-copy-id` command copies the public key to the machine where the trust relationship gets established. In our case, this is the workstation itself.
To copy the key, the command asks for the user’s one-time login password.
Once access is granted, the command copies the content of the public key to the `authorized_keys` file in the hidden `.ssh` directory inside the user’s home directory.

1.  To test the connection, use the **ssh** command to offer the private key:

    ```

    $ ssh -i id_rsa localhost

    ```

![Figure 10.58 – Testing the trust relationship](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_58.jpg)

Figure 10.58 – Testing the trust relationship
The connection that uses the private key must allow access and run remote commands without requesting the user’s password.
Now, let’s configure and automate simple tasks with Ansible.

1.  Create the **inventory** file, adding **workstation** as a managed node, indicating the private key as a variable for the host. In the **inventory** file, add the following lines:

    ```

    [workstation]

    localhost

    [workstation:vars]

    ansible_ssh_private_key_file=/home/acallejas/wdir/ansible/id_rsa

    ```

    Let’s test the communication to the managed nodes using ad hoc commands. Ad hoc commands allow us to run basic tasks from the command line.

     2.  Use the **ansible** command, along with the **ping** module, to verify the connection to all managed nodes:

    ```

    $ ansible all -m ping

    ```

![Figure 10.59 – Testing communication with all managed nodes](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_59.jpg)

Figure 10.59 – Testing communication with all managed nodes
This ad hoc command allows us to run any operating system command by passing it as an argument using the `-a` or `--``args` option:

$ ansible workstation -a "hostname"


 ![Figure 10.60 – Using ad hoc commands](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_60.jpg)

Figure 10.60 – Using ad hoc commands
Note
For more information, refer to the *Introduction to ad hoc commands* section of the Ansible documentation at [`docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html`](https://docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html).
As mentioned previously, the *tasks* and the set of them (*plays*) get grouped into a file. This file allows us to run them *sequentially or selectively*. These files are called *playbooks*.
*Playbooks* come written in `YAML` format. This format, due to its simplicity, is based on a tree structure, which makes it more human-readable than a `JSON` or `XML` file. This means that it maintains its ideal of being the simplest implementation tool.
Let’s analyze a *playbook* through a simple example. The following *playbook* runs the *tasks* to verify that a web server has the `httpd` package installed and running:

verify_webserver.yaml


 ![Figure 10.61 – Ansible playbook](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_61.jpg)

Figure 10.61 – Ansible playbook
Let’s take a closer look:

*   **<1>** **Identification block**: This assigns a *name* to the play and the *hosts* in the inventory where the play applies.
*   **<2>** **Variables block**: This declares the *variables* used in the play.
*   **<3>** **Tasks block**: This declares the *tasks* to run in the play.

In the preceding example, the *play* consists of two *tasks*:

*   Verify that the **httpd** package has been installed as the latest version
*   Verify that the **httpd** service has been enabled and started

For each of these tasks, the play uses two modules: `ansible.builtin.package` and `ansible.builtin.service`.
The modules consist of small units of code that perform tasks using instructions from the operating system. In the preceding example, the `ansible.builtin.package` module uses the `dnf` command to determine the *installation status* of the package.
The `ansible.builtin.service` module uses the `systemctl` command to determine the *status of* *the service*.
Since the syntax of the playbooks uses the `YAML` file format, they are prone to indentation failures. Ansible provides a tool to check the syntax of playbooks – that is, the `ansible-playbook` command with the `--``syntax-check` option:

$ ansible-playbook --syntax-check verify_webserver.yaml


 ![Figure 10.62 – Verifying the syntax of the playbook](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_62.jpg)

Figure 10.62 – Verifying the syntax of the playbook
The output of this command explains the syntax error. The error message shows where you can find the runtime error.
Note
Although the error message displays the error’s location, it could be a result of an error that wasn’t caused by the displayed location. As a best practice, confirm the correct indentation on the lines before the error message.
After editing the playbook and fixing the error, run the syntax check again:

$ ansible-playbook --syntax-check verify_webserver.yaml


 ![Figure 10.63 – Running the syntax check on the playbook once more](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_63.jpg)

Figure 10.63 – Running the syntax check on the playbook once more
This output indicates that our playbook has the correct syntax and is ready to run.
Another test tool provided by Ansible consists of running it in *dry-run mode*. Use the `ansible-playbook` command with the `--check` option to run in dry-run mode:

$ ansible-playbook --check verify_webserver.yaml


 ![Figure 10.64 – Running the playbook in dry-run mode](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_64.jpg)

Figure 10.64 – Running the playbook in dry-run mode
Dry-run mode doesn’t send any error message, so it is considered positive. Note that no change occurred; the playbook only tested if the tasks could be run on the managed node.
Now that there are no errors in the tests, run the playbook:

$ ansible-playbook verify_webserver.yaml


 ![Figure 10.65 – Running the playbook](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_65.jpg)

Figure 10.65 – Running the playbook
The `failed=0` output indicates that the tasks were finished successfully.
As predicted, in dry-run mode, the playbook does not generate errors.
Confirm this by logging into the host and verifying that the tasks are running:
![Figure 10.66 – Verifying tasks](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_66.jpg)

Figure 10.66 – Verifying tasks
Note
To learn more about Ansible, refer to the Ansible documentation at [`docs.ansible.com/index.html`](https://docs.ansible.com/index.html).
Automating with Ansible simplifies day-to-day tasks.
*Automate as much as possible, so long as that automation* *impacts backups.*
Automating enables us to get free time. Free time must be used to develop or learn new skills. *A SysAdmin never* *stops learning*.
Finally, let’s take a look at a few resources where we can learn about and develop new skills.
Never-ending study
*Study: A SysAdmin must have free time to study, so long this free time does not conflict with the first or* *second law.*
Infrastructure profiles have been evolving the most in recent years. It is not that it is going to disappear as a job role, but it is reinventing itself and new skills are emerging. Thus, professionals must gain new knowledge.
The best advice is to always go back to the basics – that is, to programming and coding. Knowing all sides of the system is what assures the *SysAdmin* that they can continue to play a fundamental role. Each new technology can mean better work in less time with greater control of the system.
When new technology comes along, it takes time to study and implement it while keeping the system active, all while increasing productive leisure time.
A SysAdmin needs commitment to continuous learning while keeping up to date with the latest technology trends and best practices.
In these same years, the boom of online education took place. Among the many resources available, there are several to take advantage of.
Let’s review those that could develop our skills as a SysAdmin:

*   A great place to develop **vim** editor skills is VimTricks. **VimTricks** ([`vimtricks.com/`](https://vimtricks.com/)) collects the necessary tricks, how-tos, guides, videos, links, and plugins and posts them on their social networks and as an e-newsletter. The site offers an extensive archive of published material, as well as a book and the possibility to register to receive the e-newsletter regularly:

![Figure 10.67 – The VimTricks web page](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_67.jpg)

Figure 10.67 – The VimTricks web page

*   Another recommendation is a page that brings together the tools and training needed to develop with the new **Red Hat** technologies – that is, [`developers.redhat.com/:`](https://developers.redhat.com/:)

![Figure 10.68 – The Red Hat Developer web page](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_68.jpg)

Figure 10.68 – The Red Hat Developer web page
Red Hat Developer is a community that provides tools, training, and technical talks designed to help developers improve their skills and keep up-to-date on the technologies that are shaping the future.
Subscribers have access to weekly DevNation technical talks, technology deep dives, and open source tutorials.
The subscription is free – just sign up and you’ll also get access to other official Red Hat learning and reference sites.

*   As a form of support to help you get solid knowledge of Ansible and automate tasks with this tool, you can go to [`www.ansiblepilot.com/`](https://www.ansiblepilot.com/):

![Figure 10.69 – The Ansible Pilot learning page](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_69.jpg)

Figure 10.69 – The Ansible Pilot learning page
*Luca Berton*, Ansible automation expert and author of several books on automation, offers many examples, pieces of code, and videos of automating tasks of all kinds on his site. This is a great place to learn and practice task automation.

*   **Kubernetes** is the *new paradigm of technology*. It is an open source container orchestrator that manages scalable applications. A good place to start with this technology is [`kubebyexample.com/`](https://kubebyexample.com/):

![Figure 10.70 – The Kube by Example web page](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_70.jpg)

Figure 10.70 – The Kube by Example web page
With Kube by Example, you can learn about Linux principles, the basics of Kubernetes, developing applications deployed on the platform, and applicable security best practices. It includes lots of downloadable code, practical examples, and videos so that learning can flow naturally.

*   At **Packt Publishing** we don’t lag behind. You can access free learning content at [`www.packtpub.com/free-learning`](https://www.packtpub.com/free-learning):

![Figure 10.71 – Packt free learning](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_10_71.jpg)

Figure 10.71 – Packt free learning
By registering, you will have unlimited access to thousands of learning materials such as e-books and video courses – free content forever! Don’t miss the opportunity and sign up for your free trial.
*Remember, a SysAdmin is always* *reinventing themselves.*
*It’s not magic. It’s talent* *and sweat.*
There is a lot of reference material that could guide you in your development as a *SysAdmin*. However, if you want to look at it from a simple point of view, these three principles should help you create good habits that generate good practices.
I hope you find them useful.
In the next chapter, we will discuss the use and practical application of operating system tuning.
Summary
In this chapter, we reviewed the path to becoming a SysAdsmin and proposed three basic rules for the acquisition of good habits.
First, we looked at *backing up* any information with which we have contact with the system. This backup will ensure that changes that are made can be returned safely in case they’re needed. It also provides a snapshot of the system at a certain point in time. Next, we looked at various task *automation* techniques to streamline our daily routine. This principle lightens your workload and allows SysAdmins to get free time that might be occupied with expanding their knowledge. The last point we considered was to *never stop learning*. Since technology is advancing faster and faster, this should be taken as a responsibility.
In the following chapters, we will make use of these principles and apply them to various particular aspects of *systems administration*. We will start by learning how to tune operating systems.


第十三章:性能调优最佳实践

性能调优系统资源是任何系统管理员的主要(甚至最重要的)任务之一。性能调优是调整系统配置以提高计算资源使用、数据吞吐量或用户体验的过程。它需要深入理解系统的硬件和软件组件,以及它们之间的许多交互。

性能调优常常与故障排除混淆,但它们之间有显著区别。在故障排除过程中,主要目标是找出并解决问题,使系统正常运行。而在性能调优中,目标是让系统在其所处的资源和环境下以最佳性能运行。

本章将涵盖以下主要主题,帮助您在系统的关键方面获得最佳性能:

  • 理解内核调优

  • 主要调优 – CPU 和内存

  • 不要忽视存储调优

  • 通过网络调优提升性能

技术要求

为了完成本章中的主题,您需要安装每个部分中指示的软件包。在那里,您会找到每个需要安装的软件包的说明。

本章中创建的示例可以从本书的 GitHub 仓库下载:github.com/PacktPublishing/Fedora-Linux-System-Administration/tree/main/chapter11

理解内核调优

Linux 内核通过/proc目录暴露用户空间可调配的信息和配置/proc文件系统充当了内核数据结构和运行时信息的接口。它提供了一种访问有关进程、系统配置、硬件等详细信息的方式,通过虚拟文件的层级结构公开这些数据。

/proc目录中的文件包含系统信息,如内存(meminfo)、CPU(cpuinfo)和可用的文件系统。

/proc/sys子目录包含可配置参数,用于调整内核行为和活动。/proc/sys中的大多数文件可以由root用户修改。修改/proc/sys中的文件会立即对运行中的系统产生影响。

注意

更改/proc/sys中的文件可以提高运行系统的性能,但也可能导致性能下降。在对这些文件进行任何更改时,请小心。记得在更改前进行备份。

/proc/sys内,文件按可调内核类型组织成子目录树:

  • /proc/sys/dev:包含系统设备的可调参数

  • /proc/sys/fs:包含与文件系统相关的可调变量

  • /proc/sys/kernel:包含改变内部 内核操作的调整

  • /proc/sys/net:包含改变网络配置的可调参数

  • /proc/sys/vm:包含更改内核虚拟内存管理的参数

注意

请记住,并非所有/proc/sys中的文件都是可写的。有些文件仅能由操作系统本身修改。

让我们看看如何更改这些内核可调项。

调整内核参数

以下图示说明了如何以不同方式更改内核参数:

图 11.1 – Linux 性能调优工具,Brendan Gregg 著(CC BY-SA 4.0)

图 11.1 – Linux 性能调优工具,Brendan Gregg 著(CC BY-SA 4.0)

作为系统管理员,最佳实践是通过命令行进行更改,如下所示:

  • 使用文本编辑器更改/proc/sys文件

  • 使用echo命令设置可调项中的简单值

  • 使用sysctl命令按名称设置参数

  • /etc/sysctl.d目录中创建包含所需参数的文件

注意

直接更改内核可调值并不会使其在重启时保持持久性。要实现这一点,必须使用sysctl命令,或者在/etc/sysctl.d目录中创建一个包含更改的文件。

让我们看看每种不同方法的示例。按照以下步骤操作:

  1. 查看/proc/sys/net/ipv4目录中icmp_echo_ignore_all可调项的内容:

    # cat /proc/sys/net/ipv4/icmp_echo_ignore_all
    0
    

    这个值(0)表示存在icmpping)支持:

图 11.2 – 工作站支持 ping

图 11.2 – 工作站支持 ping

让我们进行更改。

  1. 使用echo命令将可调项的值从false0)更改为true1):

    # echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_all
    

    确认可调项的更改:

    # cat /proc/sys/net/ipv4/icmp_echo_ignore_all
    1
    

    如你所见,操作系统不再允许ping

图 11.3 – 操作系统不允许 ping

图 11.3 – 操作系统不允许 ping

这个更改也可以通过sysctl命令进行。

让我们开始吧。

  1. 可调项的路径可以通过将/proc/sys下的斜杠替换为点表示。在我们的示例中,可调项如下:

    sysctl command with the –w option to enable ping:
    
    

    sysctl -w net.ipv4.icmp_echo_ignore_all="0"

    net.ipv4.icmp_echo_ignore_all = 0

    
    Confirm that `ping` support is already enabled:
    

图 11.4 – 允许 ping

图 11.4 – 允许 ping

然而,这个更改在重启时不会保持。

让我们看看如何做这个。

  1. 在启动时,内核会从这些目录中的配置文件加载可调设置:

    • .conf扩展名,且可调设置为true

      # sysctl command with the -p option to apply the setting:
      
      

      sysctl -p /etc/sysctl.d/ping.conf

      net.ipv4.icmp_echo_ignore_all = 1

      
      Confirm the `ping` behavior change:
      

图 11.5 – 不允许 ping

图 11.5 – 不允许 ping

这些是更改内核可调项的不同方法。要获取内核可调项的列表,可以使用sysctl命令的-a选项:

# sysctl –a

图 11.6 – 内核可调列表

图 11.6 – 内核可调列表

对内核可调参数所做的更改会修改操作系统的行为。现在,让我们看看操作系统主组件的某些特定方面的更改。

主要调优 - CPU 和内存

在前一节中,图 11.1 展示了帮助我们收集系统资源使用性能信息的不同工具。

工具显示系统信息,如空闲磁盘空间、CPU 温度以及其他重要组件,还包括网络信息,如系统的 IP 地址和当前上传下载速率。

监控运行系统的资源是系统管理员的主要任务之一。系统监控的目标是确定当前的性能是否符合指定的技术要求。

监控资源性能帮助我们了解需要改进的领域。

在更改 CPU 和内存使用配置之前,让我们简要了解一下 Fedora Linux 中包含的基本监控工具。

监控工具概述

监控工具提供每个进程的统计信息,并基于进程结构或来自内核的系统范围统计信息。监控工具通常对普通用户可用,但对于更精细的详细信息,则需要系统管理员权限。

pstop 命令是提供进程统计信息的最常用命令,包括 CPU 和内存使用情况。

作为 root 用户,使用 aux 选项运行 ps 命令将按用户列出具有扩展详细信息的进程:

# ps aux

图 11.7 –  命令的输出

图 11.7 – ps aux 命令的输出

以下表格按列描述了ps aux命令的输出:

描述
USER 运行此进程的用户。
PID 此进程的进程 ID。
%``CPU 此进程使用的 CPU 时间(以百分比表示)。
%``MEM 此进程使用的物理内存(以百分比表示)。
VSZ 此进程使用的虚拟内存(以字节为单位)。
RSS 常驻内存集大小,不可交换的物理内存使用量(以 KiB 为单位)。
TTY 启动进程的终端。问号?)表示该进程不是从终端启动的。
STAT 进程状态。
START 进程的启动时间和日期。
TIME 此进程使用的总 CPU 时间。
COMMAND 启动进程的命令及其所有参数。

表 11.1 – ps aux 命令输出描述(按列)

*更多详细信息将在以下部分提供。

注意

有关进程状态代码的更多信息,请参阅ps的手册页。使用man ps命令。

ps 命令不同,ps 输出是静态的,而 top 命令提供了进程活动的实时报告。它还提供了一个界面,用于筛选和操作监控数据:

# top

图 11.8 – top 命令的输出

图 11.8 – top 命令的输出

命令输出的标题提供有关当前系统行为的一般信息:

  • 第一行包括当前时间、系统运行时间、连接的用户数量,以及过去 1、5、15 分钟的平均负载

  • 第二行显示任务数量及其状态:运行、睡眠、停止或僵尸

  • 第三行显示不同的 CPU 使用值(在运行时):

    • us: CPU用户空间 中运行进程所花费的时间

    • sy: 花费时间运行 系统内核 空间 进程

    • ni: 花费时间运行具有手动设置 nice 值的进程

    • id: CPU 空闲时间

    • wa: CPU 花费时间等待 I/O 任务完成

    • hi: 花费时间服务 硬件中断

    • si: 花费时间服务 软件中断

    • st: 由于运行虚拟机而丧失的时间(steal time

  • 第四行显示物理内存的总量(以 KiB 为单位),以及空闲、已用、已缓存或缓冲的内存量

  • 第五行显示交换内存的总量(以 KiB 为单位),以及空闲、已用和可用的交换内存

以下表格逐列描述 top 命令的输出:

描述
PID 进程 ID
USER 进程的用户拥有者
PR 进程优先级
NI 进程的 Nice 值
VIRT 进程使用的虚拟内存
RES 进程使用的常驻内存
SHR 进程使用的共享内存
S 进程状态
%``CPU 自上次更新以来,进程使用的 CPU 时间百分比
%``MEM 使用的物理 内存 的百分比
TIME+ 任务使用的总 CPU 时间,以百分之一秒为单位
COMMAND 启动进程的命令及其所有参数

表 11.2 – top 命令输出的逐列说明

注意

有关进程状态代码的更多信息,请参阅 top 手册页面。使用 man top 命令进行查看。

关于 memoryfree 命令列出了空闲和已用的 物理 内存和 交换 内存。通过使用 -b-k-m-g 选项,输出将分别以字节、KB、MB 和 GB 显示:

# free

图 11.9 – 运行 free 命令的不同选项后的输出

图 11.9 – 运行 free 命令的不同选项后的输出

GNOME 桌面提供了一个图形工具用于资源监控。在主菜单中,选择 系统监视器

图 11.10 – 来自活动概览的 GNOME 系统监视器

图 11.10 – 来自活动概览的 GNOME 系统监视器

你也可以通过运行 gnome-system-monitor 命令从终端打开该工具:

$ gnome-system-monitor

图 11.11 – GNOME 系统监视器

图 11.11 – GNOME 系统监视器

Fedora Linux 通过其官方仓库提供一个集成多种监控工具的软件包。sysstat 软件包从内核计数器中获取 原始数据,并允许你显示和存储度量数据到历史 进程 运行 数据库中。

以下工具包含在该软件包中:

  • mpstat:报告单个或合并的 CPU 相关统计信息

  • iostat:报告设备、分区和网络文件系统的CPU和 I/O 统计信息

  • pidstat:报告进程的统计信息,包括磁盘 I/OCPU内存 使用情况

  • cifsiostat:报告共享文件系统、打印机或网络串行端口的统计信息

  • sar:收集、报告并存储系统活动信息

要安装该软件包,请从终端运行以下命令:

# dnf install sysstat

所有这些工具都用于测量和存储系统资源使用信息。通过这些工具,我们可以确定资源的哪些方面存在改进空间。

让我们从 CPU 使用开始。

改善 CPU 使用情况

要找出 CPU 使用的改进点,首先要观察 CPU 的行为。让我们学习如何使用不同的监控工具检查 CPU 使用情况。请按照以下步骤操作:

  1. 使用 ps 命令列出 CPU 使用率最高的进程:

    # ps ax --format pid,%cpu,cmd --sort -%cpu
    

图 11.12 – ps 命令输出列出最高 CPU 使用率的进程

图 11.12 – ps 命令输出列出最高 CPU 使用率的进程

ps ax 命令的此模式通过进程标识符(pid)、CPU 使用百分比和启动该进程的命令格式化输出,按 CPU 使用百分比进行排序。

输出显示,CPU 使用率最高的进程是 SSH,这对于管理任务来说应该是正常的。

在我们的系统中,正在运行一个 Web 服务器。让我们分析它的 CPU 使用情况。

  1. 使用 ps 命令识别 httpd 进程及其 CPU 使用情况:

    # ps auxf | grep "[h]ttpd"
    # ps ax --format pid,%cpu,cmd --sort -%cpu | grep "[h]ttpd"
    

图 11.13 – 识别 httpd 进程及其 CPU 使用情况

图 11.13 – 识别 httpd 进程及其 CPU 使用情况

注意

将进程的第一个字符括在方括号中,可以限制搜索范围,忽略输出中的 grep 命令本身。

请注意,显示的 CPU 使用情况并不显著,因此需要进一步研究,找到 CPU 使用的真实值。

  1. 使用进程标识符(pid)通过 top 命令查找资源使用情况:

    # top -p 851,876,881,910
    

图 11.14 – 使用 top 命令监控资源使用情况

图 11.14 – 使用 top 命令监控资源使用情况

现在,让我们使用 sysstat 软件包中包含的工具,获取更多有关进程性能的信息。

  1. 使用带有进程标识符(pid)的 pidstat 命令,在 1 秒间隔 下获取进程的 CPU 使用情况:

    # pidstat -p 851,876,881,910 1 1
    

图 11.15 – 进程 CPU 使用百分比

图 11.15 – 进程 CPU 使用百分比

单个 CPU 一次只能运行 一个进程。为了让 Linux 系统同时运行多个进程,通过多任务处理,进程在 CPU 上交替运行。

内核使用 进程调度器 来决定在任何给定时刻应该运行哪个进程。进程调度器必须在多个选项之间进行平衡,基于某些标准,如决定哪个进程获得下一个执行机会,确保公平的 CPU 时间分配,同时允许高优先级进程获得更多的时间片,并抢占低优先级进程,或者在不同工作负载条件下具有可预测性和可扩展性。

调度器根据分配给每个线程或进程的调度优先级策略控制执行顺序。这些调度策略分为两组:非实时策略实时策略

注意

实时计算 (RTC) 保证系统从事件到响应的时间满足规定的时间约束。实时系统 描述了一种控制环境的系统。它接收数据,处理数据,并迅速返回结果,足以在该时刻影响环境。

使用实时策略的进程优先级值在 1 (最低) 和 99 (最高) 之间,并且被称为 0

进程的优先级在生命周期内可能会 增加减少;这就是 nicerenice 命令的作用。

由于非实时进程的静态优先级被设置为 0,因此 nice 值决定了非实时进程的 相对调度。nice 值的范围从 -20 (最高) 到 19 (最低)。

Fedora Linux 提供了六种调度策略,分为两组:实时非实时 调度类。以下表格列出了每个调度类中可用的策略:

类别 策略 定义
实时调度器 SCHED_FIFO 使用 先到先服务 调度算法,无时间片。
SCHED_RR 使用 轮转调度 算法并带有时间片。
完全公平调度器 (CFS) SCHED_NORMAL (也称为 SCHED_OTHER) 定义了 轮转调度 样式的时间共享计划。
SCHED_BATCH 适用于 批处理型 工作负载。
SCHED_IDLE 适用于运行低优先级的应用程序。
截止时间调度器 SCHED_DEADLINE 调度器保证在高负载条件下也能进行实时任务调度。它通过使用三个参数——perioddeadlineruntime——以纳秒为单位定义任务。

表 11.3 – 调度策略

继续使用之前的 Web 服务器示例来分析其进程的优先级。按照以下步骤进行:

  1. 使用 ps 命令列出进程的优先级:

    # ps axo pid,pri,rtprio,ni,cls,comm | grep -e "PID" -e "[h]ttpd"
    

图 11.16 – 查看进程优先级

图 11.16 – 查看进程优先级

在这里,pri列显示19rtprio列显示实时优先级(-),ni列显示0cls列显示TS表示时间共享

以上输出表明该进程的运行优先级较低。让我们更改它。

  1. 使用chrt命令显示 web 服务器进程的调度策略和优先级:

    # chrt -p [PID]
    

图 11.17 – 调度 web 服务器进程的策略和优先级

图 11.17 – 调度 web 服务器进程的策略和优先级

所有进程的调度优先级为0,其调度策略为SCHED_NORMAL(或SCHED_OTHER)。让我们优化进程运行。

  1. 停止 web 服务器服务,并通过将调度策略更改为SCHED_FIFO,调度优先级设置为38来重新启动它。使用chrt命令和进程二进制文件:

    # systemctl stop httpd
    # chrt -f 38 /usr/sbin/httpd
    

图 11.18 – 更改调度策略和优先级

图 11.18 – 更改调度策略和优先级

审查 web 服务器进程的调度策略和优先级。

注意

要了解更多有关chrt命令选项的信息,请通过运行man chrt命令参考手册页。

该更改是临时的。当进程重新启动时,调度策略和调度优先级应该再次生效。

要为每次启动时改进进程的性能设置调度策略和优先级,必须将其添加到服务的unit文件中。

让我们开始吧。

  1. httpd服务创建一个调度配置文件,内容如下:

    # vim /etc/systemd/system/httpd.service.d/10-scheduler.conf
    [Service]
    CPUSchedulingPolicy=rr
    CPUSchedulingPriority=10
    

图 11.19 – 创建服务配置文件

图 11.19 – 创建服务配置文件

单元配置策略设置为在启动过程中更改服务的优先级。[Service]部分的指令更改调度策略和优先级:

  • CPUSchedulingPriorityNice):设置服务的默认nice级别。nice级别设定为-20最高优先级)和19最低优先级)之间的数字。

  • CPUSchedulingPolicy:为服务设置CPU调度策略。策略的值可以是otherbatchidlefiforr

注意

在撰写本文时,CPUSchedulingPolicy变量不支持SCHED_DEADLINE策略设置。

在重新启动 web 服务器服务之前,重新加载由systemd加载的服务配置。使用systemctl命令:

# systemctl daemon-reload

重新启动服务以应用策略和优先级更改。

  1. 使用systemctl命令重新启动 web 服务器服务:

    # systemctl restart httpd
    

图 11.20 – 重新启动 web 服务器服务

图 11.20 – 重新启动 web 服务器服务

审查 web 服务器进程的调度策略和优先级。

优先级和调度策略的更改应在服务启动时生效。此更改可提高服务的性能。

我们在这里讲解的内容适用于处理的情况。接下来,让我们学习如何优化内存使用。

提升内存使用效率

pstop 工具区分了两个统计值:VIRT(或 VSZ),即进程请求的虚拟内存总量;以及 RES(或 RSS),即进程当前映射到物理内存中的虚拟内存总量。RSS 是最关键的值。

使用前面的示例,使用 ps 命令查看映射到 Web 服务器进程的虚拟内存值:

# ps -o pid,vsz,rss,comm -C httpd

图 11.21 – 审查映射到 Web 服务器的虚拟内存

图 11.21 – 审查映射到 Web 服务器的虚拟内存

当进程请求内存时,虚拟内存地址会被保留,但内核并不会将其分配给物理页面框架。内核只有在进程开始使用 memory 时,才会将物理页面框架分配给它。

与处理的优先级和调度策略改变类似,应用程序所消耗的内存也可能发生变化。

按照此步骤调整 sshd 进程的 MemoryLimit 参数:

  1. 使用 sysctl 命令,调整 MemoryLimit 参数:

    # systemctl set-property sshd.service MemoryLimit=1G
    

注意

内存大小可以使用 KMGT 后缀分别表示千字节、兆字节、吉字节或太字节。

此命令修改服务的启动配置文件。

使用 sysctl 命令显示服务单元文件。过滤 Memory 参数:

图 11.22 – 审查 MemoryLimit 参数

图 11.22 – 审查 MemoryLimit 参数

图 11.22 中,我们发现内核为该服务分配了 10 MB 内存,并通过设置 MemoryLimit 参数,确保它只使用最多 1 GB 的内存。

控制分配给服务的资源使它们能够提高自身性能,并间接提升操作系统的性能。

在 Linux 中,管理未分配的内存是最需要密切监控的任务。内核将大部分未分配的内存作为缓存,用来存储从磁盘读取或写入的数据。下次当进程需要这些数据时,系统会从 RAM 中获取,而不是从磁盘中读取。缓存机制通常能提高性能,因为存储通常比物理内存慢得多。

除了缓存页面外,系统还使用匿名页面。匿名页面在磁盘上没有关联的数据。这些匿名页面代表了进程分配并用来存储工作数据的页面。

我们有不同的工具和实用程序帮助我们监控两者。

让我们简要概述一下这些工具和实用程序。

使用 freevmstat 命令检查内存使用情况:

# free –m

图 11.23 – free 命令输出

图 11.23 – free 命令输出

在该系统中,有 8 GiB 的 RAM。缓存页面(buff/cache 列)消耗了 1 GiB。

让我们将其与 vmstat 命令的输出进行比较:

# vmstat --unit M 1

图 11.24 – vmstat 命令的输出

图 11.24 – vmstat 命令的输出

vmstat 命令的输出中注意到,页面缓存的大小是 buffcache 列的总和。缓冲区是页面缓存的一部分。缓冲区存储的是直接从块设备读取的数据块,而不是从文件系统读取的数据。

匿名页在系统内存紧张时会被移到交换区。内核在恢复匿名页或页面缓存中的页面之间做出选择。交换空间的可用性增加了有效内存的数量。

vmstat 命令的输出(图 11.24)中,在 swap 部分,我们可以看到匿名页的值:

  • si: 每秒交换 进出 页数

  • so: 每秒交换 移出 页数

交换区 是多年来讨论的话题。计算交换区的方式在计算机时代初期,曾是当时系统管理员之间的一个神话。这些方法不仅被视为良好的实践,还是创建交换区时的必备项。这是因为,在那个时期,物理存储非常昂贵。

如今,随着新技术的发展,存储变得更加便宜和可承受。因此,创建交换区被视为根据使用案例标准来考虑。

Red Hat 提出了以下表格,作为在创建交换区时进行大小调整的使用案例基础:

RAM 交换空间
2 GiB 或更少 两倍 的 RAM
介于 2 GiB 和 8 GiB 之间 等于 RAM
介于 8 GiB 和 64 GiB 之间 至少 4 GiB
超过 64 GiB 至少 4 GiB

表 11.4 – 交换区大小的基本指导

请记住,当进程请求内存时,内核仅保留虚拟内存,并不消耗任何 RAM。由于内核只有在进程启动时才分配物理页面帧,这允许应用程序分配比系统实际可用内存更多的内存。这一功能被称为内存超分配。

系统的内存超分配策略可以通过 sysctl 变量 vm.overcommit_memory 调整。你可以将其设置为以下值之一:

  • 0: 内核使用 启发式超分配算法。这是系统的默认配置。

  • 1: 内核 始终 超分配内存。无论是否有足够的空闲内存,它总是会授予内存分配。

  • 2: 内核严格控制内存超分配。它仅分配等于 交换空间加上物理内存百分比 的内存(默认是 50%)。这个百分比由 vm.overcommit_ratio 变量定义。

使用 sysctl 命令验证这两个变量:

# sysctl vm.overcommit_memory
# sysctl vm.overcommit_ratio

图 11.25 – 查看内存超分配变量

图 11.25 – 查看内存超分配变量

监控这些内存的基本方面有助于提高系统及其提供的服务的性能。

让我们看看如何对存储进行相同的操作。

不要忽视存储调优。

与其他系统资源不同,存储可以在确定其大小后优化性能。当分析其使用情况时,也可以进行调优。

为此,必须考虑正确的大小调整取决于区分存储分配过程中使用的度量单位。

一个非常常见的错误是使用错误的存储度量进行大小调整,导致存储空间未被充分利用或浪费。

关键点在于,硬盘制造商如何克服创建一个存在于两个世界中的设备的挑战。磁盘在创建文件系统之前是一个原始的物理设备,没有虚拟或数据结构。一个原始磁盘在格式化为文件系统后变成一个块设备结构。文件系统是二进制结构。

这意味着我们必须创建一个物理设备,按照现实世界的物理规则创建,并将其转换为数字世界中的数据结构。

-kilo-,并添加字母- bi - 代表 binary

错误点发生在值被混淆时。请注意以下对比表中的差异:

SI 十进制前缀
前缀
kilo-
mega-
giga-
tera-
IEC 二进制前缀
kibi-
mebi-
gibi-
tebi-

表 11.5 – 前缀比较

那么,1 TB 磁盘上有多少字节?

1 TB 等于 1 x 1,012 字节 = 1,000,000,000,000。

如前所述,要在磁盘上创建文件系统,二进制结构使用默认的块大小为 4,096(4 KiB)。使用这种命名法,1 TB 等于 1,012 字节。

因此,一些操作系统工具可能报告为 TB,但实际上它们是以二进制的 tebibytes (TiB) 为单位进行度量的。

一个报告磁盘大小为 TB 的工具,但将 TB 作为 tebibytes 来度量,会报告一个 1 TB 的磁盘只有 0.91 TB 大(即 0.91 TiB),差异接近 10%。

因此,明确说明在系统报告中使用哪些单位来分析大小非常重要,否则分析中可能会发生重大错误。

现在,让我们学习如何分析存储空间使用情况。

改善存储空间使用

分析存储空间使用时的一个大问题是如何正确识别它。映射到文件系统的稳定标识符是其 UUID。这是一个十六进制数字,作为 通用唯一标识符

UUID 是文件系统的一部分,只要文件系统没有重新生成,它就会保持不变。

列出块设备的工具是 lsblk 命令。

带有-fp选项的lsblk命令显示设备的完整路径,以及 UUID 和挂载点,还显示物理磁盘分区的文件系统类型:

# lsblk -fp

图 11.26 – lsblk 命令的输出

图 11.26 – lsblk 命令的输出

注意

如果文件系统未挂载,则挂载点会显示为空白。

获取文件系统总使用情况信息时最常用的工具是df命令。使用-h选项时,输出会以易于阅读的格式显示:

$ df –h

图 11.27 – df -h 命令的输出

图 11.27 – df -h 命令的输出

通过安装之前提到的sysstat包,我们可以获得用于监控系统性能的工具。在存储方面,iostat命令显示每个磁盘的 I/O 统计信息,以及工作负载、使用率和饱和度指标。

运行没有参数的iostat命令将返回 CPU 使用情况和磁盘 I/O 指标:

# iostat

图 11.28 – iostat 命令的输出

图 11.28 – iostat 命令的输出

通过结合使用iostat命令的不同选项,我们可以生成非常有用的使用统计信息。以下是一个示例:

# iostat -dyz  1 3

图 11.29 – iostat -dyz 命令的输出

图 11.29 – iostat -dyz 命令的输出

让我们看看使用了哪些选项:

  • -d显示磁盘 I/O 使用报告

  • -y跳过自系统启动以来的第一次统计报告

  • -z跳过非活动设备

  • 间隔是 1 秒

  • 计数是每个间隔输出的三次报告

注意

关于iostat命令选项的更多信息,请参阅手册页。您可以通过运行man iostat命令来查找它们。

如前一节所述,拥有交换空间可以增加有效内存量。让我们学习如何在系统中创建有效大小的交换空间。

我们将使用在第四章中使用的/dev/sdc磁盘来使用 Stratis 创建文件系统。请按照上述章节中的说明,删除池并清除磁盘上的文件系统。

然后,按照以下步骤操作:

  1. 使用wipefs命令:

    # wipefs /dev/sdc
    

注意

此命令会销毁设备的文件系统信息。请务必小心,并验证您希望擦除的设备是否正确。

现在,让我们为交换区创建分区。

  1. 检查/dev/sdc磁盘,以确认没有创建任何分区。使用parted命令:

    # parted /dev/sdc print
    

图 11.30 – 列出/dev/sdc 磁盘分区

图 11.30 – 列出/dev/sdc 磁盘分区

创建一个 512 MB 的分区。

  1. 使用parted命令:

    # parted /dev/sdc
    

图 11.31 – 创建交换分区

图 11.31 – 创建交换分区

使用没有子命令的parted命令将打开一个交互式的parted会话。

在交互式会话中,执行以下操作:

  1. 运行mkpart子命令来创建分区。

  2. 将分区名称设置为swap1

  3. 将文件系统类型设置为linux-swap

  4. 分区应该从 2,049 KB 开始,结束于 513 MB,这样大小为 512 MB。

注意

重要的是,分区要从底层存储的第一个块开始。以扇区2048开始是现代 Linux 的默认值,并且在所有存储类型上都存储此值。这被称为初始偏移或分区对齐。

通过运行print子命令来验证分区是否已创建。

输入quit以退出交互式parted会话。

注意

parted命令也可以以二进制方式显示输出。

运行# parted /dev/sdc unit MiB print命令:

图 11.32 – 在二进制基础上显示的命令输出

图 11.32 – 在二进制基础上显示的parted命令输出

请求系统检测新分区并在/dev目录中创建相关的设备文件。

  1. 运行udevadm settle命令以观察udev事件队列并更新它:

    # udevadm settle
    

    现在,格式化分区。

  2. 使用mkswap命令:

    # mkswap /dev/sdc1
    

图 11.33 – 格式化分区

图 11.33 – 格式化分区

查看系统的交换区域。

  1. 使用free命令:

    # free -m
    

图 11.34 – 查看交换内存

图 11.34 – 查看交换内存

在系统中激活交换分区。

  1. 使用swapon命令:

    # swapon /dev/sdc1
    

图 11.35 – 激活交换分区

图 11.35 – 激活交换分区

现在,验证系统的交换区域。

新的交换区域分区已经激活并可供系统使用。为了使其持续激活,我们需要在/etc/fstab文件中创建一个条目。

  1. 识别交换分区的 UUID 并在/etc/fstab文件中创建条目。使用lsblk命令执行此操作:

    # lsblk -fp
    

图 11.36 – 识别交换分区的 UUID

图 11.36 – 识别交换分区的 UUID

将以下行添加到/etc/fstab文件中:

UUID=6f53144d-fe12-4fc3-bb69-6469012592dc  swap  swap  defaults 0 0

图 11.37 – 将交换分区添加到文件

图 11.37 – 将交换分区添加到/etc/fstab文件

通过这种方式,交换分区可以持续可用。这样,系统性能得以提高,因为物理内存变得可用。在此阶段,存储空间已优化为交换空间。

最后,我们来学习如何改善网络连接到系统的性能。

通过网络调优提升性能

网络是调整系统时涉及的最复杂和关键的元素之一。作为外部自组织的元素,一些网络元素可能会影响性能。为了识别并调整它们,需要了解数据包在系统发送和接收时的流向,以及可用于它们的调优选项。

数据包的传输和接收流程大致如下:

  • 传输

    1. 数据会被写入套接字(如文件等对象)并发送到传输缓冲区。

    2. 内核将数据封装为 协议数据单元PDU)。

    3. PDU 会进入设备的传输队列。

    4. 网络设备驱动程序将 PDU 从传输队列头部复制到 NIC。

    5. NIC 在传输时发送数据并触发中断。

  • 接收

    1. NIC 接收一个帧,并使用 DMA 将该帧复制到接收缓冲区。

    2. NIC 触发一个硬中断。

    3. 内核处理硬中断,并调度软中断来处理数据包。

    4. 软中断处理并将数据包移至 IP 层。

    5. 如果数据包是为了本地传输,则 PDU 会被解封装并放入套接字接收缓冲区:

    6. 如果进程在此套接字中等待,它会处理接收缓冲区中的数据。

针对提高网络流量的调优参数包括缓冲区限制。内核会根据当前的网络使用情况调整这些缓冲区的大小,但会在内核调优参数规定的范围内。变量的默认值会在启动时根据可用内存的数量进行计算。

sysctl 命令执行时可能会改变的内核调优参数如下:

  • net.ipv4.tcp_mem:此项指定 TCP 系统内存限制。它包含三个字段:minmaxpressure(当 TCP/UDP 中使用的内存量超过此值时,页面会被调节,直到它们降回并恢复到 min 值)。这些值表示页面的数量,而非字节。

  • Net.ipv4.udp_mem:此项指定 UDP 系统内存限制。它包含三个字段:minmaxpressure。这些值表示页面的数量,而非字节。

  • net.core.rmem_max:最大网络核心套接字接收/发送(读/写)缓冲区。值以字节为单位。

  • net.core.wmem_max:最大网络核心套接字接收/发送(读/写)缓冲区。值以字节为单位。

  • net.ipv4.tcp_rmem:最大 TCP 套接字接收/发送(读/写)缓冲区。它包含三个字段:mindefaultmax。值以字节为单位。

  • net.ipv4.tcp_wmem:最大 TCP 套接字接收/发送(读/写)缓冲区。它包含三个字段:mindefaultmax。值以字节为单位。

注意

这些值的变化应在进行行为分析后进行。

通常,在分析完系统行为后,才应更改调优值。

为此目的,存在一些工具和实用程序,我们在本章中已使用它们。

现在,让我们看看一个与 sysstat 配合使用并提供收集指标的图形显示的工具。

分析指标

除了安装 sysstat 包以监控工具外,它也可以作为服务运行:

# systemctl enable --now sysstat

图 11.38 – 查看 sysstat 服务的状态

图 11.38 – 查看 sysstat 服务的状态

sysstat 服务收集、报告或保存有关系统活动的信息。它将操作系统计数器存储在 /var/log/sa/sadd 文件中。从收集到的数据中,可以获得大量关于系统的信息:

  • CPU 使用情况

  • 内存分页和使用情况

  • 网络 I/O 和传输统计

  • 进程创建活动

  • 所有块设备的活动

  • 每秒中断数

我们可以使用 sar 命令获取收集的数据:

  • 使用 sar 命令和 -u 选项来获取 CPU 使用报告:

    # sar –u 1 3
    

图 11.39 – sar –u 命令的输出

图 11.39 – sar –u 命令的输出

此命令也用于读取记录日常数据的历史文件。

  • 使用 sar 命令读取 /var/log/sa 中的日志文件之一:

    # sar –q –f /var/log/sa17
    

图 11.40 – 日志文件的内容

图 11.40 – 日志文件的内容

注意

要了解 sar 命令选项的更多信息,请运行 man sar 命令查看手册页。

从这些文件中,可以生成图表并将其导出为图片或甚至 PDF 报告。最常用的工具是 ksar

要获得 ksar,请从他们的网站下载:sourceforge.net/projects/ksar/

图 11.41 – ksar 网站

图 11.41 – ksar 网站

注意

可以从其 GitHub 仓库下载 ksar 工具的更新版本:github.com/vlsi/ksar/releases

ksar 工具作为 jar 文件提供,因此需要安装 java-17-openjdk 包:

# log files to analyze, either by period or by a known event. A copy of the file can be used to generate the performance graphs. Follow these steps:

1.  Use the **sar** command to redirect the contents of the log files to a text file:

    ```

    # LC_ALL=C sar -A > /tmp/sar.data.txt

    ```

     2.  Use the **java** command to open the **ksar** tool:

    ```

    $ java -jar kSar.jar

    ```

![Figure 11.42 – The ksar tool’s main window](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_11_42.jpg)

Figure 11.42 – The ksar tool’s main window
Load the `data` file created from the `log` files to generate the system performance graphs.

1.  From the **Data** menu, click on **Load from a** **text file…**:

![Figure 11.43 – The Load from a text file… option](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_11_43.jpg)

Figure 11.43 – The Load from a text file… option
The file selection window will open.

1.  Navigate to the directory where the generated file resides:

![Figure 11.44 – Selecting the sar file](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_11_44.jpg)

Figure 11.44 – Selecting the sar file
Select the `sar` file and click **Open**.
The file will take some time to load, depending on its size and the amount of data to analyze.

1.  After loading finishes, the window will display the data column for filtering by resource:

![Figure 11.45 – Data analyzed window](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_11_45.jpg)

Figure 11.45 – Data analyzed window
Navigate to **NIC** to review the network behavior.

1.  Click **Interfaces traffic** and then **eth0**:

![Figure 11.46 – NIC performance graph](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_11_46.jpg)

Figure 11.46 – NIC performance graph
Note
On your system, the device may be different. Please confirm the device information that we will analyze.
This way, the behavior could be better visualized.
Let’s export these graphs.

1.  Click on the **Export** menu and then **Export** **to PDF…**:

![Figure 11.47 – The Export menu](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_11_47.jpg)

Figure 11.47 – The Export menu
The window for selecting the data to export will open.

1.  Select the data to export and click the **OK** button:

![Figure 11.48 – Selecting data to export](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/B19121_11_48.jpg)

Figure 11.48 – Selecting data to export
Name the file, as is best practice, with the system’s name, and save it in the directory of your choice.
Note
You can get the export report from our GitHub repository at [`github.com/PacktPublishing/Fedora-Linux-System-Administration/blob/main/chapter11/workstation.packt.lab.pdf`](https://github.com/PacktPublishing/Fedora-Linux-System-Administration/blob/main/chapter11/workstation.packt.lab.pdf).
This concludes the best practices for improving system performance. In the next chapter, we will talk about the security provided by Fedora Linux.
Summary
In this chapter, we provided an overview of best practices in system tuning. Beyond configurations, we looked at the tools that are available in Fedora Linux to analyze system behavior.
System tuning, in most cases, is performed after a failure incident. It is at this point that it gets confused with troubleshooting. System tuning is about more than resolving a failure – it is about collecting metrics and performing analysis using the right tools.
First, we learned how to change the kernel tunables using the `sysctl` command to understand their origin and the scope of these changes in their original configuration.
Then, we learned how to use the system tools and utilities, including the `ps`, `top`, and `free` commands. We also covered the suite of utilities provided by installing the `sysstat` package, including the `mpstat`, `iostat`, `pidstat`, and `sar` commands.
After that, we briefly reviewed the *units of measurement*, which cause failures when sizing resources, particularly in storage.
Finally, we touched on network tuning and discussed the limitations of the operating system. While doing so, we learned how to use `ksar`, a tool that helps us graph the metrics collected by `sysstat` when it’s running as a service.
In the next chapter, we will take a walk through the hardened security layer offered by Fedora Linux: SELinux. In most cases, this is not activated due to ignorance of how it works. We will clarify how it operates so that we can implement it in our systems.

第十四章:用 SELinux 解开安全难题

安全性加固合规性策略:这四者是系统管理员末日审判中的四骑士。一个重要的战斗是维护系统的安全性。除了日常任务,系统的安全性也是工作的一部分,即使是那些第三方提供商要求我们停用增强安全性的系统。在大多数情况下,通过分析就足以找到正确的故障排除方法或解决方法。

增强安全 LinuxSELinux)是一种运行在用户空间的代码,通过利用内核代码(Linux 安全模块)提供对系统资源的强制访问控制MAC)。对系统对象和特性进行的访问基于每个域,并遵循最小特权原则。

在本章中,我们将学习如何利用 SELinux 的优势来保护管理系统,主要涵盖以下内容:

  • 学习强制访问控制

  • 标签和类型强制

  • 如何故障排除 SELinux 问题

技术要求

为了发展本章的主题,需要安装每个部分中所指定的包。在每个部分,你会找到关于如何安装每种包的不同类型的说明。

学习强制访问控制

Linux 文件权限控制哪些用户或用户组可以访问特定的文件。但是,一个具有读或写访问权限的用户可以以任何方式使用该文件,即使这种使用方式并不是该文件应该被使用的方式。

Linux 的标准文件权限在文件由单一所有者和单一指定用户组使用时成功。

文件权限未能防止某些形式的不必要访问。它们的设计初衷并不是控制文件的使用方式,而是控制谁可以读取、写入或执行文件。

Linux 标准文件权限是基于所有权的,也称为自由访问控制DAC)。

MAC 规则是基于策略,而非所有权。

MAC 有两种类型:

  • 多级安全系统:MAC 的原始和最简单形式由垂直的保护和安全级别结构组成。信息仅在该区域内流动。每个用户也被分配一个保护级别,这样他们只能访问相同或较低级别的资源。

  • 多边安全系统:这些系统更加复杂,并根据不同的区段分配访问权限。这些区段形成关联,进而由保护级别和密码组成。这形成了一个水平的安全系统,同时也包括垂直的保护级别。

SELinux 是 Linux 上 MAC 应用的一个例子。

SELinux 提供了一种基于 MAC 原则的特殊安全架构。SELinux 通过严格的访问控制方法和相应的安全措施来 最小化访问 操作系统进程和文件。该模块旨在确保数据的机密性和完整性。此外,通过 SELinux,操作系统和用户程序保持相互隔离。

SELinux 还依赖于另外两个实现:类型强制TE)和 基于角色的访问控制RBAC)。通过 RBAC,访问权限根据定义的角色模型映射。定义的用户角色抽象了组织的工作流程。在 MAC 模型中,TE 是通过基于 主体-访问-对象集规则 来管理访问的概念。

SELinux 定义了安全措施,并设置了额外的属性,说明在什么条件和什么情况下,权利持有者可以访问某些操作系统进程或文件。如果这些条件或关系(即属性)没有得到满足,访问将被拒绝。

SELinux 由一组策略组成,声明了每个应用程序使用的对象所允许的 操作和访问。它也被称为 定向策略,因为该策略仅涵盖单个应用程序的活动。策略声明了适用于单个程序、文件和网络端口的预定义标签。

SELinux 强制执行一套访问规则,防止一个应用程序中的安全漏洞影响到其他应用程序或系统本身。SELinux 提供了额外的安全层,但它也增加了一个复杂度,可能让那些不熟悉的人感到困惑。

为了更好地理解它是如何工作的,我们来了解 SELinux 的基本概念。

标签和类型强制

SELinux 是一个 标签系统,这意味着系统中每个文件、目录或对象都有一个相应的标签。每个文件、进程、目录和端口都有一个 特殊的安全标签,称为 SELinux 上下文。上下文是 SELinux 策略使用的标签名称,用来判断一个进程是否可以访问文件、目录或端口。策略控制这些元素之间的交互。默认情况下,策略不允许任何交互,除非显式的规则授权访问。如果没有权限规则,访问将被拒绝。Linux 内核强制执行这些规则。

SELinux 上下文的格式中有不同的标签,以冒号分隔:用户角色类型敏感级别。它们的构成方式如下:

user:role:type:level (optional)

注意

敏感级别是可选部分。

定向策略基于第三个上下文组件:类型上下文。类型上下文名称通常以 _t 结尾。

让我们来看以下示例:

图 12.1 – SELinux 标签示例

图 12.1 – SELinux 标签示例

在前面的示例中,/var/www/html/file 文件具有以下上下文组件:unconfined_u 是 SELinux 用户,角色是 object_r,类型是 httpd_sys_content_t,敏感级别是 s0。因此,访问该文件的上下文依赖于 httpd_sys_content_t

我们通过一个示范例子来学习 SELinux 策略是如何工作的。

SELinux 的工作原理

在分析 SELinux 的运行情况之前,首先要确定 SELinux 在系统上的运行模式。

SELinux 有三种模式:

  • 强制模式:SELinux 强制执行访问控制规则。这是 Fedora Linux 默认启用的模式。

  • 宽容模式:SELinux 虽然启用,但不会强制执行访问控制规则,而是记录违反规则的警告。此模式用于测试和故障排除。

  • 禁用:SELinux 完全禁用;没有任何 SELinux 违规行为会被拒绝或记录。在任何情况下都不推荐使用此模式

要了解 SELinux 的运行状态,可以使用 sestatus 命令:

$ sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Memory protection checking:     actual (secure)
Max kernel policy version:      33

SELinux 运行模式在 /etc/selinux/config 文件中配置:

图 12.2 – SELinux 配置文件

图 12.2 – SELinux 配置文件

在执行过程中,测试时 SELinux 模式可能会发生变化。但在重启系统后,它将再次使用配置文件中设置的模式。使用 getenforce 命令查看 SELinux 正在运行的模式,并使用 setenforce 命令更改模式,前提是以 root 用户身份执行:

图 12.3 – 更改 SELinux 运行模式

图 12.3 – 更改 SELinux 运行模式

现在,让我们来看一下 SELinux 是如何工作的。

那么,SELinux 是如何工作的呢?

Linux 在操作系统早期阶段诞生。它作为运行 web 服务器的主要操作系统。

Apache web 服务器(httpd)本身并不不安全,但其访问范围非常广泛,因此非常重要的是要对其进行安全保护。

接下来,我们通过这个示例来理解 SELinux 是如何工作的。按照以下步骤操作:

  1. 验证 httpd 服务是否已安装并在系统中处于活动状态:

图 12.4 – httpd 服务状态

图 12.4 – httpd 服务状态

  1. 如果没有,使用 dnfsystemctl 命令来执行此操作:

    # dnf install httpd
    # httpd service runs a binary file to start. This file launches from /usr/sbin/.
    
  2. Linux 内核在大多数命令中集成了一个选项,可以查看 SELinux 上下文类型。使用 ls 命令和 -Z(或 –context)选项来识别二进制文件的 SELinux 上下文类型:

    # ls -Z /usr/sbin/httpd
    system_u:object_r:httpd_exec_t.
    
  3. httpd 服务的配置文件位于 /etc/httpd。我们来看一下它们的上下文类型:

    # ls -dZ /etc/httpd/
    system_u:object_r:httpd_config_t.
    
  4. httpd 服务的日志位于 /var/log/httpd。使用以下命令查找其上下文类型:

    # ls -dZ /var/log/httpd
    system_u:object_r:httpd_log_t.
    
  5. httpd 服务的内容类型目录位于 /var/www/html。使用以下命令查找它们的上下文:

    # ls -dZ /var/www/html
    system_u:object_r:httpd_sys_content_t.
    
  6. httpd 服务启动的单元文件位于 /usr/lib/systemd/system/。使用以下命令查找其上下文类型:

    # ls -Z /usr/lib/systemd/system/httpd.service
    system_u:object_r:httpd_unit_file_t.
    
  7. 使用ps命令来查找正在运行的httpd服务守护进程的上下文类型:

    # ps auxfZ | grep httpd
    

图 12.5 – 查找 httpd 守护进程的上下文

图 12.5 – 查找 httpd 守护进程的上下文

服务守护进程的上下文类型是httpd_t

  1. 使用netstat命令来查找正在运行的httpd服务端口的上下文类型:

    # netstat -tulpnZ | grep httpd
    

图 12.6 – 查找 httpd 端口的上下文

图 12.6 – 查找 httpd 端口的上下文

服务端口的上下文类型是httpd_t

上述示例可以总结为以下表格:

类型 上下文类型
二进制文件 httpd_exec_t
配置文件 httpd_config_t
日志 httpd_log_t
内容目录 httpd_sys_content_t
单元文件 httpd_unit_file_t
进程 httpd_t
端口 httpd_thttp_port_t

表 12.1 – httpd 服务上下文

注意上下文的关系;所有都属于httpd_t域。

因此,类型强制是一个概念,按照这个概念,在httpd_t上下文中运行的进程与标记为httpd_sys_content_t的文件进行交互是有意义的。

让我们来看看/etc/shadow文件的上下文,用户密码存储的位置:

# ls -Z /etc/shadow
system_u:object_r:/etc/shadow file is shadow_t.
From a basic functional perspective, the web server (`httpd`) *reads and publishes* documents that live within it. Besides the file’s proprietary permissions, security could enhance this through the SELinux policy. With proper proprietary permissions, there would be nothing to prevent the `httpd` service from publishing the file with user passwords.
With SELinux policies enabled, no matter what permission level the file has, the policy would *prevent* it from doing so.
Files with the `httpd_t` context type can only interact with files *under the same context type*. The password file belongs to a different context type than `httpd_t`; its context is `shadow_t`, thus their *interaction* *is denied*.
This is the most basic way SELinux works, and the way the policy works. It is not free of issues, but these are also limited by its nature.
Let’s see how to determine SELinux errors.
How to troubleshoot SELinux issues
The popular belief is that it is very difficult to determine and fix SELinux issues. This stems from the fact that the *logs* get logged with the system audit. This log reading is not intuitive and, in fact, to the human eye is quite complex; but it isn’t.
Carrying on with the example of the `httpd` web server, consider the following sequence of commands:

1.  As the **root** user, create the **myfile** file:

    ```

    # touch myfile

    ```

     2.  Move the **myfile** file to the *web* *content directory*:

    ```

    # mv myfile /var/www/html/

    ```

     3.  Use the **curl** command to get the contents of the **myfile** file published by the web server:

    ```

    # curl http://localhost/myfile

    ```

     4.  Observe the output:

![Figure 12.7 – Creating the myfile web file](https://github.com/OpenDocCN/freelearn-linux-pt3-zh/raw/master/docs/fdr-linux-sys-adm/img/12.7.jpg)

Figure 12.7 – Creating the myfile web file

1.  Use the **grep** command to search for the **myfile** string in the *system* *audit log*:

    ```

    # grep myfile /var/log/audit/audit.log

    ...

    type=AVC msg=audit(1689045662.823:264): avc:  denied  { getattr } for  pid=1035 comm="httpd" path="/var/www/html/myfile" dev="vda3" ino=769948 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:admin_home_t:s0 tclass=file permissive=0

    ```

At first glance, it is not very easy to read, but let’s analyze it in parts:

*   **type=AVC**: **AVC Audit Events** generated by the *AVC subsystem* (**AVC** means **Access Vector Cache**) as a result of access denials, or where specific events have requested an **audit** message.
*   **msg=audit(1689045662.823:264)**: The *timestamp* of the message in **Unix format** (epoch); use the **date** command to determine the time:

    ```

    # date -d @1689045662.823

    2023 年 7 月 10 日,晚上 09:21:02 CST

    ```

     *   **avc:  denied  { getattr } for  pid=1035**: The result of the **avc** audit event called **denied for** **pid 1035**
*   **comm="httpd"**: The **httpd** command
*   **path="/var/www/html/myfile"**: The **var/www/html/myfile** file path
*   **dev="vda3"**: The **vda3** device
*   **ino=769948**: Inode identifier
*   **scontext=system_u:system_r:****httpd_t****:s0**: *Source* context
*   **tcontext=unconfined_u:object_r:****admin_home_t****:s0**: Target context
*   **tclass=file**: Target class is a file
*   **permissive=0**: SELinux permissive mode disabled

Then, the audit message reads as follows:
*At the described timestamp, an* AVC *event resulted as denied for pid* `1035` *of the* `httpd` *command on the* `/var/www/html/myfile` *file, located on the device and the described inode. The source context type is* `httpd_t` *and the target context type is* `admin_home_t`*. The permissive mode is set* *as disabled.*
This indicates that it is an error of the SELinux contexts since they do not correspond and cannot interact with each other. That is, the web server is not allowed to read that file, as indicated by the output of the `curl` command.
At first, it looked very complicated, but when analyzing the log, the error stands out at a glance.
This is the most common error with SELinux, about contexts that *cannot interact* and whose access is *denied*.
*What is SELinux trying to* *tell me?*
Let’s analyze each of them and their solutions or workarounds.
Labeling
As we observed, every process and object in the system has a *label* associated with it. If files are not labeled in the right context, access may be denied. Or, if *alternate or custom paths* get used for confined domains, SELinux needs to know about it.
Let’s use the following illustrative example.
`/srv/myweb` are not labeled correctly and the web server cannot access them.
In this particular case, a *custom path* to the web server’s content directory appears to be used. To assign the correct label, there are two ways:

*   If you know the correct label, use the **semanage** command to assign it to the policy:

    ```

    semanage 命令将`httpd_sys_content_t`标签应用于/srv/myweb 目录的整个内容,并将其继承到其中创建的新文件。这意味着命令末尾会出现`(/.*)?`字符。

    ```

     *   If you don’t know the correct label, but know a file with the correct label, use the **semanage** command to assign it:

    ```

    # `semanage`命令的-e 选项,将标签分配给参考另一个已知文件并具有正确标签的文件。

    ```

In both cases, to restore the context from the policy, use the `restorecon` command:

restorecon -vR /srv/myweb


 Using the example from the previous section, let’s solve the issue.
**Labeling issue**: If a file moves, instead of copying it, it keeps its original context.
To fix this, use the `chcon` command to change the context:

*   Change the context to the correct label:

    ```

    # chcon -t httpd_system_content_t /var/www/html/myfile

    ```

     *   Change the context with a reference label:

    ```

    # chcon --reference /var/www/html/ /var/www/html/myfile

    ```

In the same way as before, for both cases, to restore the context from the policy, use the `restorecon` command:

chcon 命令。这是解决问题的最简单方法。

如果有自定义路径,需要通知 SELinux 将使用与策略不同的目录。要更改策略,请使用semanage命令。

要将上下文更改保存到策略中,请使用restorecon命令。

当有自定义更改策略时,这属于 SELinux 故障的第二种情况。如果任务的标准使用发生变化,则必须通知 SELinux。

让我们来看一下这些情况。

SELinux 需要知道

根据 Web 服务器的配置,如果使用不同于标准端口(80)的端口——例如,8585——让我们首先找出端口标签。使用semanage命令查询策略中配置的标签:

# semanage port --list | grep -w 80
httpd port is httpd_port_t.
Then, use the `semanage` command to add the `8585` port to the `httpd` policy:

semanage port -a -t http_port_t -p tcp 8585


 Besides these custom configurations, SELinux also offers to change pre-loaded configurations in the policy, turning them on and off.
These configurations, known as *Booleans*, allow parts of SELinux policies to get modified at runtime without the need to rewrite the policy.
For example, if we want to allow the web server to send mail with the `sendmail` service, *turn on* the Boolean with the `setsebool` command:

使用 -P 选项时,布尔值更改会在系统重启时持续生效。

要查看所有布尔值,请使用getsebool命令,并带上-a选项:

# getsebool -a

图 12.8 – SELinux 布尔值

图 12.8 – SELinux 布尔值

要审查布尔描述,可以使用semanage boolean命令,并带上-l选项:

# semanage boolean -l

图 12.9 – 审查布尔描述

图 12.9 – 审查布尔描述

这两种情况,错误标签和自定义配置,是 SELinux 错误最常见的原因。

还有其他两种情况,虽然不常见,但有可能发生。

我们来看第一个案例。

策略 bug

在某些情况下,当行为未能达到预期时,策略可能不起作用,如以下几种情况:

  • 配置

  • 标准输出重定向

  • 过滤的文件描述符

  • 可执行内存

  • 错误构建的库

  • 代码中的不寻常路径

策略或应用程序很可能存在 bug。这些 bug 或行为缺陷应报告给开发人员以修复。

这是一个不寻常的情况,因为开发人员会密切关注 SELinux 策略,但也不能排除发生这种情况的可能性。

一个不太常见的情况是系统可能已经被攻破,并且策略的行为发生变化,允许信息泄露或入侵。

我们来看最后一种 SELinux 失败的情况。

黑客攻击

如果当前工具不能有效区分上下文,或者如果您发现受限域尝试执行以下操作,则存在黑客攻击的风险:

  • 加载内核模块

  • 关闭 SELinux 强制模式

  • 写入etc_t/shadow_t

  • 更改防火墙规则

如果发生这种情况,请小心,因为系统可能已经被攻破,您的信息可能处于危险之中。

黑客攻击并不常见,但可以放心的是,黑客知道如何使用 SELinux。不要轻信,跟随直觉。如果你发现意外的行为,并且与其他 SELinux 失败无关,那么你的系统可能已被黑客入侵。

认真对待,并彻底检查您的系统。

你永远不知道何时可能成为安全攻击的目标。

总结

在本章中,我们概述了 SELinux 的使用,并区分了两种访问控制类型:自主强制。作为强制访问控制,SELinux 可以帮助我们增强系统的安全性。

SELinux 的使用被认为非常复杂,但我们提供了几个简化其工作原理的示例。SELinux 是一个标签系统

我们学习了如何使用 SELinux 查看错误日志,并发现 SELinux 失败的情况只有四种,并查看了它们的解决方案。最常见的失败情况是标签问题或使用了自定义配置的服务。

同时,策略可能存在错误,应该报告给开发人员。

在一个较为罕见的案例中,我们分析了策略行为的变化可能是由于对我们系统的攻击

在下一章中,我们将深入探讨如何虚拟化完整系统,或通过容器将它们缩小到最小的表达形式。

进一步阅读

要了解本章涉及的更多内容,您可以访问以下链接:



第十五章:虚拟化与容器

回到 1990 年代,大多数公司使用来自单一供应商的物理服务器和 IT 堆栈,这些堆栈无法在其他供应商的硬件上运行传统应用程序。这导致了庞大的多服务服务器环境,非常复杂且难以管理。

随着公司通过更便宜的服务器、操作系统和来自多个供应商的应用程序升级其 IT 环境,他们开始低效使用物理硬件。每台服务器只能运行一个特定任务且仅来自一个供应商。

虚拟化是解决两个问题的自然方案:公司可以将服务器分区并在多种类型和版本的操作系统上运行传统应用程序,同时服务器的使用效率得到提高,从而减少了与购买、安装、冷却和维护相关的成本。

接下来的步骤是对这些服务进行容器化。容器化是一种虚拟化形式。虚拟化的目标是在单台服务器上运行多个操作系统实例,而容器化则运行一个操作系统实例,并通过多个用户空间将进程相互隔离,以提供服务。

在本章中,我们将简要介绍虚拟化容器化,并涵盖以下主要内容:

  • 使用 QEMU、KVM 和 libvirt 进行虚拟化

  • 使用 GNOME Boxes

  • 使用 Podman 发现 OCI 容器

技术要求

为了完成本章提到的主题,您必须安装每个部分中指示的包。在每个部分中,您将找到每个包所需的不同安装方式的说明。

使用 QEMU、KVM 和 libvirt 进行虚拟化

Fedora Linux 原生支持虚拟化扩展。该支持由基于内核的虚拟机KVM)提供,并作为内核模块可用。QEMU/KVMLibvirt 管理工具包结合是 Fedora Linux 中的标准虚拟化方法。

快速仿真器QEMU)是一个完整的系统仿真器,它与 KVM 一起工作,允许您创建具有硬件和外设的虚拟机。

最后,libvirt 是 API 层,它允许您管理基础架构——也就是创建并运行虚拟机。它包括一个本地虚拟网络,支持虚拟客户系统之间以及与主机之间的安全通信。libvirt 的默认配置还允许对公共网络进行NAT 访问,这对于没有直接访问公共接口的虚拟机或容器非常有用。

下图展示了带有 libvirt 的 QEMU/KVM 架构:

图 13.1 – QEMU/KVM 架构

图 13.1 – QEMU/KVM 架构

让我们简要了解一下管理工具。

管理工具

libvirt 是一个与 Fedora Linux 虚拟化功能交互的 C 工具包。主包包括 libvirtd 服务器,用于导出虚拟化支持。

基本管理工具如下:

  • virsh:这是管理 virsh 来宾域的主要接口。该程序可以创建暂停关闭 域。它还可以列出当前的域及其状态。

  • Virt-manager:这是一个用于管理虚拟机的桌面工具。它提供当前虚拟机的生命周期控制(启动/关机、暂停/恢复、挂起/恢复)、配置新的虚拟机和各种类型的存储、管理虚拟网络、访问虚拟机的图形控制台,并生成本地或远程的性能统计数据:

图 13.2 – 虚拟机管理器

图 13.2 – 虚拟机管理器

  • virt-viewer:这是一个用于显示虚拟机图形控制台的简洁工具。它通过VNCSPICE 协议访问控制台。可以根据虚拟机的名称IDUUID 进行访问。如果虚拟机尚未运行,查看器会等待它启动后再尝试连接控制台。查看器可以连接到远程主机以查找控制台信息,并通过相同的网络传输连接到远程控制台。

  • virt-install 获取启动安装过程所需的最小文件,允许来宾根据需要获取其余的操作系统分发版。也支持 PXE 引导和导入现有磁盘镜像(从而跳过安装阶段)。

    通过适当的命令行参数,virt-install 可以完全无人值守地运行,来宾系统“自我启动”。这使得来宾安装过程的自动化变得简单。

使用 virt-install 的功能可以简化虚拟机创建过程,最小化安装时间。让我们来学习如何执行这个过程。

简化虚拟机创建过程

QEMU 使用 qcow 文件格式存储磁盘镜像文件。它代表 QEMU Copy On Writeqcow 格式采用一种延迟分配存储的磁盘存储优化策略,直到需要时才分配存储。qcow 格式内的文件可以包含多种与特定来宾操作系统相关的磁盘镜像。该格式有三个版本:qcowqcow2qcow3

截至撰写时,绝大多数发行版,包括 Fedora Linux,都提供云端准备的可下载版本。其中一个可选项是 qcow2 镜像磁盘格式。

OpenStack,一个开放标准的云计算平台,提供了一个网页,引用了许多发行版和操作系统的云镜像(您将在下一个示例中看到)。

使用virt-customize命令行工具,你可以修改下载的磁盘镜像,将其导入为即用型虚拟机。

按照以下步骤,使用这三种工具快速创建虚拟机:

  1. 在开始之前,请验证你的工作站是否在BIOS/UEFI中启用了虚拟化功能。

注意

该过程依赖于工作站制造商,请参考制造商文档确认他们是否在 BIOS/UEFI 中启用了虚拟化能力。

  1. 确认你的工作站的处理器是否支持虚拟化的标志:

    $ sudo grep -E 'svm|vmx' /proc/cpuinfo
    
  2. 使用sudo命令安装虚拟化所需的软件包:

    • qemu-kvm

    • virt-manager

    • virt-viewer

    • guestfs-tools

    • virt-install

    • genisoimage

      $ sudo dnf install qemu-kvm virt-manager virt-viewer guestfs-tools virt-install genisoimage
      
  3. 使用浏览器访问docs.openstack.org/image-guide/obtain-images.html并下载所选发行版/操作系统的qcow2磁盘镜像:

图 13.3 – OpenStack – 获取镜像

图 13.3 – OpenStack – 获取镜像

  1. 对于此示例,请下载 Fedora Linux 镜像。使用浏览器访问fedoraproject.org/cloud/download/

图 13.4 – Fedora Cloud 下载页面

图 13.4 – Fedora Cloud 下载页面

下载适用于x86_64架构的qcow2镜像。

注意

每个镜像大约需要 400 MB 的磁盘空间。确保你有足够的存储空间来存放它们,并且在使用过程中它们会不断增长。

  1. 将下载的qcow2镜像移动到KVM工作目录/var/lib/libvirt/images中:

    $ sudo mv Fedora-Cloud-Base-38-1.6.x86_64.qcow2 /var/lib/libvirt/images/
    

注意

最佳实践是复制qcow2镜像并使用虚拟机名称,而不是移动它。可以重复使用原始下载的镜像来创建多个虚拟机:

$ sudo cp Fedora-Cloud-Base-38-1.6.x86_64.qcow2 /var/lib/****libvirt/images/vmtest01.qcow2

  1. 使用virt-customize命令来修改下载的镜像:

    $ sudo virt-customize \
    -a /var/lib/libvirt/images/vmtest01.qcow2 \
    --hostname vmtest01.packt.lab \
    --root-password password:rootpw \
    --ssh-inject 'root:file:labkey.pub' \
    --uninstall cloud-init \
    ssh-keygen command:
    
    

    $ ssh-keygen -t ecdsa -b 521 -f labkey

    
    

注意

有关ssh-keygen命令的更多信息,请参考命令手册:man ssh-keygen

  • --uninstall选项允许我们卸载下载镜像中默认包含的软件。

cloud-init是一个帮助初始化镜像以供 OpenStack 使用的工具集。在这种情况下,由于基本的功能性定制已能最佳工作,因此不需要使用它。

注意

如果cloud-init软件包没有被卸载,虚拟机启动时将会等待cloud-init 脚本的初始化参数,从而导致启动时间较长。

  • 由于这是一个 Fedora Linux 镜像,必须使用--selinux-relabel选项对 SELinux 上下文进行重新标签,因为需要更改或删除多个文件:

图 13.5 – 自定义 qcow2 镜像

图 13.5 – 自定义 qcow2 镜像

现在我们已经完成了定制,让我们将磁盘镜像导入为新的虚拟机。

  1. 要将磁盘映像导入为新的虚拟机,请使用 virt-install 命令:

    $ sudo virt-install \
    --name vmtest01 \
    --memory 1024 \
    --vcpus 1 \
    --disk /var/lib/libvirt/images/vmtest01.qcow2 \
    --import \
    --osinfo fedora38 \
    --noautoconsole
    

    让我们详细看看我们使用的每个选项:

    • virt-install 是一个用于创建新虚拟机的命令行工具。

    • --name 设置新虚拟机实例的名称。名称必须在连接中超管所知道的所有虚拟机中保持唯一,包括那些当前不活跃的虚拟机。

    • --memory 指定分配给虚拟机的内存(以 MiB 为单位)。

    • --vcpus 指定为虚拟机配置的 vcpus 数量。

    • –disk 指定用作虚拟机存储的设备。在此情况下,这是磁盘映像的路径。

    • --import 表示将磁盘映像导入为新虚拟机,围绕此磁盘映像构建一个虚拟机。

    • --osinfo 优化虚拟机配置,以适配特定的操作系统版本或发行版。在此案例中,指定的是 fedora38

注意

要查找支持的操作系统名称值列表,请使用 virt-install 命令——即 virt-install --****osinfo list

  • --noautoconsole 指定不自动连接到虚拟机控制台:

图 13.6 – 创建新虚拟机

图 13.6 – 创建新虚拟机

  1. 验证已创建虚拟机的状态:

    $ sudo virsh list
    

    以下是输出:

图 13.7 – 验证新虚拟机的状态

图 13.7 – 验证新虚拟机的状态

现在,让我们通过 SSH 连接到虚拟机。

  1. 使用 virsh 命令查找由虚拟化 DHCP 服务分配的 IP 地址:

    $ sudo virsh domifaddr vmtest01
    

    以下是输出:

图 13.8 – 查找虚拟机的 IP 地址

图 13.8 – 查找虚拟机的 IP 地址

  1. 使用 ssh 命令和 SSH 密钥访问虚拟机:

    $ ssh –i labkey root@192.168.124.225
    

    你将得到以下输出:

图 13.9 – 访问虚拟机

图 13.9 – 访问虚拟机

通过这些步骤,你可以在短时间内启动一个正常工作的虚拟机。掌握这些命令可以加速这个过程。

在进入容器化(微型虚拟化形式)之前,让我们看看 GNOME 在其桌面套件中包含的工具。

使用 GNOME Boxes

GNOME Boxes 是 GNOME 桌面环境中的一个应用程序,用于访问远程或虚拟系统。Boxes 使用 QEMU、KVM 和 Libvirt 虚拟化技术。

除了前面章节中提到的虚拟化扩展外,Boxes 至少需要 20 GB 存储空间500 MB RAM 分配给虚拟机。GNOME 建议工作站至少配备 8 GB 的 RAM 和 20 GB 存储空间,以高效运行 Boxes。Boxes 会根据厂商建议自动动态分配资源给虚拟机。

让我们回顾一下使用 Boxes 创建虚拟机的过程:

  1. 要访问 Boxes,请打开 活动概览 并输入 Boxes

图 13.10 – 通过活动概览访问 GNOME Boxes

图 13.10 – 通过活动概览访问 GNOME Boxes

  1. 主 Boxes 窗口将出现。要创建新虚拟机,点击左上角的加号+)按钮:

图 13.11 – GNOME Boxes 主窗口

图 13.11 – GNOME Boxes 主窗口

  1. Boxes 提供两种创建新虚拟机的选项:

图 13.12 – 使用 Boxes 创建新的虚拟机

图 13.12 – 使用 Boxes 创建新的虚拟机

它们如下所示:

  • 从文件安装:使用此选项时,必须指定新虚拟机中要安装的操作系统的ISO镜像位置。

  • 下载操作系统:此选项选择操作系统版本或发行版,并下载相应的镜像以安装到新虚拟机中:

图 13.13 – 选择操作系统镜像

图 13.13 – 选择操作系统镜像

对于这个例子,我们自己下载 ISO 镜像。

  1. 使用浏览器访问 fedoraproject.org/workstation/download/

图 13.14 – Fedora Workstation 下载页面

图 13.14 – Fedora Workstation 下载页面

下载 x86_64 架构。

  1. Boxes中,点击加号+)并选择从文件安装选项。导航到下载的ISO镜像所在位置并点击打开按钮:

图 13.15 – 创建新的虚拟机

图 13.15 – 创建新的虚拟机

Boxes 将显示虚拟机创建选项。使用默认选项并点击创建按钮。

  1. 下载的镜像(在本例中为 Fedora Workstation)的安装将在裸机上开始:

图 13.16 – Fedora Workstation 实时镜像

图 13.16 – Fedora Workstation 实时镜像

  1. 在 Fedora Workstation 安装窗口中,点击安装 Fedora按钮并继续安装操作系统:

图 13.17 – Fedora Linux 安装窗口

图 13.17 – Fedora Linux 安装窗口

  1. 安装完成后,点击完成安装按钮并重启虚拟机:

图 13.18 – 完成虚拟机安装

图 13.18 – 完成虚拟机安装

在重启虚拟机后,它将出现在主Boxes窗口中:

图 13.19 – Boxes 主窗口

图 13.19 – Boxes 主窗口

  1. 点击虚拟机图标以访问它:

图 13.20 – 访问虚拟机

图 13.20 – 访问虚拟机

虚拟机窗口占据了主Boxes窗口的空间,并允许我们以图形方式使用它,就像远程会话一样:

图 13.21 – Fedora 工作站虚拟机

图 13.21 – Fedora 工作站虚拟机

现在,使用虚拟机来执行分配给它的任务。

如果虚拟机关闭,它会出现在主窗口中,如下图所示:

图 13.22 – 盒子中的虚拟机关闭

图 13.22 – 盒子中的虚拟机关闭

  1. 右键单击虚拟机图标以访问管理选项:

图 13.23 – 虚拟机管理选项

图 13.23 – 虚拟机管理选项

  1. 要启动虚拟机,双击其图标。

GNOME Boxes 是一个简单的应用程序,用于访问和管理虚拟机。它不提供 virsh精细管理自动化功能,但它是一个很好的工具,可以帮助熟悉虚拟机的使用。

一种更精细的方法,创建时间更短,且主机资源优化更好,就是使用容器。

现在,让我们学习 Fedora Linux 如何实现开放容器的使用。

使用 Podman 发现 OCI 容器

粗略来说,Linux 容器的工作原理类似于虚拟化过程,我们导入操作系统的 预构建镜像 并从中创建虚拟机。在容器的情况下,镜像仅打包了应用程序操作所需的 程序及其最小依赖项

容器是一组与系统其他部分隔离的一个或多个进程。

内核提供以下主要组件:

  • 使用 namespaces 来确保进程隔离。

  • 使用 cgroups 来控制系统资源。

  • 使用 SELinux 确保主机与容器之间,以及容器与容器之间的隔离。

管理接口与内核组件交互,并提供用于 构建和管理 容器的工具。运行容器所需的所有文件来自于镜像。

容器镜像存储在一个名为 registry 的外部仓库中。要创建容器,需要下载注册表镜像并生成一个可在主机上运行的应用程序副本。这些过程需要运行时,并且镜像需要是特定格式才能运行。

Linux 基金会赞助 开放容器倡议 (OCI) 项目,目的是围绕容器格式和运行时创建开放的行业标准。

目前 OCI 包含三个规范:

  • 运行时规范 (runtime-spec) 。该规范指定了如何运行解压缩到磁盘上的文件系统包。

  • 镜像规范 (image-spec) 。该规范提供了可互操作的工具,用于构建、传输和准备容器镜像以供运行。

  • 分发规范 (distribution-spec) 。该规范定义了一个 API 协议,用于简化和标准化内容分发。

一个 OCI 实现会下载一个 OCI 镜像,然后将该镜像解压到 OCI 运行时文件系统包中。

Fedora Linux 实现了Podman用于 OCI 容器管理。

Podman 依赖于OCI 合规的容器运行时runccrunrunv 等)与操作系统进行交互,并通过命令行创建运行中的容器:

图 13.24 – Podman 交互

图 13.24 – Podman 交互

使用 Podman 控制的容器可以由root非特权用户运行。Podman 管理整个容器生态系统,包括 Pod、容器、容器镜像和容器卷,使用 libpod 库。

要以非 root 用户身份运行容器,必须考虑所使用的资源:

  • 容器镜像将存储在用户的主目录($HOME/.local/share/containers/storage/)中,而不是/var/lib/containers

  • 由于没有root权限,必须遵循以下规则:

    • 无法访问低于1024的端口

    • 存储必须保存在本地文件系统中

让我们从安装实用程序开始。

安装 Podman 需要管理员权限。以 root 用户身份运行 dnf 命令:

# dnf install podman

现在,让我们创建一个示例容器,以展示作为非 root 用户时该实用程序的作用:

  1. 验证无根配置:

    $ podman unshare cat /proc/self/uid_map
    

    你应该看到以下输出:

图 13.25 – Podman 无根配置

图 13.25 – Podman 无根配置

  1. 创建一个基础容器。使用 podman pull 命令下载镜像:

    $ podman pull ubi9/ubi
    

注意

Red Hat 通用基础镜像UBI)使你能够构建、共享并与容器化应用进行协作。

以下是输出结果:

图 13.26 – 获取容器镜像

图 13.26 – 获取容器镜像

下载完成后,验证并检查镜像。

  1. 使用 podman images 命令列出下载的镜像:

    $ podman images
    

    以下是输出结果:

图 13.27 – 列出容器镜像

图 13.27 – 列出容器镜像

使用 镜像 ID 来检查它。

  1. 检查下载的镜像可以为我们提供有关镜像创建和使用的信息,以及可用的变量。使用podman inspect命令并指定镜像的名称或 ID:

    $ podman inspect 05936a40cfa4
    

    你将看到以下输出:

图 13.28 – 检查 UBI 镜像

图 13.28 – 检查 UBI 镜像

现在,让我们从下载的镜像创建一个容器。

  1. 运行 UBI 镜像的容器以显示操作系统版本:

    $ podman run ubi9/ubi cat /etc/os-release
    

    以下是输出结果:

图 13.29 – 运行容器

图 13.29 – 运行容器

该容器是运行在 Fedora Linux 上的 RHEL9 基础镜像。

现在,让我们创建一个容器化服务的示例。按照以下步骤操作:

  1. 使用浏览器访问registry.fedoraproject.org/

图 13.30 – Fedora 项目容器镜像注册表

图 13.30 – Fedora 项目容器镜像注册表

  1. 找到最新的网页服务器镜像httpd)并复制其pull命令:

    docker pull registry.fedoraproject.org/f29/httpd
    

注意

修改pull命令,使其使用podman而非docker

  1. 下载容器镜像并验证:

    $ podman pull registry.fedoraproject.org/f29/httpd
    $ podman images
    

    你应该看到以下输出:

图 13.31 – 获取 httpd 镜像

图 13.31 – 获取 httpd 镜像

  1. httpd镜像进行容器的测试运行:

    $ podman run httpd
    

    这是输出内容:

图 13.32 – 运行 httpd 镜像容器

图 13.32 – 运行 httpd 镜像容器

以这种方式运行httpd镜像容器会阻止使用终端。让我们用自定义名称并在后台运行它。

  1. myapache作为名称,并在后台运行httpd镜像容器。使用--name-d选项与podman run命令一起使用:

    $ podman run --name myapache -d httpd
    $ podman ps
    

    你应该看到以下输出:

图 13.33 – 运行容器并验证其状态

图 13.33 – 运行容器并验证其状态

检查运行中的容器,找到你可以用来访问服务的信息。

  1. 使用podman inspect命令:

    $ podman inspect myapache | grep IPAddress
    $ podman inspect myapache | grep expose-service
    

    你应该看到以下内容:

图 13.34 – 查找容器化服务的访问

图 13.34 – 查找容器化服务的访问

容器化服务没有分配IPAddress,但打开了端口80808443。检查日志以获取更多信息。

  1. 使用podman logs命令查看日志:

    $ podman logs myapache
    

    这是输出内容:

图 13.35 – 审查服务日志

图 13.35 – 审查服务日志

在容器化服务的日志中,找到打开端口8443的 IP 地址。使用这些信息验证服务的访问情况。

  1. 验证访问容器化服务。使用以下curl命令:

    $ curl 10.0.2.100:8443
    

    这是输出内容:

图 13.36 – 验证对网页服务的访问

图 13.36 – 验证对网页服务的访问

服务未能在端口84438080上响应。

这是因为容器端口没有引用主机端口。删除容器并重新创建它,引用本地端口。

  1. 移除容器。首先,使用stop选项中断容器运行,然后使用rm选项删除容器,命令为podman

    $ podman stop myapache
    $ podman rm myapache
    

    你应该看到以下输出:

图 13.37 – 删除容器

图 13.37 – 删除容器

验证myapache容器是否已经不再运行。使用podman ps命令进行验证。

  1. 通过将容器的端口8080映射到本地端口8080,重新创建myapache容器。在我们在第 4 步中运行的podman run命令中添加-p选项:

    $ podman run --name myapache -d -p 8080:8080 httpd
    

    这是输出内容:

图 13.38 – 映射容器端口到本地端口

图 13.38 – 映射容器端口到本地端口

  1. 使用 podman ps 命令确认端口是否正确映射。

  2. 重新测试服务,但在本地端口8080上:

    $ curl localhost:8080
    

    你应该看到以下输出:

图 13.39 – 测试服务访问

图 13.39 – 测试服务访问

现在服务可访问,显示了 Apache 测试页面。

一个始终在单个主机上运行的应用程序,通过访问本地磁盘存储来获取运行所需的信息。这些存储卷在逻辑上物理上都保持持久性。容器化的动态和弹性部署将应用程序存储的逻辑状态和物理状态分开。

由于 OCI 技术固有的重新分配和扩展能力,容器化应用程序可能是逻辑常驻但物理临时的。

如果一个容器驻留在主机上,但该容器停止工作,容器管理器可以在另一台主机上启动一个新的实例。该应用程序可能需要在运行时访问一些数据,这被称为逻辑持久性。由于容器信息默认是临时的,因此存储在物理上是短暂的。

在容器化存储中,一般的趋势是尽可能将持久存储短暂容器结合起来。

现在,让我们学习如何为我们的容器化服务添加持久存储。请按照以下步骤操作:

  1. 停止并移除 myapache 容器:

    $ podman stop myapache
    $ podman rm myapache
    
  2. 在用户的主目录中,为 myapache 容器创建持久存储的工作目录:

    $ mkdir –p ~/containers/myapache/var/www/html
    

    现在,让我们为我们的服务创建一个自定义主页。

  3. 在我们为 myapache 容器创建的持久存储目录中创建 index.html 文件:

    $ echo "Hello from myapache container!" > ~/containers/myapache/var/www/html/index.html
    
  4. 使用 -v 选项重新生成 myapache 容器,将持久存储作为 myapache 容器的卷添加。

  5. 使用之前提到的挂载卷选项重新运行容器。与端口一样,本地目录必须映射到容器目录。在这种情况下,由于涉及文件操作,添加 -Z 选项,将 SELinux 策略从容器目录应用到本地目录:

    $ podman run --name myapache -d -p 8080:8080 -v ~/containers/myapache/var/www/html:/var/www/html:Z httpd
    
  6. 验证容器是否正在运行,并确认你能访问该服务:

图 13.40 – 测试具有持久存储的容器

图 13.40 – 测试具有持久存储的容器

现在,我们的 web 服务具有持久存储。这个存储可以在本地管理,或独立于运行内容的容器实例进行管理。

该服务可以作为容器自行管理,并将此管理交给 systemd 作为系统的一部分。让我们学习如何设置自我管理。

使用之前示例中的相同 myapache 容器,按照以下步骤操作:

  1. 停止并移除 myapache 容器:

    $ podman stop myapache
    $ podman rm myapache
    
  2. 为了使myapache容器自主管理,添加一个命令来确定容器的健康状况。在我们的示例中,我们将使用之前示例中的curl命令。使用--health-cmd--health-interval选项在容器中定义这些选项:

    $ podman run --name myapache -d -p 8080:8080 -v ~/containers/myapache/var/www/html:/var/www/html:Z --health-cmd="curl localhost:8080 || exit 1" --health-interval=0 httpd
    

    这是输出结果:

图 13.41 – 使容器自主管理

图 13.41 – 使容器自主管理

验证容器的创建情况。然后,使用podman ps命令。

  1. 要检查容器的健康状况,运行podman healthcheck run命令:

    $ podman healthcheck run myapache
    

    这是输出结果:

图 13.42 – 审查容器健康状况

图 13.42 – 审查容器健康状况

  1. 上述命令的输出应该不返回任何内容。运行echo命令查看特殊变量$?的输出,以获取命令的结果:

    $ 0), the command ran *successfully*, which means that the health of the container is good.
    

因此,myapache容器能够指示其何时工作正常——也就是说,它是自主管理的。这种管理可以作为操作系统服务的一部分委托给操作系统。为此,必须设置systemd来支持从用户会话启动的服务。按照以下步骤进行:

  1. 作为root,通过运行loginctl命令启用用户持久化:

    # loginctl enable-linger acallejas
    
  2. 创建一个目录来托管容器化服务,并切换到该目录:

    $ mkdir -p ~/.config/systemd/containerized-services
    $ cd ~/.config/systemd/containerized-services
    
  3. 使用podman generate命令构建systemd的配置文件:

    podman generate systemd --name myapache --files
    

    这是输出结果:

图 13.43 – 容器化服务配置文件

图 13.43 – 容器化服务配置文件

上述命令会创建container-myapache.service单元文件,并为systemd管理配置。

  1. 通过运行以下systemctl命令重新加载用户的守护进程:

    $ systemctl --user daemon-reload
    
  2. 通过运行以下systemctl命令启用用户的容器化服务:

    $ systemctl --user enable --now ~/.config/systemd/containerized-services/container-myapache.service
    
  3. 通过运行以下systemctl命令验证容器化服务的状态:

    $ systemctl --user status container-myapache
    

    你应该能看到以下输出:

图 13.44 – 验证容器化服务

图 13.44 – 验证容器化服务

最后,验证容器化服务是否继续运行。使用我们之前使用过的curl命令。

通过这些内容,我们已经学习了如何使用 OCI 容器,从简单的使用到创建由操作系统管理的容器化服务。

Fedora Linux 实现了 OCI 容器的使用,以便简化服务管理。根据工作负载的不同,我们可以选择虚拟机,虚拟机可以使用基于云的镜像创建,或者使用传统的安装镜像创建。或者,如果我们只需要部署一个简单的服务,如myapache,我们也可以选择使用容器。

我希望这些选项能帮助你提高作为 Linux 系统管理员的日常工作效率。

总结

在本章中,我们简要概述了创建虚拟机的方法。在我看来,最简单和最快的方法是依赖于使用预构建的云镜像。通过几步操作,经过一定的练习,我们可以在几分钟内使其运行。

除此之外,我们还使用 Gnome Boxes 作为一种传统的方法,通过安装镜像来创建虚拟机。这种方法稍微慢一些,但允许我们定制来宾操作系统的安装。

接下来,我们研究了服务,在其中我们学习了如何使用 Fedora Linux 实现的 OCI 容器。这是一种简单的创建容器化服务的方式,因为它赋予了我们定制这些服务的能力,以满足我们的需求。例如,我们可以将它们用于持久化存储、自我管理,甚至将一个容器化服务委派为由systemd管理的系统服务。

所有这些工具构成了一个武器库,可以帮助缓解 Linux 系统管理员日常任务的执行。正如我们在第十章中提到的,“一个优秀的系统管理员会在 空闲时间 进行备份、自动化和学习。”

恭喜你读完了本书!希望你觉得它有用。

延伸阅读

要了解本章所涵盖的更多内容,您可以访问以下链接:

第十六章:索引

由于本电子书版本没有固定的分页,以下的页码仅作为参考,超链接是基于本书的印刷版。

A

AAC 344

Affero 通用公共许可证 273

AGPLv3 272

AlmaLinux 15

Ansible 414

自动化 414

配置 416-423

安装 416-423

层 415

参考链接 423

用例方法 415

工作单元级别 415

AppImage 应用程序 189

开发 194, 195

运行 189-194

应用程序编程接口 (API) 220

档案文件 400

ArcMenu 85

扩展 84

Attachmate 集团 302

AVC 审计事件 484

B

备份 400

档案文件 400-405

压缩文件 400-405

balenaEtcher 40

Bash 一行命令 22

Bash 脚本 373-385

电池续航延长

参考链接 91

Bitbucket 387

阻止 110

启动媒体

创建 40

创建,使用 Fedora Media Writer 41-45

操作系统,从 46-50 启动

Bourne Again Shell (Bash) 17

bpytop 177, 178

浏览

扩展,使用 Google Chrome 344-364

Btrfs 文件系统 110

创建 111-117

子卷 114

btrfs-usage-report 命令

使用 124-127

C

Calc 279

Calligra 273

组件 273

CDROM 494

CentOS 项目 8

CentOS Stream 8, 15

Chromium 1.0 344

Chromium 5.0 344

块 118

C 语言 4

CLI

使用 203-213

cloud-init 497

行为准则 11

参考链接 11

命令行 220, 221

命令行界面 16

基础知识 17

控制台,使用 21, 22

文件系统空间 30-33

信息搜索 17

正则表达式处理 27

文本文件编辑器 24

命令行界面,控制台

高级水平 23, 24

基础水平 22

中级水平 22

命令行界面,信息搜索

高级水平 20, 21

基础水平 17-19

中级水平 19, 20

命令行界面,正则表达式处理

高级水平 30

基础水平 27-29

中级水平 29

命令行界面,文本文件编辑器

高级水平 26

基础水平 24, 25

intermediate level 25, 26

compressed file 400

compression utilities 401

computer science 369

Conky 97-104

containerization 491

context 477

Control Center

VPN client, configuring with 167, 168

Conventional Commits

reference link 393

copy-on-write (COW) filesystem 110

Corel’s Word Perfect 280

COW clones 129

CPU 435

usage, improving 441-448

D

Dash to Dock 105

Debian 6

Debian Package 6

delayed allocation 118

desktop environment 34

discretionary access control (DAC) 476

docks 104

handy applications 104

Document Foundation Wiki 291

dynamic priority 443

E

Evolution

mailing with 302-315

Evolution 2.0 302

F

Fedora Core 1 327

Fedora Core 3 328

Fedora Council 11

Fedora Docs 40

Fedora documentation

reference link 39

Fedora Edition 39

Fedora Engineering Steering Committee (FESCo) 11

Fedora Linux 3, 7, 39, 162, 177, 196

extra package selection 69-71

office tools, exploring 272

Fedora Linux, office tools

Calligra 273

fonts 274-276

LibreOffice 276-279

ONLYOFFICE 桌面编辑器 272

WPS Office 272

Fedora 媒体写入器 40

启动媒体,创建 41-45

下载链接 40

Fedora 心智委员会 11

Fedora 项目 7, 56

行为守则 11

贡献 10

Fedora 的使命和基础 9, 10

实现 12-15

Red Hat 贡献路径 8

参考链接 186

系统管理工具 15

Fedora 的使命和基础 9, 10

使命 10

视野 10

Fedora Spins

参考链接 34

Fedora 工作站 40, 73

版本,包管理 64-69

文件 220

文件格式 110

文件系统 110, 430

Firebird 327

Firefox 327-335

自定义 335-344

Firefox 1.0 327

Firefox Nightly 190

firewall-cmd 命令行工具 162

Flatpak 应用程序

构建 213-217

CLI,使用 203-213

检查 196

GUI,使用 197-203

使用 197

浮动码头 105, 106

字体 274-276

自由软件基金会 273

Frippery Panel Favorites 扩展

参考链接 89

G

Git 386, 387

基础 388-400

版本管理 405-414

GitHub 386, 387

GitLab 386

Git 状态

提交 388

已修改 388

阶段 388

GNOME 73, 157

桌面环境 34

GNOME Boxes

使用 501-512

Gnome Frippery Panel Favorites 扩展

参考链接 331

GNOME Shell 扩展 81-83

GNOME 调整工具 78-80

GNU AGPLv3 273

GNU Emacs 4

基础 223-228

功能 235-243

掌握 228-235

概述 221-223

GNU 通用公共许可证 (GPL) 4, 175, 273

GNU/Linux 271

GNU nano 244-246

GNU 项目 4

Google 387

Google Chrome

浏览,扩展 344-364

Google Chrome 5.0 344

GUI

使用 197-203

H

H.264 344

Linux 硬件

参考链接 150

隐藏目录 99

Hurd 5

参考链接 5

I

IBM AIX 操作系统 175

信息技术 369

inode 110

安装包,Fedora Linux

参考链接 69

内部内核操作 430

国际电工委员会 (IEC) 453

国际单位制 (SI) 453

Internet 邮件访问协议 (IMAP) 309

基于 IPsec 的 VPN 166

连接,添加 168-170

ISO 镜像 494

J

Java 起诉 Google

参考链接 276

K

内核

日志级别 377

参数,调优 430-434

调优 430

调优,类型 430

基于内核的虚拟机 (KVM)

用于虚拟化 492

密钥文件插件 140

参考链接 141

密钥文件,网络连接 142

KISS 原则 370

参考链接 371

Konsole 桌面环境 (KDE) 17

Kubernetes

参考链接 426

L

标签 477

问题 485

Lempel-Ziv (LZ77) 401

Lempel-Ziv-Markov 链算法 (LZMA) 401

LibreOffice 271,276

组件 276

LibreOffice Calc 291-294

功能 292

接口 293

LibreOffice Impress 294

LibreOffice Writer 280-282

样式,应用 283-291

libvirt

用于虚拟化 492

Linux

历史 3-5

Linux 发行版 6

Linux 内核驱动数据库 (LKDDb)

参考链接 149

Linux 安全模块 475

Linux 系统管理员 369

本地存储

分区 51-54

逻辑持久性 523

逻辑卷管理器 (LVM) 112-129

快照,比较 129, 130

M

管理工具 492, 493

强制访问控制 (MAC) 475

学习 476

多边安全系统 476

多级安全系统 476

Markdown 语言语法

参考链接 407

机械工程 369

内存 435

使用,提升 448-452

气象学 369

监控工具

概述 435-440

Montserrat 字体项目

参考链接 276

Mozilla 项目 327

Mozilla Thunderbird

邮件发送 315-327

多边安全系统 476

多级安全系统 476

MZLA Technologies Corporation 315

N

Netscape 通信公司 (NSCP) 327

Netscape Communicator 5.0 327

Netscape Communicator 软件 327

网络配置 430

网络连接监控工具 175

bpytop 177, 178

Nigel’s Monitor (nmon) 175-177

网络连接

提升,使用 VPN 165, 166

网络防火墙 161

NetworkManager 140

基础 140, 141

NetworkManager 命令行界面 (nmcli) 141-148

网络调优

用于提升性能 462,463

nice 值 443

Nigel 的监视器(nmon)175-177

nm-connection-editor 157-161

Linux 的 nmon

参考链接 177

非实时策略 443

O

OASIS 办公应用程序的 OpenDocument 格式 273

OCI 容器

发现,使用 Podman 512-526

ODF 电子表格(.ods)292

一次性密码(OTP)173

ONLYOFFICE 桌面编辑器 272

开放容器倡议(OCI)512

分发规范 512

图像规范 512

运行时规范 512

OpenDocument 格式(ODF)273

OpenDocument(ODT)280

OpenOffice

URL 276

OpenStack 494

OpenVPN 166

连接,添加 171-174

OpenWeather 扩展

参考链接 90

OpenWeatherMap

参考链接 100

操作系统(OS)39

启动,从启动介质 46-50

配置,在第一次启动时完成 54-63

P

PackageKit GUI 69

包管理系统(PMS)6

面板

自定义 84-88

分区 50

性能

提升,通过网络调优 462,463

性能,通过网络调优

度量,分析 463-473

Phoenix 0.1 327

Pino 克隆 244

Podman

OCI 容器,使用 512-526 发现

点对点协议(PPP)166

Post-Office Protocol (POP) 309

protocol data unit (PDU) 462

proxy server 165

ps aux command

output, describing 436

Python 177

Q

Qt

URL 40

Quick Emulator (QEMU)

used, for virtualization 492

R

real-time computing (RTC) 443

real-time policies 443

received signal strength (RSSI) 156

reception flow 462

Red Hat 3, 7, 302

Red Hat 6.2E 7

Red Hat contribution path 8

Red Hat Enterprise Linux (RHEL) 7

Red Hat Linux 7

Red Hat Linux 6.1 7

Red Hat Package Manager 7

Red Hat technologies

reference link 425

registry 512

RHEL 2.1AS 8

Rocky 15

role-based access control (RBAC) 476

RPM Fusion 106

RPM Package Manager 7, 69

S

Salesforce 387

sandbox applications

inspecting 184

SELinux sandbox 184-189

Secure Shell (SSH) 166

security 310

Security-Enhanced Linux (SELinux) 475

disabled mode 478

enforcing mode 478

permissive mode 478

working 478-482

Security-Enhanced Linux (SELinux), issues

hack attack 488, 489

labeling 485

policy bugs 488

troubleshooting 483, 484

web server configuration 486, 487

SELinux context 477

SELinux 沙箱 184-189

服务器类型 310

shell 脚本 23

Slackware 6

幻灯片和图像管理

创建 294-298

快照 110

Softlanding Linux 系统 (SLS) 6

软件工程 369

SSH 密钥 417,497

静态优先级 443

存储

空间使用,提升 454-461

调整,注意事项 452-454

存储空间大小

btrfs-usage-report 命令,使用 124-127

优化 117-120

空间分配检查 120-124

Stratis 池

创建 132-136

Stratis 存储

发现 131,132

Stratis 池,创建 132-136

SUSE 302

系统管理员 369

系统管理员法则 370

基本原理 371,372

KISS 原则 370

系统设备 430

系统工程 369

系统调整 74

桌面体验,调整 75-77

GNOME 调整工具 78-80

swappiness 值,调整 74,75

T

tar 命令 402

选项 402

任务栏 88-94

定制 84-88

任务小部件扩展 94,95

参考链接 94

资源监视器 95

系统监视器 95

TopHat 96

生命体征 97

模板管理器 295

文本编辑器与校对工具 (TECO) 4

文本编辑器 220, 221

Tip Isn’t Pico (TIP) 244

top 命令

输出,描述 438

传输流 462

隧道技术 165

调整工具 77

Twitter 387

类型强制 (TE) 476, 477

U

Unetbootin 40

单工信息与计算服务 (UNICS) 3

通用基础镜像 (UBI) 514

通用唯一标识符 (UUID) 454

Unix 3

Unix 纪元 148

Unix 格式 484

UNIX 操作系统 220

V

Vi IMproved (Vim) 25, 246-251

功能 255, 256

配置 256

自定义命令 257

自定义功能 257, 258

映射 259

掌握 251-255

插件 260-269

Vim 插件,面向开发者

参考链接 269

Vim 插件,面向系统管理员

参考链接 269

VimTricks

参考链接 424

虚拟化 491

虚拟机

创建、精简 494-500

虚拟私人网络 (VPN)

用于改善网络连接性 165, 166

视力健康计划 (VHI)

参考链接 63

VPN 客户端

配置,使用控制中心 167, 168

W

Wi-Fi 接入点 153

无线网络连接

设备,识别 149-153

nm-connection-editor 157-161

搜索,以获得最佳连接性能 153-157

安全性 161-165

调优 149

WPS Office 272

Writer 279, 280

X

XML 用户界面语言 (XUL) 315

Packtpub.com

订阅我们的在线数字图书馆,全面访问超过 7000 本书籍和视频,并获得行业领先的工具,帮助您规划个人发展并推进职业生涯。欲了解更多信息,请访问我们的网站。

第十七章:为什么订阅?

  • 通过来自 4000 多位行业专业人士的实用电子书和视频,减少学习时间,增加编程时间

  • 通过为您量身定制的技能计划提高学习效果

  • 每个月获取一本免费的电子书或视频

  • 完全可搜索,便于快速访问关键信息

  • 复制并粘贴、打印和书签内容

您知道 Packt 为每本出版书籍提供电子书版本,包含 PDF 和 ePub 文件吗?您可以在packtpub.com升级到电子书版本,作为纸质书籍的客户,您有资格享受电子书的折扣。请通过 customercare@packtpub.com 联系我们获取更多详情。

www.packtpub.com,您还可以阅读一系列免费的技术文章,注册各种免费的新闻通讯,并获得 Packt 书籍和电子书的独家折扣和优惠。

您可能喜欢的其他书籍

如果您喜欢这本书,您可能会对 Packt 出版的其他书籍感兴趣:

嵌入式 Linux 开发与 Yocto 项目 - 第三版

Otavio Salvador, Daiane Angolini

ISBN:978-1-80461-506-5

  • 掌握 Poky 工作流

  • 配置并准备 Poky 构建环境

  • 通过实例探索 Yocto 项目的最新版本

  • 配置构建服务器并使用 Toaster 自定义镜像

  • 使用 BitBake 生成镜像并将软件包适配到创建的镜像中

  • 通过设置和使用 Package feeds 支持开发过程

  • 通过配置 Poky 调试 Yocto 项目

  • 为 BeagleBone Black、RaspberryPi 4 和 VisionFive 构建并启动 SD 卡镜像

  • 探索使用 QEMU 通过仿真加速开发周期

Linux 存储栈的架构与设计

Muhammad Umer

ISBN:978-1-83763-996-0

  • 理解虚拟文件系统的角色

  • 探索不同类型的 Linux 文件系统及其关键概念

  • 使用块层管理与块设备的 I/O 操作

  • 深入探索小型计算机系统接口(SCSI)子系统及物理设备布局

  • 在每一层存储栈中评估 I/O 性能

  • 探索最佳存储实践

Packt 正在寻找像您这样的作者

如果您有兴趣成为 Packt 的作者,请访问authors.packtpub.com并今天就申请。我们与成千上万的开发者和技术专业人士合作,帮助他们与全球技术社区分享见解。您可以提交通用申请,申请我们正在招募作者的特定热门话题,或提交您自己的想法。

分享您的想法

现在您已经完成了Fedora Linux 系统管理,我们很想听听您的想法!如果您是从 Amazon 购买的此书,请点击这里直达 Amazon 评论页面并分享您的反馈,或在您购买书籍的网站上留下评论。

您的评论对我们以及技术社区非常重要,将帮助我们确保提供卓越质量的内容。

下载本书的免费 PDF 版本

感谢购买本书!

您喜欢随时随地阅读,但又无法携带印刷书籍到处走动吗?您的电子书购买是否与您选择的设备不兼容?

不用担心,现在每本 Packt 书籍您都可以免费获得该书的无 DRM PDF 版本。

随时随地,在任何设备上阅读。直接搜索、复制并粘贴您最喜欢的技术书籍中的代码到您的应用程序中。

优惠还不止这些,您还可以独享折扣、时事通讯和每天发送到您邮箱的精彩免费内容。

按照这些简单步骤即可享受福利:

  1. 扫描二维码或访问下面的链接

packt.link/free-ebook/978-1-80461-840-0

  1. 提交您的购买证明

  2. 就是这样!我们会直接将您的免费 PDF 和其他福利发送到您的电子邮件。


  1. a-z ↩︎

posted @ 2025-07-04 15:40  绝不原创的飞龙  阅读(318)  评论(0)    收藏  举报