如何在基于 Debian 的发行版上使用 update-alternatives 设置默认程序
通常,在我们的 Linux 系统上,我们可以发现安装了两个或多个相同类型的应用程序:通常是文本编辑器,但例如,我们也可以有多个 Web 浏览器。然而,设置用于在系统范围内执行特定任务的默认应用程序有时可能会出现问题。为了解决这个问题,在Debian和基于Debian的Linux发行版上,我们可以使用替代系统和“update-alternatives”工具。
在本教程中,我们将了解如何使用 Debian 替代系统和“update-alternatives”工具为 Debian 和基于 Debian 的发行版上的通用应用程序类型设置默认程序。
在本教程中您将学习:
Debian 替代系统是如何组织的
如何使用 update-alternatives 工具列出可用的替代方案组
如何列出一个群体的所有替代方案及其优先级
如何更改组的当前替代方案
如何创建自定义组
如何删除组中的一个或所有备选方案
Debian 替代系统如何工作
Debian 替代系统基本上使用两个概念:组名和符号链接。让我们以文本编辑器为例。即使在最低配置下,我们通常也会发现安装了多个文本编辑器,其中包括 nano
和 vi
。
为了设置默认的系统文本编辑器,在这种情况下,Debian 替代系统使用抽象或通用名称(“编辑器”)。 /etc/alternatives
目录中具有通用/组名称的文件是一个符号链接,它指向用作默认值的实际应用程序。 Debian 中的默认文本编辑器是“nano”:
lrwxrwxrwx 1 root root 9 Jan 30 12:20 editor -> /bin/nano
从上面的输出中可以看到,在 /etc/alternatives
目录中,editor
是 nano 可执行文件的符号链接:/bin/nano
.此外,通常会在 /usr/bin
(本例中为 /usr/bin/editor)下创建到组名称本身的符号链接:这允许我们调用通用名称,就好像它是一个实际应用。
然而,当我们想要获取有关替代方案的信息或添加修改它们时,我们不必手动创建符号链接。相反,我们需要做的是使用 update-alternatives 实用程序。让我们看看如何。
列出所有可用组
要列出系统上可用的所有替代方案组,我们只需使用 --get-selections
选项调用“update-alternatives”:
$ update-alternatives --get-selections
上面的命令返回以下(截断的)输出:
arptables auto /usr/sbin/arptables-nft
awk auto /usr/bin/mawk
builtins.7.gz auto /usr/share/man/man7/bash-builtins.7.gz
cpp auto /usr/bin/cpp
default-GM.sf2 auto /usr/share/sounds/sf2/TimGM6mb.sf2
default-GM.sf3 auto /usr/share/sounds/sf2/TimGM6mb.sf2
desktop-background auto /usr/share/desktop-base/active-theme/wallpaper/contents/images/1920x1080.svg
desktop-background.xml auto /usr/share/desktop-base/active-theme/wallpaper/gnome-background.xml
desktop-grub auto /usr/share/desktop-base/active-theme/grub/grub-4x3.png
desktop-lockscreen.xml auto /usr/share/desktop-base/active-theme/lockscreen/gnome-background.xml
desktop-login-background auto /usr/share/desktop-base/active-theme/login/background.svg
desktop-plasma5-wallpaper auto /usr/share/desktop-base/active-theme/wallpaper
desktop-theme auto /usr/share/desktop-base/homeworld-theme
ebtables auto /usr/sbin/ebtables-nft
editor auto /bin/nano
[...]
在第一列中,我们可以读取组名称,在第二列中,我们可以读取该组所处的“模式”(稍后会详细介绍);在第三列中报告组符号链接指向的当前目标。
列出组的可用替代方案
要列出特定组的可用替代方案,我们所要做的就是使用 --display
选项调用 update-alternatives,并将组名称作为参数传递。要检索“编辑器”组的可能替代方案,我们将运行:
$ update-alternatives --display editor
上面的命令返回以下输出:
editor - auto mode
link best version is /bin/nano
link currently points to /bin/nano
link editor is /usr/bin/editor
slave editor.1.gz is /usr/share/man/man1/editor.1.gz
slave editor.da.1.gz is /usr/share/man/da/man1/editor.1.gz
slave editor.de.1.gz is /usr/share/man/de/man1/editor.1.gz
slave editor.fr.1.gz is /usr/share/man/fr/man1/editor.1.gz
slave editor.it.1.gz is /usr/share/man/it/man1/editor.1.gz
slave editor.ja.1.gz is /usr/share/man/ja/man1/editor.1.gz
slave editor.pl.1.gz is /usr/share/man/pl/man1/editor.1.gz
slave editor.ru.1.gz is /usr/share/man/ru/man1/editor.1.gz
/bin/nano - priority 40
slave editor.1.gz: /usr/share/man/man1/nano.1.gz
/usr/bin/vim.tiny - priority 15
slave editor.1.gz: /usr/share/man/man1/vim.1.gz
slave editor.da.1.gz: /usr/share/man/da/man1/vim.1.gz
slave editor.de.1.gz: /usr/share/man/de/man1/vim.1.gz
slave editor.fr.1.gz: /usr/share/man/fr/man1/vim.1.gz
slave editor.it.1.gz: /usr/share/man/it/man1/vim.1.gz
slave editor.ja.1.gz: /usr/share/man/ja/man1/vim.1.gz
slave editor.pl.1.gz: /usr/share/man/pl/man1/vim.1.gz
slave editor.ru.1.gz: /usr/share/man/ru/man1/vim.1.gz
在输出的第一行,我们可以看到组当前所处的模式。组可以处于两种模式:自动(默认)或手动。当组处于“自动”模式时(如本例所示),当安装或删除软件包时,替代系统会根据优先级自动决定如何更新链接。。在这种情况下,“editor”组的替代项是“/bin/nano”和“/usr/bin/vim.tiny”。前者的优先级 (40) 高于后者 (15),因此会自动选为默认替代方案。系统管理员执行手动更改后,组将进入“手动”模式。
为了仅可视化可用于组的替代方案的路径,而不需要其他信息,我们可以使用 --list
选项:
$ update-alternatives --list editor
/bin/nano
/usr/bin/vim.tiny
设置组的当前替代方案
为组设置当前替代方案非常简单。我们可以通过交互或非交互的方式来执行这样的操作。后者很有用,因为可以编写脚本,而前者更用户友好,需要用户确认;让我们按顺序看看它们。
以交互方式设置组当前选项
要以交互方式更改组的替代方案,我们使用 --config
选项调用 update-alternatives 并将组名称作为参数传递。执行写入操作时,我们需要使用权限升级来启动命令。假设我们想将 vi 设置为默认编辑器(明智的选择),我们将运行:
$ sudo update-alternatives --config editor
一旦我们启动命令,就会显示可用的选项,每个选项都与一个数字相关联,系统将提示我们输入与我们的选择相对应的选项:
There are 2 choices for the alternative editor (providing /usr/bin/editor).
Selection Path Priority Status
------------------------------------------------------------
* 0 /bin/nano 40 auto mode
1 /bin/nano 40 manual mode
2 /usr/bin/vim.tiny 15 manual mode
Press to keep the current choice[*], or type selection number: 2
在本例中,我们输入“2”。将显示一条确认消息:
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/editor (editor) in manual mode
以非交互方式设置当前组选项
如果我们需要直接更改组的当前替代方案(可能是通过脚本),我们需要使用另一个选项:--set
。在这种情况下,我们必须提供组的名称作为第一个参数,并提供我们想要用于该组的替代路径作为第二个参数。继续前面的示例,要将 vi(在 debian 上为 /usr/bin/vim.tiny
)设置为“editor”组的当前替代项,我们将运行:
$ sudo update-alternatives --set editor /usr/bin/vim.tiny
将自定义组添加到替代系统
通过使用“update-alternative”工具,我们可以在 Debian 替代系统中创建自定义组。要执行此类操作,我们使用 --install
选项,并按顺序提供:
用作可执行文件的主链接的路径
应出现在 /etc/alternatives 目录中的符号链接的名称
另类路径
替代方案的优先级
让我们用一个例子来澄清这一点。假设我们要创建一个名为“python-editor”的新组,并且希望将“idle”(Python 集成开发和学习环境)和 Vim 设置为该组的替代项,并给予前者更高的优先级。要将 Idle 设置为默认替代方案,我们将运行:
$ sudo update-alternatives --install /usr/bin/python-editor python-editor /usr/bin/idle 40
一旦我们启动上面的命令,如果一切按预期进行,系统将响应以下消息:
update-alternatives: using /usr/bin/idle to provide /usr/bin/python-editor (python-editor) in auto mode
该命令创建了 /usr/bin/python-editor
主链接(第一个参数),它指向 /etc/alternatives
中提供的文件名(第二个参数)目录: $ls -l /usr/bin/python-editorlrwxrwxrwx 1 root root 31 Jan 31 06:54 /usr/bin/python-editor -> /etc/alternatives/python-editor
/etc/alternatives/python-editor
文件本身就是一个链接,指向我们选择的替代方案的“真实”可执行文件(第三个参数):
$ ls -l /etc/alternatives/python-editor
lrwxrwxrwx 1 root root 13 Jan 31 06:54 /etc/alternatives/python-editor -> /usr/bin/idle
分配给替代方案的优先级是我们作为命令 (40) 的第四个参数提供的优先级:
python-editor - auto mode
link best version is /usr/bin/idle
link currently points to /usr/bin/idle
link python-editor is /usr/bin/python-editor
/usr/bin/idle - priority 40
要添加 Vim 作为优先级较低的替代方案,我们只需重复该命令并进行适当的更改:
$ sudo update-alternatives --install /usr/bin/python-editor python-editor /usr/bin/vim 30
从组中删除一个或所有备选方案
如果由于某种原因我们想要从组中删除替代项,我们只需使用带有 --remove
选项的“update-alternatives”,并提供组的名称作为第一个参数和路径我们要删除的替代方案,如第二个。要从我们在上一个示例中创建的“python-editor”组中删除 /usr/bin/vim
替代项,我们将运行:
$ sudo update-alternatives --remove python-editor /usr/bin/vim
要从组中删除所有现有替代项,我们需要使用--remove-all
选项,并仅传递组名称作为参数:
$ sudo update-alternatives --remove-all python-editor
从组中删除所有替代项会导致组本身以及所有相关符号链接的删除。
结论
在本教程中,我们学习如何使用 Debian 替代系统和“update-alternatives”实用程序来设置通用应用程序类型的默认程序。我们了解了如何列出所有可用组和所有相关替代方案及其优先级、如何更改组中的当前替代方案、如何创建和填充自定义组,以及最后如何从组中删除一个或所有替代方案。团体。