Linux Command: update-alternatives

當Linux系統裡面有多套同性質的軟體,或是同樣軟體的不同版本,如何設定所要使用的版本呢?

會發現這個問題主要是因為在公司的軟體開發上,用舊版的automake可以正常的產生config檔,用新版的反而就不行了。其實這時候最簡單的方法就是「移除新版,單單安裝舊版」。但個人又很龜毛,過去的專案也就算了,之後的新專案還是用新版的比較好吧,所以堅持不想移除新版。所以在網路搜尋後就找到了這個指令。

先來看看它的用法:


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.


可以先用 update-alternatives --get-selections 來看看它的效果。輸出結果有三個欄位,第一個是群組名稱、第二個是「自動、手動」、第三個則是實際的執行程式。來看一行範例:

editor                         auto     /usr/bin/joe

群組名稱為「editor」,「auto」代表這是系統自動,實際執行的程式為 /usr/bin/joe.

要變更的話就使用下面的指令:update-alternatives --config editor

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/joe         70        auto mode
  1            /bin/ed             -100       manual mode
  2            /bin/nano            40        manual mode
  3            /usr/bin/jmacs       50        manual mode
  4            /usr/bin/joe         70        manual mode
  5            /usr/bin/jpico       50        manual mode
  6            /usr/bin/jstar       50        manual mode
  7            /usr/bin/rjoe        25        manual mode
  8            /usr/bin/vim.basic   30        manual mode

想當然了,我這個 joe 的愛用者是能去更改設定的。

如果要建立新的群組以及安裝新的選項,則要使用 --install --slave 的功能。

至於這套機制是怎麼運作的呢?其實到 /usr/bin 下面看看就知道了,很容易就會發現有連結到 /etc/alternatives 然後在連結回 /usr/bin。而所謂的更改,也不過就是更改 /etc/alternatives 裡面的連結而已。

不過說真的,後來我也沒這樣改,為什麼呢?這就是另外一個龜毛的故事了  ...

留言

這個網誌中的熱門文章

我弟家的新居感恩禮拜分享:善頌善禱

Openssl 範例程式:建立SSL連線

如何利用 Wireshark 來監聽 IEEE 802.11 的管理封包