命令 alternatives
描述 maintain symbolic links determining default commands 更改默认命令链接
用法 1 2 3 4 5 6 7 8 9 10 alternatives --install <link> <name> <path> <priority> [--initscript <service>] [--family <family>] [--slave <link> <name> <path>]* alternatives --remove <name> <path> alternatives --auto <name> alternatives --config <name> alternatives --display <name> alternatives --set <name> <path> alternatives --list
选项 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Options: COMMON OPTIONS --verbose 显示详情 --quiet 静默模式,只显示错误信息,功能尚未实现 --test 测试执行,功能尚未实现 --keep-missing 如果切换到新版本时出错,则重新切换回上一个版本 --altdir 指定软链接目录,默认目录/etc/alternatives --admindir 指定保存软链接信息的管理目录,默认目录/var/lib/alternatives ACTIONS --install <link> <name> <path> <priority> [--slave slink sname spath] [--initscript service]... 创建新的链接组,link是软链接路径,name是链接组名称,path是命令路径,priority是优先级 NOTE: --initscript is a Red Hat Linux specific option. NOTE: --family is a Red Hat Linux specific option. --remove <name> <path> 从链接组中删除一个路径 --set <name> <path> 命令模式切换链接 --config <name> 交互模式切换链接 --auto <name> 切换链接组为自动模式,即自动选择优先级最高的命令路径作为命令链接 --display <name> 显示指定链接组的信息 --list 显示所有链接组信息
注意 不要更改系统本身的软链接路径,否则可能导致系统部分组件无法运行,如更改/usr/bin/python
的链接路径后可能导致yum
命令无法使用 Ubuntu系统中的命令为update-alternatives
示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 $ alternatives --version alternatives version 1.7.6 $ yum install java-1.8.0-openjdk java-11-openjdk $ alternatives --display java |head java - status is auto. link currently points to /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64/jre/bin/java /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64/jre/bin/java - family java-1.8.0-openjdk.x86_64 priority 1800312 slave alt-java: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64/jre/bin/alt-java slave jjs: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64/jre/bin/jjs slave keytool: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64/jre/bin/keytool $ alternatives --display java |grep family /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64/jre/bin/java - family java-1.8.0-openjdk.x86_64 priority 1800312 /usr/lib/jvm/java-11-openjdk-11.0.13.0.8-1.el7_9.x86_64/bin/java - family java-11-openjdk.x86_64 priority 1 $ java -version openjdk version "1.8.0_312" OpenJDK Runtime Environment (build 1.8.0_312-b07) OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode) $ alternatives --config java There are 2 programs which provide 'java' . Selection Command ----------------------------------------------- *+ 1 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64/jre/bin/java) 2 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-1.el7_9.x86_64/bin/java) Enter to keep the current selection[+], or type selection number: 2 $ update-alternatives --display java |head java - status is manual. link currently points to /usr/lib/jvm/java-11-openjdk-11.0.13.0.8-1.el7_9.x86_64/bin/java $ java -version openjdk version "11.0.13" 2021-10-19 LTS OpenJDK Runtime Environment 18.9 (build 11.0.13+8-LTS) OpenJDK 64-Bit Server VM 18.9 (build 11.0.13+8-LTS, mixed mode, sharing) $ alternatives --auto java $ java -version openjdk version "1.8.0_312" OpenJDK Runtime Environment (build 1.8.0_312-b07) OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode) $ grep @ /var/lib/alternatives/java @java-1.8.0-openjdk.x86_64@1800312 @java-11-openjdk.x86_64@1 $ alternatives --set java java-11-openjdk.x86_64 $ yum install python36 $ alternatives --install /usr/bin/python python /usr/bin/python2.7 20 $ alternatives --install /usr/bin/python python /usr/bin/python3.6 10 $ alternatives --display python python - status is auto. link currently points to /usr/bin/python2.7 /usr/bin/python2.7 - priority 20 /usr/bin/python3.6 - priority 10 Current 'best' version is /usr/bin/python2.7. $ python -V Python 2.7.5 $ alternatives --config python There are 2 programs which provide 'python' . Selection Command ----------------------------------------------- *+ 1 /usr/bin/python2.7 2 /usr/bin/python3.6 Enter to keep the current selection[+], or type selection number: 2 $ ll /usr/bin/python lrwxrwxrwx 1 root root 24 Jan 18 18:57 /usr/bin/python -> /etc/alternatives/python $ ll /etc/alternatives/python lrwxrwxrwx 1 root root 18 Jan 18 18:57 /etc/alternatives/python -> /usr/bin/python3.6 $ ll /usr/bin/python3.6 -rwxr-xr-x 2 root root 11328 Nov 17 2020 /usr/bin/python3.6 $ yum repolist File "/bin/yum" , line 30 except KeyboardInterrupt, e: ^ SyntaxError: invalid syntax $ alternatives --set python /usr/bin/python2.7 $ python -V Python 2.7.5 $ more /var/lib/alternatives/python manual /usr/bin/python /usr/bin/python2.7 20 /usr/bin/python3.6 10 $ alternatives --remove python /usr/bin/python2.7 $ alternatives --remove python /usr/bin/python3.6 $ alternatives --display python $ ll /usr/bin/python ls : cannot access /usr/bin/python: No such file or directory$ ln -s /usr/bin/python2.7 /usr/bin/python $ python -V Python 2.7.5