Translation(s): English - Italiano - Português (Brasil) - Русский



Debian Alternatives System

The Debian alternatives system lets you choose which of several similar programs should be designated as the default.

For example, you can install several text editors (nano, nvi, emacs, etc) and use the alternatives system to designate one as the default. Programs that want to launch an editor can simply call /usr/bin/editor without needing to know which editor the user actually wants to use.

Implementation

The alternatives system is implemented using symlinks managed by the update-alternatives program in the dpkg package.

The update-alternatives program is used to set or display information about the chosen alternative.

Some terminology used in this page:

Priorities

Priorities are assigned to alternatives by package creators.

The alternative with the highest priority usually determines the default alternative to use (automatically), but the local administrator may override the automatic selection with a manual selection.

For example, the default editor is nano. If you install an editor assigned a higher priority, such as jed, it will automatically become the new default editor. But if you install an editor that has been assigned a lower priority, such as emacs, the default does not change. Emacs was assigned a lower priority because it is a rather complicated, graphical, programme that is probably not what you want to launch as root: you can choose emacs as your /usr/bin/editor by making a manual selection if you want.

Examples

Changing which alternative is the default

To change which alternative is the default, use update-alternatives --config name and select the programme you want from the menu. For example,

update-alternatives --config editor

will list the available editors. The line with a * is the current default.

If you want to do this in a script use, for example, update-alternatives --set editor /usr/bin/emacs

Registering a new programme as providing an alternative

Adding a new programme as an alternative is done with update-alernatives --install.

For example, to use edbrowse as www-browser you can run:

update-alternatives --install /usr/bin/www-browser www-browser /usr/bin/edbrowse 50

The full syntax is:

update-alternatives --install <mainlink> <name> <path> <priority> [--slave link name path]...

One way to find the syntax is to look at the postinst script of something already providing an alternative.

If you have added the new alternative with a higher priority it will be automatically promoted to the default:

$ update-alternatives --config www-browser
There are 2 choices for the alternative www-browser (providing /usr/bin/www-browser).

  Selection    Path               Priority   Status
------------------------------------------------------------
* 0            /usr/bin/edbrowse   50        auto mode
  1            /usr/bin/edbrowse   50        manual mode
  2            /usr/bin/w3m        25        manual mode

Press <enter> to keep the current choice[*], or type selection number:

Because this was manually done, if you now remove the edbrowse package, the alternative will not be automatically removed. To remove it, run as root:

update-alternatives --remove www-browser /usr/bin/edbrowse

And all is well again.

You can also use the above procedure to make a new link group entirely.

For example, on a CDE system with nedit and dtpad, one might want a motif-text-editor link group to match the gnome-text-editor group. Running:

update-alternatives --install /usr/bin/motif-text-editor motif-text-editor /usr/bin/nedit 25

will do the trick.

One-Liners to manipulate alternatives

Here is a long one-liner that will print out all manually configured Debian Alternatives symlinks.

for i in /etc/alternatives/*; do
  LANG=C update-alternatives --display "${i#/etc/alternatives/}";
done 2>/dev/null | awk '/manual.mode/{print $1}'

If all of those are something that you want to reset to automatic then this can be done using the same generated list. Otherwise simply grep out the ones that you want or don't want.

for i in /etc/alternatives/*; do
  LANG=C update-alternatives --display "${i#/etc/alternatives/}";
done 2>/dev/null |
  awk '/manual.mode/{print $1}' |
  xargs -L 1 sudo update-alternatives --auto

History

The Debian alternatives system was originally created for Debian but has been picked up by other GNU/Linux software distributions. For example Red Hat now uses a fork of the code to maintain system software such as /usr/sbin/sendmail. On RHEL/Fedora /usr/sbin/sendmail is an alternatives symlink to either the classic Sendmail or to Postfix as alternative implementations of an MTA. In fact the alternative group is called the mta group there.

See Also