Linux 命令 alternatives和update-alternatives


參考:

http://lionbule.iteye.com/blog/717722

http://manpages.ubuntu.com/manpages/oneiric/man8/update-alternatives.8.html

http://blog.chinaunix.net/uid-9185047-id-445077.html

http://blog.csdn.net/heyutao007/article/details/5441482

 

======================================================

 

1. alternatives

 

Linux代碼  收藏代碼
  1. alternatives version 1.3.13.5.EL4 - Copyright (C) 2001 Red Hat, Inc.  
  2. This may be freely redistributed under the terms of the GNU Public License.  
  3.   
  4. usage: alternatives --install <link> <name> <path> <priority>  
  5.                     [--initscript <service>]  
  6.                     [--slave <link> <name> <path>]*  
  7.        alternatives --remove <name> <path>  
  8.        alternatives --auto <name>  
  9.        alternatives --config <name>  
  10.        alternatives --display <name>  
  11.        alternatives --set <name> <path>  
  12.   
  13. common options: --verbose --test --help --usage --version  
  14.                 --altdir <directory> --admindir <directory>  

 

 

2. update-alternatives

 

Linux代碼  收藏代碼
  1. alternatives version 1.3.13.5.EL4 - Copyright (C) 2001 Red Hat, Inc.  
  2. This may be freely redistributed under the terms of the GNU Public License.  
  3.   
  4. usage: alternatives --install <link> <name> <path> <priority>  
  5.                     [--initscript <service>]  
  6.                     [--slave <link> <name> <path>]*  
  7.        alternatives --remove <name> <path>  
  8.        alternatives --auto <name>  
  9.        alternatives --config <name>  
  10.        alternatives --display <name>  
  11.        alternatives --set <name> <path>  
  12.   
  13. common options: --verbose --test --help --usage --version  
  14.                 --altdir <directory> --admindir <directory>  

 

3、實例

 

舉個使用例子吧,改變系統bin默認java的指向:

 

Linux代碼  收藏代碼
  1. 安裝  
  2. #alternatives --install /usr/bin/java java /home/server/jdk1.6.0_21/bin/java 300  
  3.   
  4. 查詢  
  5. #alternatives --display java  
  6.   
  7. 刪除  
  8. #alternatives --remove java  /home/server/jdk1.6.0_21/bin/java  

 

4、總結

 

      1、alternatives和update-alternatives其實一個東東,都指向alternatives。

      2、網上找了一個說明(也不知道對不對)

           用於存放系統的一些默認打開程序的信息和配置, 比如默認的編輯器、默認的網絡瀏覽器、 默認的圖形登陸器、默認的鼠標指針  等。

=====================================================================

Linux 發展到今天,可用的軟件已經非常多了。這樣自然會有一些軟件的功能大致上相同。例如,同樣是編輯器,就有 nvi、vim、emacs、nano,而且我說的這些還只是一部分。大多數情況下,這樣的功能相似的軟件都是同時安裝在系統里的,可以用它們的名稱來執行。例如,要執行 vim,只要在終端下輸入 vim 並按回車就可以了。不過,有些情況下我們需要用一個相對固定的命令調用這些程序中的一個。例如,當我們寫一個腳本程序時,只要寫下 editor,而不希望要為“編輯器是哪個”而操心。Debian 提供了一種機制來解決這個問題,而 update-alternatives 就是用來實現這種機制的。
首先要介紹的參數是 --display。它使我們可以看到一個命令的所有可選命令。執行:
update-alternatives --display editor
可以看到我的機器上的所有可以用來被 editor 鏈接的命令。
--config。這個選項使我們可以選擇其中一個命令程序來作為editor,執行:
update-alternatives --config editor

首先,update-alternatives 在一般情況下是由postinst 和 prerm 這樣的安裝腳本自動調用的,所以一個 alternative 的狀態有兩種:自動和手動。每個 alternative 的初始狀態都是自動。如果系統發現管理員手動修改了一個 alternative,它的狀態就從自動變成了手動,這樣安裝腳本就不會更新它了。如果你希望將一個 alternative 變回自動,只要執行代碼:
update-alternatives --auto editor

general name -- 這是指一系列功能相似的程序的“公用”名字(包括絕對路徑),比如 /usr/bin/editor。
link -- 這是指一個 alternative 在 /etc/alternative 中的名字,比如 editor。
alternative -- 顧名思義,這是指一個可選的程序所在的路徑(包括絕對路徑),比如 /usr/bin/vim。

