Guide to Linux Command and Unix Command Alternatives

Alternatives sets symbolic links for common default programs

Graphic image of computer code with bands of blue and purple color

Negative Space / Pexels / CC0

Alternatives creates, removes, maintains and displays information about the symbolic links comprising the alternatives system. The alternatives system is a re-implementation of the Debian alternatives system.

Several programs fulfilling the same or similar functions often appear on a single system at the same time. For example, many systems have several text editors installed at once. This diversity 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 of editor to invoke if the user has not specified a particular preference.

The 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 and nvi 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.

When each package providing a file with a particular functionality is installed, changed or removed, alternatives is called to update information about that file in the alternatives system.alternatives is usually called from the %post or %pre scripts in RPM packages.

It is often useful for a number of alternatives to be synchronized so that they are changed as a group. For example, when several versions of the vi editor are installed, the man page referenced by /usr/share/man/man1/vi.1 should correspond to the executable referenced by/usr/bin/vi. Alternatives handles this referencing 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.

Terminology

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

  • Generic name: A name, like /usr/bin/editor, which refers, through the alternatives system, to one of a number of files of similar function.
  • Symlink: Without any further qualification, this means a symbolic link in the alternatives directory: one which the system administrator is expected to adjust.
  • Alternative: The name of a specific file in the filesystem, which may be made accessible through 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/alternatives, containing alternatives' state information.
  • Link group: A set of related symlinks, intended to be updated as a group.
  • Master link: The link in a link group which determines how the other links in the group are configured.
  • Slave link: A 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 alternatives 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.

Synopsis

The command takes one of the following forms:

alternatives [options] --install link name path priority [--slave link name path]... [--initscriptservice]
alternatives [options] --remove name path
alternatives [options] --set name path
alternatives [options] --auto name
alternatives [options] --display name
alternatives [options] --config name  

Options

Exactly one action must be specified if alternatives is to perform any meaningful task. Any number of the common options may be specified together with any action.  

  • --verbose: Generate more comments about what alternatives is doing.
  • --quiet: Don't generate any comments unless errors occur. This option is not yet implemented.
  • --test: Don't actually do anything, just say what would be done. This option is not yet implemented.
  • --help: Give some usage information (and say which version of alternatives this is).
  • --version: Tell which version of alternatives this is (and give some usage information).
  • --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.

Actions

The actions include a special syntax of their own.

Install

Add a group of alternatives to the system. Name is the generic name for the master link, link is the name of its symlink, and path is the alternative being introduced for the master link. Snameslink and spath are the generic name, symlink name and alternative for a slave link, and service is the name of any associated initscript for the alternative. 

--initscript is a Red Hat Linux specific option. Zero or more --slave options, each followed by three arguments, may be specified.

If the master symlink 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.

If --initscript is used, the alternatives system will manage the initscript associated with the alternative through chkconfig, registering and unregistering the init script depending on which alternative is active.

--initscript is a Red Hat Linux specific option.

Remove

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 pathname will be updated to point to another appropriate alternative, 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.

Set

The symbolic link and slaves for link group name set to those configured for path, and the link group is set to manual mode. This option is not in the original Debian implementation.

Auto

Switch the master symlink name to automatic mode. In the process, this symlink and its slaves are updated to point to the highest priority installed alternatives.

Display

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