Differences between revisions 18 and 19
Revision 18 as of 2020-06-29 14:28:15
Size: 6633
Editor: Brian Potkin
Revision 19 as of 2020-06-29 16:00:33
Size: 6721
Editor: PaulWise
Comment: dh_installalternatives
Deletions are marked like this. Additions are marked like this.
Line 117: Line 117:

 * DebianMan:dh_installalternatives - tool for declarative alternatives maintenance

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

If you are looking for other mechanisms to set default applications, refer to this page on Desktop default settings.

Debian Alternatives System

The Debian alternatives system creates a way for several programs that fulfill the same or similar functions to be listed as alternative implementations that are installed simultaneously but with one particular implementation designated as the default. For example many systems have several text editors installed at the same time. The vi program is a classic example of an editor that has many implementations such as nvi, elvis, vim, etc. but which one should be designated as the default?

Debian's alternatives system aims to solve the problem of selecting a default preferred version. Management is done through the update-alternatives program that creates, removes, maintains and displays information about the symbolic links constituting the Debian alternatives system. Priorities are assigned by package creators. The alternative with the highest priority determines the default value when in automatic mode. Additionally the local administrator may override the automatic selection with a manual selection.

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.

Here is a previous rant on the subject in the debian-user mailing list that I think is a good introduction to using the Debian alternatives system. I will eventually pull most of that discussion into this wiki page but for now I will just reference it.

Adding a new alternative to an existing group

One common scenario for interacting with the Debian alternatives system is to register a custom application for an existing link group.

For example, on a system with w3m and edbrowse, running update-alternatives --config www-browser (as root) will return the following:

There is only one alternative in link group www-browser (providing /usr/bin/www-browser): /usr/bin/w3m
Nothing to configure.

If one wants to use edbrowse as a www-browser alternative, they have to install it using update-alternatives - specifically,

--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 distinction between the master link <link> and the alternatives directory link <name> can be seen here:

$ ls -l /usr/bin/www-browser
lrwxrwxrwx 1 root root 29 Dec 16  2018 /usr/bin/www-browser -> /etc/alternatives/www-browser
$ ls -l /etc/alternatives/www-browser
lrwxrwxrwx 1 root root 12 May 19 01:34 /etc/alternatives/www-browser -> /usr/bin/w3m

To add edbrowse, we can do (as root):

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

update-alternatives will complain if <link> is not an absolute path. This is because it is a user-specified path for a symlink to /etc/alternatives/<name>, rather than the name for a file in a default folder. In our scenario, we want to continue using /usr/bin/www-browser, and just want to add an alternative to /etc/alternatives/www-browser - so we use /usr/bin/www-browser.

<priority> is a number you have to provide - numbers with a higher priority are the first to be chosen in 'auto-mode' (the default mode for all Debian alternatives). In our scenario, the priority of w3m was 25, so we use 50, promoting the new edbrowse as the new default www-browser. You can see this with --config (again, as root):

$ 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

See Also