-auto,--display 和 --config 跟的都是 link。我們要說的第三個概念是優先級。這個 比較簡單,當然優先級越高的程序越好啦(在大多數情況下,我不想爭論)最后一個概 念是主和從的 alternative。想想看,你將 /usr/bin/editor 鏈接到了 vim,可是當你執 行 man editor 時看到的卻是 emacs 的 manpage,你會做何感想呢?這就引出了主和從 alternative 的概念了:當更新主的 alternative 時,從的 alternative 也會被更新。

另外兩個選項:
第一個是 --install。它的格式是:  代碼:

update-alternatives --install gen link alt pri [--slave sgen slink salt] ...

gen,link,alt,pri 分別是我們上面說過的。如果需要從的 alternative,你可以用--slave 加在后面。如果你在向一個已經存在的 alternative 組中添加新的 alternatives,該命令會把這些 alternatives 加入到這個已經存在的 alternative 組的 列表中,並用新的可選命令作為新的命令;否則,將會建立一個新的自動的 alternative組。

嗚呼!我加入了一個錯誤的 alternative。我不想要這個 alternative 了。在這種情況
下,可以執行下面的命令:代碼:

update-alternatives --remove name path

name 是一個在 /etc/alternatives 中的名字,也就是上面的 link,而 path 是希望刪除 的可選程序名的絕對路徑名(放心,這樣只是從列表中刪除了這個程序,並不會真的從硬盤 上刪除程序的可執行文件)。如果從一個 alternative 組中刪除了一個正在被鏈接的程序並且這個組仍然沒有變成空的,update-alternatives 會自動用一個具有其他優先級的可選程序代替原來的程序。如果這個組變成空的了,那么連這個 alternative 組都會被移 除。如果刪除的程序沒有被鏈接,則只有有關這個程序的信息會被移除。

=====================================================================

As @Tommy suggested, you should use update-alternatives.
It assigns values to every software of a family, so that it defines the order in which the applications will be called.

It is used to maintain different versions of the same software on a system. In your case, you will be able to use several declinations of gcc, and one will be favoured.

To figure out the current priorities of gcc, type in the command pointed out by @tripleee's comment:

update-alternatives --query gcc

Now, note the priority attributed to gcc-4.4 because you'll need to give a higher one to gcc-3.3.
To set your alternatives, you should have something like this (assuming you gcc installation is located at /usr/bin/gcc-3.3, and gcc-4.4's priority is less than 50):

update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-3.350

--edit--

Finally, you can also use the interactive interface of update-alternatives to easily switch between versions. Type update-alternatives --config gcc to be asked to choose the gcc version you want to use among those installed.

--edit 2 --

Now, to fix the CXX environment variable systemwide, you need to put the line indicated by @DipSwitch's in your .bashrc file (this will apply the change only for your user, which is safer in my opinion):

echo 'export CXX=/usr/bin/gcc-3.3'>>~/.bashrc

Here's a complete example of jHackTheRipper's answer for the TL;DR crowd. :-) In this case, I wanted to run g++-4.5 on an Ubuntu system that defaults to 4.6. As root:

apt-get install g++-4.5 update-alternatives --install /usr/bin/g++ g++/usr/bin/g++-4.6100 update-alternatives --install /usr/bin/g++ g++/usr/bin/g++-4.550 update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6100 update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.550 update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.6100 update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.550 update-alternatives --set g++/usr/bin/g++-4.5 update-alternatives --set gcc /usr/bin/gcc-4.5 update-alternatives --set cpp-bin /usr/bin/cpp-4.5

Here, 4.6 is still the default (aka "auto mode"), but I explicitly switch to 4.5 temporarily (manual mode). To go back to 4.6:

update-alternatives --auto g++ update-alternatives --auto gcc update-alternatives --auto cpp-bin

(Note the use of cpp-bin instead of just cpp. Ubuntu already has a cpp alternative with a master link of /lib/cpp. Renaming that link would remove the /lib/cpp link, which could break scripts.)

=====================================================================

 

AME

       update-alternatives  -  maintain  symbolic  links  determining  default
       commands

SYNOPSIS

       update-alternatives [options] command

