zoukankan      html  css  js  c++  java
  • 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命令的默认版本。

  • 相关阅读:
    赞赏出版模式:在大众之中寻找大家,在凡品之上打造精品-百道网
    赞赏——人人都能赞赏成书
    赞赏网---赞赏网
    知乎利用众筹模式出书 颠覆传统出版业规则|众筹模式|出书|知乎_新浪新闻
    发起项目
    北京墨知缘文化传媒有限公司,出书,出书挂名, 代理出书,学术出书,出书流程,出书渠道,如何出书,出版,出版图书,想出书,怎么出书,出书费用,出书哪家好,那家出书最便宜,图书出版,书号申请,墨之源,墨知源,墨之缘
    springboot~openfeign从JSON文件读取数据
    springboot~openfeign从此和httpClient说再见
    java~mac下的终端工具oh-my-zsh
    springboot~内嵌redis的使用
  • 原文地址:https://www.cnblogs.com/caidi/p/6009217.html
Copyright © 2011-2022 走看看