Linux下alternatives命令学习总结
在Linux系统中提供了一个alternatives命令,用于在多个同功能的软件,或软件的多个不同版本间选择、切换。简单来说就是版本切换控制。例如,你的操作系统有多个Python版本,例如python3.6,Python 3.9,如果不用alternatives命令,那么你可能需要通过手工修改软链接来实现Python版本的切换。如果用alternatives命令就可以很容易的实现Python版本的切换和管理。
另外,关于命令alternatives与update-alternatives的关系,其实先有update-alternatives命令,然后才有alternatives,update-alternatives最开始是Debian Linux下的一个项目,用于管理多版本,它是Perl编写的,然后RHEL重写了这个项目,名字也改为了alternatives,alternatives则在基于Fedora的分发版本(Redhat, CentOS)中发行,传播,而update-alternatives一般存在Debian Linux下。但是为了统一或者方便,在RHEL下你也会看到update-alternatives这个命令,它此时实际上是一个软链接,指向alternatives命令。如下所示:
# more /etc/redhat-release
Red Hat Enterprise Linux release 8.8 (Ootpa)
# whereis alternatives
alternatives: /usr/sbin/alternatives /etc/alternatives /usr/share/man/man8/alternatives.8.gz
# whereis update-alternatives
update-alternatives: /usr/sbin/update-alternatives /usr/share/man/man8/update-alternatives.8.gz
# ll /usr/sbin/update-alternatives
lrwxrwxrwx. 1 root root 12 Jul 27 2021 /usr/sbin/update-alternatives -> alternatives
查看alternatives命令的帮助信息
alternatives或alternatives --help
$ alternatives
alternatives version 1.19.1 - Copyright (C) 2001 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.
usage: alternatives --install <link> <name> <path> <priority>
[--initscript <service>]
[--family <family>]
[--slave <slave_link> <slave_name> <slave_path>]*
alternatives --remove <name> <path>
alternatives --auto <name>
alternatives --config <name>
alternatives --display <name>
alternatives --set <name> <path>
alternatives --list
alternatives --remove-all <name>
alternatives --add-slave <name> <path> <slave_link> <slave_name> <slave_path>
alternatives --remove-slave <name> <path> <slave_name>
common options: --verbose --test --help --usage --version --keep-missing --keep-foreign
--altdir <directory> --admindir <directory>
主要常使用的参数是 install ,remove,config,display,list这5个参数。
install --生成软连接
remove --删除软连接
config --选择软连接
display --显示软连接
list --显示所有软连接
例子:显示所有软连接
$ alternatives --list
libnssckbi.so.x86_64 auto /usr/lib64/pkcs11/p11-kit-trust.so
python auto /usr/libexec/no-python
cifs-idmap-plugin auto /usr/lib64/cifs-utils/cifs_idmap_sss.so
ld auto /usr/bin/ld.bfd
modules.sh auto /usr/share/Modules/init/profile.sh
python3 manual /usr/bin/python3.9
例子:显示python3的软连接
$ alternatives --display python3
python3 - status is manual.
link currently points to /usr/bin/python3.9
/usr/bin/python3.6 - priority 1000000
slave easy_install-3: /usr/bin/easy_install-3.6
slave pip-3: /usr/bin/pip-3.6
slave pip3: /usr/bin/pip3.6
slave pydoc-3: /usr/bin/pydoc3.6
slave pydoc3: /usr/bin/pydoc3.6
slave pyvenv-3: /usr/bin/pyvenv-3.6
slave python3-man: /usr/share/man/man1/python3.6.1.gz
/usr/bin/python3.9 - priority 3900
slave easy_install-3: /usr/bin/easy_install-3.9
slave pip-3: /usr/bin/pip-3.9
slave pip3: /usr/bin/pip3.9
slave pydoc-3: /usr/bin/pydoc3.9
slave pydoc3: /usr/bin/pydoc3.9
slave pyvenv-3: (null)
slave python3-man: /usr/share/man/man1/python3.9.1.gz
Current `best' version is /usr/bin/python3.6.
如上所示,--display显示链接组的所有信息,包括链接的模式(自动还是手动)、链接priority值、所有可用的链接命令
例子:选择软连接
注意,选择软链接必须使用root权限,否则会遇到权限问题,如下所示:
$ alternatives --config python3
There are 2 programs which provide 'python3'.
Selection Command
-----------------------------------------------
* 1 /usr/bin/python3.6
+ 2 /usr/bin/python3.9
Enter to keep the current selection[+], or type selection number: 1
failed to create /var/lib/alternatives/python3.new: Permission denied
root用户下操作:
# alternatives --config python3
There are 2 programs which provide 'python3'.
Selection Command
-----------------------------------------------
* 1 /usr/bin/python3.6
+ 2 /usr/bin/python3.9
Enter to keep the current selection[+], or type selection number: 1
# alternatives --list
libnssckbi.so.x86_64 auto /usr/lib64/pkcs11/p11-kit-trust.so
python auto /usr/libexec/no-python
cifs-idmap-plugin auto /usr/lib64/cifs-utils/cifs_idmap_sss.so
ld auto /usr/bin/ld.bfd
modules.sh auto /usr/share/Modules/init/profile.sh
python3 manual /usr/bin/python3.6
(*)星号表示当前系统使用的版本,加号表示优先级最高的。输入数值可修改默认配置,直接按回车保持原来状态
例子:生成软连接python
alternatives --install <link> <name> <path> <priority>
# link是在/usr/bin/,/usr/local/bin/等默认PATH搜索目录
# name是在/etc/alternatives目录中的链接名
# path是真正的可执行程序的位置,可以在任何位置
# priority是优先级
例如,当前环境只有python3,你想使用python这个命令,而不想使用python3的话,那么我们可以生成一个软连接。
# alternatives --install /usr/bin/unversioned-python python /usr/bin/python3.9 2
# alternatives --list | grep python
python auto /usr/bin/python3
python3 manual /usr/bin/python3.9
注意,link的命令最好合乎规范,否则可能有告警信息:
# alternatives --install /usr/bin/python python /usr/bin/python3.9 2
the primary link for python must be /usr/bin/unversioned-python
# alternatives --list
libnssckbi.so.x86_64 auto /usr/lib64/pkcs11/p11-kit-trust.so
python auto /usr/bin/python3
cifs-idmap-plugin auto /usr/lib64/cifs-utils/cifs_idmap_sss.so
ld auto /usr/bin/ld.bfd
modules.sh auto /usr/share/Modules/init/profile.sh
python3 manual /usr/bin/python3.9
关于priority优先级, 当命令链接已存在时,需高于当前值,因为当alternative为自动模式时,系统默认启用priority高的链接
例子:删除软连接
# alternatives --remove python /usr/libexec/no-python
那么我们接下来简单探究一下,在版本切换时,alternatvies命令做了一下啥
# alternatives --list | grep python3
python auto /usr/bin/python3
python3 manual /usr/bin/python3.9
# whereis python3
python3: /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3 /usr/bin/python3.9 /usr/lib/python3.6 /usr/lib/python3.9 /usr/lib64/python3.6 /usr/lib64/python3.9 /usr/include/python3.6m /usr/include/python3.9 /usr/share/man/man1/python3.1.gz
# ll /usr/bin/python3
lrwxrwxrwx. 1 root root 25 May 12 11:04 /usr/bin/python3 -> /etc/alternatives/python3
# ll /etc/alternatives/python3
lrwxrwxrwx 1 root root 18 Jul 6 15:00 /etc/alternatives/python3 -> /usr/bin/python3.9
# ll /usr/bin/python3.9
-rwxr-xr-x 1 root root 7776 Dec 21 2022 /usr/bin/python3.9
# alternatives --config python3
There are 2 programs which provide 'python3'.
Selection Command
-----------------------------------------------
* 1 /usr/bin/python3.6
+ 2 /usr/bin/python3.9
Enter to keep the current selection[+], or type selection number: 1
# python3 --version
Python 3.6.8
# ll /usr/bin/python3
lrwxrwxrwx. 1 root root 25 May 12 11:04 /usr/bin/python3 -> /etc/alternatives/python3
# ll /etc/alternatives/python3
lrwxrwxrwx 1 root root 18 Jul 7 10:46 /etc/alternatives/python3 -> /usr/bin/python3.6
如上所示,我们看到alternatvies其实是通过多一层软链接,例如,/usr/bin/python3指向/etc/alternatives/python3,这一层关系不变, 切换python版本时,我们看到链接/etc/alternatives/python3 指向了不同的python版本来实现的。