DESCRIPTION

       update-alternatives   creates,   removes,   maintains   and    displays
       information about the symbolic links comprising the Debian alternatives
       system.

       It is possible for several programs  fulfilling  the  same  or  similar
       functions  to  be  installed  on a single system at the same time.  For
       example, many systems have several  text  editors  installed  at  once.
       This  gives  choice  to  the  users of a system, allowing each to use a
       different editor, if desired, but makes it difficult for a  program  to
       make  a  good  choice  for  an  editor  to  invoke  if the user has not
       specified a particular preference.

       Debian's alternatives system aims to solve  this  problem.   A  generic
       name in the filesystem is shared by all files providing interchangeable
       functionality.  The alternatives system and  the  system  administrator
       together  determine  which  actual  file  is referenced by this generic
       name.  For example, if the text  editors  ed(1)  and  nvi(1)  are  both
       installed on the system, the alternatives system will cause the generic
       name /usr/bin/editor to refer to /usr/bin/nvi by  default.  The  system
       administrator  can  override  this and cause it to refer to /usr/bin/ed
       instead, and the alternatives system will not alter this setting  until
       explicitly requested to do so.

       The  generic  name  is  not  a  direct  symbolic  link  to the selected
       alternative.  Instead,  it  is  a  symbolic  link  to  a  name  in  the
       alternatives  directory, which in turn is a symbolic link to the actual
       file referenced.  This is  done  so  that  the  system  administrator's
       changes can be confined within the /etc directory: the FHS (q.v.) gives
       reasons why this is a Good Thing.

       When each package providing a file with a particular  functionality  is
       installed,  changed or removed, update-alternatives is called to update
       information   about   that   file   in   the    alternatives    system.
       update-alternatives  is usually called from the postinst (configure) or
       prerm (install) scripts in Debian packages.

       It is often useful for a number of alternatives to be synchronised,  so
       that they are changed as a group; for example, when several versions of
       the  vi(1)  editor  are  installed,  the   man   page   referenced   by
       /usr/share/man/man1/vi.1 should correspond to the executable referenced
       by /usr/bin/vi.  update-alternatives handles this by  means  of  master
       and  slave links; when the master is changed, any associated slaves are
       changed too.  A master link and its associated slaves make  up  a  link
       group.

       Each  link  group is, at any given time, in one of two modes: automatic
       or manual.  When a group is in automatic mode, the alternatives  system
       will  automatically  decide,  as  packages  are  installed and removed,
       whether and how to update the links.  In manual mode, the  alternatives
       system  will  retain the choice of the administrator and avoid changing
       the links (except when something is broken).

       Link groups are in automatic mode when they are first introduced to the
       system.   If  the  system  administrator  makes changes to the system's
       automatic   settings,   this   will   be   noticed   the   next    time
       update-alternatives  is  run on the changed link's group, and the group
       will automatically be switched to manual mode.

       Each alternative has a priority associated with it.  When a link  group
       is  in  automatic  mode,  the alternatives pointed to by members of the
       group will be those which have the highest priority.

       When using the --config option, update-alternatives will  list  all  of
       the  choices  for  the  link  group  of  which given name is the master
       alternative name. The current choice is marked with a  '*'.   You  will
       then  be prompted for your choice regarding this link group.  Depending
       on the choice made, the link group might no longer be in auto mode. You
       will  need to use the --auto option in order to return to the automatic
       mode (or you  can  rerun  --config  and  select  the  entry  marked  as
       automatic).

       If you want to configure non-interactively you can use the --set option
       instead (see below).

       Different packages providing the same file need to do so cooperatively.
       In  other  words, the usage of update-alternatives is mandatory for all
       involved packages in such case. It is not  possible  to  override  some
       file  in  a  package  that  does  not  employ  the  update-alternatives
       mechanism.

TERMINOLOGY

       Since the activities of update-alternatives are  quite  involved,  some
       specific terms will help to explain its operation.

       generic name (or alternative link)
              A name, like /usr/bin/editor, which refers, via the alternatives
              system, to one of a number of files of similar function.

       alternative name
              The name of a symbolic link in the alternatives directory.

       alternative (or alternative path)
              The name of a specific file in the filesystem, which may be made
              accessible via a generic name using the alternatives system.

       alternatives directory
              A   directory,  by  default  /etc/alternatives,  containing  the
              symlinks.

       administrative directory
              A directory, by default  /var/lib/dpkg/alternatives,  containing
              update-alternatives' state information.

       link group
              A set of related symlinks, intended to be updated as a group.

       master link
              The  alternative  link  in a link group which determines how the
              other links in the group are configured.

       slave link
              An alternative link in a link group which is controlled  by  the
              setting of the master link.

       automatic mode
              When  a link group is in automatic mode, the alternatives system
              ensures that the  links  in  the  group  point  to  the  highest
              priority alternative appropriate for the group.

       manual mode
              When  a  link  group  is in manual mode, the alternatives system
              will  not  make  any  changes  to  the  system   administrator's
              settings.

