ubuntu下update-alternatives命令的使用

在ubuntu系统中,update-alternatives是专门维护系统命令链接符的工具,其可以对某个工具的多个软件版本进行管理,通过它可以很方便的设置系统默认使用哪个命令的哪个软件版本。

 

在命令行中直接输入update-alternatives --help命令,
[19:19:24]@~$ update-alternatives --help
Usage: update-alternatives [<option> ...] <command>

Commands:
  --install <link> <name> <path> <priority>
    [--slave <link> <name> <path>] ...
                           add a group of alternatives to the system.
  --remove <name> <path>   remove <path> from the <name> group alternative.
  --remove-all <name>      remove <name> group from the alternatives system.
  --auto <name>            switch the master link <name> to automatic mode.
  --display <name>         display information about the <name> group.
  --query <name>           machine parseable version of --display <name>.
  --list <name>            display all targets of the <name> group.
  --get-selections         list master alternative names and their status.
  --set-selections         read alternative status from standard input.
  --config <name>          show alternatives for the <name> group and ask the
                           user to select which one to use.
  --set <name> <path>      set <path> as alternative for <name>.
  --all                    call --config on all alternatives.

<link> is the symlink pointing to /etc/alternatives/<name>.
  (e.g. /usr/bin/pager)
<name> is the master name for this link group.
  (e.g. pager)
<path> is the location of one of the alternative target files.
  (e.g. /usr/bin/less)
<priority> is an integer; options with higher numbers have higher priority in
  automatic mode.

Options:
  --altdir <directory>     change the alternatives directory.
  --admindir <directory>   change the administrative directory.
  --log <file>             change the log file.
  --force                  allow replacing files with alternative links.
  --skip-auto              skip prompt for alternatives correctly configured
                           in automatic mode (relevant for --config only)
  --verbose                verbose operation, more output.
  --quiet                  quiet operation, minimal output.
  --help                   show this help message.
  --version                show the version.
[19:19:28]@~$


其工作原理如下:系统路径下,/usr/bin/<name>这个软链接,指向了/etc/alternatives/<name>这个文件,其其实也是个软链接,指向了该<name>命令的实际可执行文件;如下:

lrwxrwxrwx 1 root root 22  5月  6  2015 /usr/bin/java -> /etc/alternatives/java*

lrwxrwxrwx 1 root root 70 10月 28 14:11 /etc/alternatives/java -> /home/likewise-open/SPREADTRUM/hunter.ding/tools/jdk1.8.0_112/bin/java*
可见,通过两次软链接,我们可以定位到实际的java文件;后面我们针对这个软件版本的管理都是通过改变/etc/alternatives/ --> /实际可执行文件 的软链接来进行的。

来看一个例子,在上面的java可执行组中,修改一下java的版本:

[19:37:17]@~$ java -version
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
Java HotSpot(TM) Server VM (build 25.112-b15, mixed mode)
[19:37:20]@~$ sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                                                    Priority   Status
------------------------------------------------------------
* 0            /home/likewise-open/SPREADTRUM/hunter.ding/tools/jdk1.8.0_112/bin/java   1062      auto mode
  1            /home/likewise-open/SPREADTRUM/hunter.ding/tools/jdk1.8.0_112/bin/java   1062      manual mode
  2            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java                           1060      manual mode
  3            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java                           1061      manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java to provide /usr/bin/java (java) in manual mode.
[19:37:24]@~$ java -version
java version "1.6.0_35"
OpenJDK Runtime Environment (IcedTea6 1.13.7) (6b35-1.13.7-1ubuntu0.12.04.2)
OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)
[19:37:26]@~$
可见我们已经将java的版本从1.8切换到1.6了,这样我们来看一下软链接实例:

lrwxrwxrwx 1 root root 22  5月  6  2015 /usr/bin/java -> /etc/alternatives/java*

lrwxrwxrwx 1 root root 46 10月 28 19:37 /etc/alternatives/java -> /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java*
呵呵,/usr/bin/java -> /etc/alternatives/java* 的链接未改变,而改变的是/etc/alternatives/java -> /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java*。

 

主要的几个命令用法如下:

  • --install <link> <name> <path> <priority>

向系统中添加一个新的alternatives组,

link:指向/etc/alternatives/<name>的符号引用

name:这个链接组的名称

path:这个命令对应的可执行文件的实际路径

priority:优先级,在auto模式下,数字较大的选项有较高的优先级

示例: sudo update-alternatives --install /usr/bin/java  java  /home/likewise-open/SPREADTRUM/hunter.ding/tools/jdk1.8.0_112/bin/java  1062

  • --remove <name> <path>   remove <path> from the <name> group alternative.

移除系统中注册的某个<name>的某个软件版本<path>

  • --display <name>         display information about the <name> group.
  • --list <name>            display all targets of the <name> group.

显示命令<name>的信息及目标文件

[19:53:34]@~$ update-alternatives --display java
java - manual mode
  link currently points to /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java
/home/likewise-open/SPREADTRUM/hunter.ding/tools/jdk1.8.0_112/bin/java - priority 1062
/usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java - priority 1060
/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java - priority 1061
Current 'best' version is '/home/likewise-open/SPREADTRUM/hunter.ding/tools/jdk1.8.0_112/bin/java

[19:53:49]@~$ update-alternatives --list java
/home/likewise-open/SPREADTRUM/hunter.ding/tools/jdk1.8.0_112/bin/java
/usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java
/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java

  • --config <name>          show alternatives for the <name> group and ask the user to select which one to use.

配置命令<name>的版本,如下:

[19:55:48]@~$ sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                                                    Priority   Status
------------------------------------------------------------
  0            /home/likewise-open/SPREADTRUM/hunter.ding/tools/jdk1.8.0_112/bin/java   1062      auto mode
* 1            /home/likewise-open/SPREADTRUM/hunter.ding/tools/jdk1.8.0_112/bin/java   1062      manual mode
  2            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java                           1060      manual mode
  3            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java                           1061      manual mode

Press enter to keep the current choice[*], or type selection number: 0
[19:55:56]@~$
这样就使用java1.8作为java命令的默认版本。

posted @ 2016-10-28 19:58  hunterDing  阅读(21537)  评论(0编辑  收藏  举报