Translation(s): English - Italiano - Português (Brasil) - Русский
If you are looking for ways to set default applications on desktops, see GUI Applications in DefaultPrograms.
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.
Contents
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:
An alternative 'name' is something that can be provided by multiple different options (eg editor). This usually corresponds to a command in that is symlink'd to the chosen default (but can be anything, see link group).
An 'alternative' is something that can be used to provide an alternative name. for example, /usr/bin/emacs and /usr/bin/nvi can be used to provide an editor
A 'link group' is the list of symlinks that are modified when the alternative is changed. For example, if we change which alternative provides editor we also need to change which man-page is displayed when the user types man editor: /usr/bin/editor and /usr/share/man/man1/editor.1.gz are in the link group for editor
- Each alternative is assigned a 'priority' (see below), so that installing a new editor doesnt always override the chosen editor
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]...
<mainlink> is the main link provided by the alternative, here /usr/bin/www-browser (usually this is /usr/bin/<name>)
<name> is the name of the alternative. In our example this is www-browser (technically, this is a file in /etc/alternatives)
<path> is the new alternative that will provide <link>. Here, we are adding /usr/bin/edbrowse
<priority> is the priority of the new alternative: this can be any integer, but you probably want something higher than the existing default: 50 is usually a reasonable choice.
--slave link name path is optional, and lets you add other files to the same link group. For example --slave /usr/share/man/man1/www-browser.1.gz www-browser.1.gz /usr/share/man/man1/edbrowse.1.gz will ensure man www-browser shows the edbrowse documentation.
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.
Adding a new link group
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
dh_installalternatives - tool for declarative alternatives maintenance.
galternatives - graphical setup tool for the alternatives system.
Using the Debian alternatives system (Archive.org).
A small chapter in The Debian Administrator's Handbook about alternatives.
This rant in the debian-user mailing list includes an introduction to the Debian alternatives system. Most of that discussion was put into this wiki page.