COMMANDS

       --install link name path priority [--slave link name path]...
              Add  a group of alternatives to the system.  link is the generic
              name for the master link, name is the name of its symlink in the
              alternatives  directory,  and  path  is  the  alternative  being
              introduced for the master link.  The arguments after --slave are
              the generic name, symlink name in the alternatives directory and
              the alternative path for a slave link.   Zero  or  more  --slave
              options,  each  followed  by  three arguments, may be specified.
              Note that the master alternative must exist  or  the  call  will
              fail.   However  if  a  slave  alternative  doesn't  exist,  the
              corresponding  slave  alternative  link  will  simply   not   be
              installed (a warning will still be displayed). If some real file
              is installed where an alternative link has to be  installed,  it
              is kept unless --force is used.

              If   the  alternative  name  specified  exists  already  in  the
              alternatives system's records, the information supplied will  be
              added  as a new set of alternatives for the group.  Otherwise, a
              new group, set to  automatic  mode,  will  be  added  with  this
              information.   If  the group is in automatic mode, and the newly
              added alternatives' priority is higher than any other  installed
              alternatives  for  this  group,  the symlinks will be updated to
              point to the newly added alternatives.

       --set name path
              Set  the  program  path  as  alternative  for  name.   This   is
              equivalent   to   --config   but  is  non-interactive  and  thus
              scriptable.

       --remove name path
              Remove an alternative and all of  its  associated  slave  links.
              name  is  a  name  in the alternatives directory, and path is an
              absolute filename to which name could  be  linked.  If  name  is
              indeed  linked to path, name will be updated to point to another
              appropriate alternative (and the group is put back in  automatic
              mode),  or  removed  if  there  is  no  such  alternative  left.
              Associated   slave   links   will   be   updated   or   removed,
              correspondingly.  If the link is not currently pointing to path,
              no links are changed; only the information about the alternative
              is removed.

       --remove-all name
              Remove all alternatives and all of their associated slave links.
              name is a name in the alternatives directory.

       --all  Call --config on all alternatives. It can be  usefully  combined
              with  --skip-auto to review and configure all alternatives which
              are not configured in automatic mode.  Broken  alternatives  are
              also   displayed.    Thus   a  simple  way  to  fix  all  broken
              alternatives is to call yes  ''  |  update-alternatives  --force
              --all.

       --auto name
              Switch  the  link  group  behind  the  alternative  for  name to
              automatic mode.  In the process,  the  master  symlink  and  its
              slaves  are  updated  to point to the highest priority installed
              alternatives.

       --display name
              Display information about the link group.  Information displayed
              includes  the  group's  mode (auto or manual), which alternative
              the master link currently points to, what other alternatives are
              available  (and their corresponding slave alternatives), and the
              highest priority alternative currently installed.

       --get-selections
              List all master alternative  names  (those  controlling  a  link
              group)  and  their  status.  Each  line  contains up to 3 fields
              (separated by one or  more  spaces).  The  first  field  is  the
              alternative name, the second one is the status (either "auto" or
              "manual"), and the last one contains the current choice  in  the
              alternative  (beware:  it's  a  filename  and thus might contain
              spaces).

       --set-selections
              Read configuration of alternatives  on  standard  input  in  the
              format  generated  by  update-alternatives  --get-selections and
              reconfigure them accordingly.

       --query name
              Display information about the link group  like  --display  does,
              but in a machine parseable way (see section QUERY FORMAT below).

       --list name
              Display all targets of the link group.

       --config name
              Show  available alternatives for a link group and allow the user
              to interactively select which one to  use.  The  link  group  is
              updated.

       --help Show the usage message and exit.

       --version
              Show the version and exit.

OPTIONS

       --altdir directory
              Specifies  the  alternatives  directory,  when  this  is  to  be
              different from the default.

       --admindir directory
              Specifies the administrative  directory,  when  this  is  to  be
              different from the default.

       --log file
              Specifies  the  log  file, when this is to be different from the
              default (/var/log/alternatives.log).

       --force
              Let update-alternatives replace any real file that is  installed
              where an alternative link has to be installed.

       --skip-auto
              Skip  configuration  prompt  for alternatives which are properly
              configured in automatic mode. This option is only relevant  with
              --config or --all.

       --verbose
              Generate more comments about what update-alternatives is doing.

       --quiet
              Don't generate any comments unless errors occur.

ENVIRONMENT

       DPKG_ADMINDIR
              If set and the --admindir option has not been specified, it will
              be used as the base administrative directory.

FILES

       /etc/alternatives/
              The default alternatives directory.  Can be  overridden  by  the
              --altdir option.

       /var/lib/dpkg/alternatives/
              The  default administration directory.  Can be overridden by the
              --admindir option.

