1. 程式人生 > >[轉載] ubuntu下update-alternatives命令的使用

[轉載] ubuntu下update-alternatives命令的使用

ubuntu下update-alternatives命令的使用

https://www.cnblogs.com/caidi/p/6009217.html

在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命令的預設版本。