EXIT STATUS

       0      The requested action was successfully performed.

       2      Problems were encountered whilst parsing  the  command  line  or
              performing the action.

QUERY FORMAT

       The  update-alternatives  --query  format  is using an RFC822-like flat
       format. It's made of n + 1 blocks where n is the number of alternatives
       available  in  the  queried  link  group.  The first block contains the
       following fields:

       Link: <link>
              The generic name of the alternative.

       Status: <status>
              The status of the alternative (auto or manual).

       Best: <best choice>
              The path of the  best  alternative  for  this  link  group.  Not
              present if there is no alternatives available.

       Value: <currently selected alternative>
              The path of the currently selected alternative. It can also take
              the magic value none. It is used if the link doesn't exist.

       The other blocks describe  the  available  alternatives  in  the
       queried link group:

       Alternative: <path of this alternative>
              Path to this block's alternative.

       Priority: <priority value>
              Value of the priority of this alternative.

       Slaves: <list of slaves>
              When  this  header  is  present,  the next lines hold all
              slave alternatives associated to the master link  of  the
              alternative.  There  is  one  slave  per  line. Each line
              contains  one  space,  the  generic  name  of  the  slave
              alternative,  another  space,  and  the path to the slave
              alternative.

       Example
              $ update-alternatives --query editor
              Link: editor
              Status: auto
              Best: /usr/bin/vim.gtk
              Value: /usr/bin/vim.gtk

              Alternative: /bin/ed
              Priority: -100
              Slaves:
               editor.1.gz /usr/share/man/man1/ed.1.gz

              Alternative: /usr/bin/vim.gtk
              Priority: 50
              Slaves:
               editor.1.gz /usr/share/man/man1/vim.1.gz
               editor.ru.1.gz /usr/share/man/ru/man1/vim.1.gz
               editor.pl.ISO8859-2.1.gz /usr/share/man/pl.ISO8859-2/man1/vim.1.gz
               editor.it.ISO8859-1.1.gz /usr/share/man/it.ISO8859-1/man1/vim.1.gz
               editor.pl.UTF-8.1.gz /usr/share/man/pl.UTF-8/man1/vim.1.gz
               editor.it.1.gz /usr/share/man/it/man1/vim.1.gz
               editor.fr.UTF-8.1.gz /usr/share/man/fr.UTF-8/man1/vim.1.gz
               editor.fr.1.gz /usr/share/man/fr/man1/vim.1.gz
               editor.it.UTF-8.1.gz /usr/share/man/it.UTF-8/man1/vim.1.gz
               editor.pl.1.gz /usr/share/man/pl/man1/vim.1.gz
               editor.fr.ISO8859-1.1.gz /usr/share/man/fr.ISO8859-1/man1/vim.1.gz

DIAGNOSTICS

       With --verbose update-alternatives  chatters  incessantly  about
       its  activities  on  its  standard  output channel.  If problems
       occur,  update-alternatives  outputs  error  messages   on   its
       standard  error  channel and returns an exit status of 2.  These
       diagnostics should be self-explanatory; if you do not find  them
       so, please report this as a bug.

EXAMPLES

       There   are   several  packages  which  provide  a  text  editor
       compatible with vi, for example nvi and vim. Which one  is  used
       is controlled by the link group vi, which includes links for the
       program itself and the associated manpage.

       To display the available  packages  which  provide  vi  and  the
       current setting for it, use the --display action:

              update-alternatives --display vi

       To  choose  a  particular vi implementation, use this command as
       root and then select a number from the list:

              update-alternatives --config vi

       To go back to having the vi implementation chosen automatically,
       do this as root:

              update-alternatives --auto vi

BUGS

       If  you  find  a  bug,  please  report  it using the Debian bug-
       tracking system.

       If  you  find  any  discrepancy   between   the   operation   of
       update-alternatives and this manual page, it is a bug, either in
       the implementation or the documentation; please report it.

AUTHORS

       Copyright (C) 1995 Ian Jackson
       Copyright (C) 2009 Raphael Hertzog

       This is free  software;  see  the  GNU  General  Public  Licence
       version 2 or later for copying conditions. There is NO WARRANTY.

       This  manual  page  is copyright 1997,1998 Charles Briscoe-Smith
       and others.

       This is free documentation; see the GNU General  Public  Licence
       version 2 or later for copying conditions. There is NO WARRANTY.

SEE ALSO

       ln(1), FHS, the Filesystem Hierarchy Standard.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM