LinuxCommandLibrary

update-alternatives

Manage default commands via symbolic links

TLDR

Add a symbolic link

$ sudo update-alternatives --install [path/to/symlink] [command_name] [path/to/command_binary] [priority]
copy

Configure a symbolic link for java
$ sudo update-alternatives --config [java]
copy

Remove a symbolic link
$ sudo update-alternatives --remove [java] [/opt/java/jdk1.8.0_102/bin/java]
copy

Display information about a specified command
$ update-alternatives --display [java]
copy

Display all commands and their current selection
$ update-alternatives --get-selections
copy

SYNOPSIS

update-alternatives [options]

Common usages:
update-alternatives --install [--slave ]...
update-alternatives --remove
update-alternatives --set
update-alternatives --config
update-alternatives --display
update-alternatives --auto
update-alternatives --get-selections
update-alternatives --set-selections

PARAMETERS

--install
    Adds a new group of alternatives or an alternative to an existing group. is the generic symbolic link (e.g., /usr/bin/java), is the name of the alternatives group (e.g., java), is the actual path to the alternative executable, and is an integer (higher priority is preferred in automatic mode).

--slave
    Used in conjunction with --install to add dependent alternative links. For example, if java is the master link, javac or javadoc could be slave links, ensuring they switch together when the master changes.

--remove
    Removes a specific alternative from the alternative group identified by .

--set
    Manually sets the specified as the active alternative for the group , switching the group to manual mode.

--config
    Provides an interactive interface to select the active alternative for the group from a numbered list. This also switches the group to manual mode.

--display
    Shows detailed information about the specified alternative group, including its current mode (auto/manual), the currently selected alternative, and all available alternatives with their priorities.

--auto
    Switches the specified alternative group back to automatic mode. The system will then select the alternative with the highest priority.

--get-selections
    Displays all alternatives selections currently managed by update-alternatives, useful for backup or migration purposes.

--set-selections
    Reads alternatives selections from standard input, allowing restoration of selections from a backup created by --get-selections.

--help
    Displays a brief help message and usage information for the command.

--version
    Displays the version information of the update-alternatives utility.

DESCRIPTION

The update-alternatives command is a powerful utility predominantly found on Debian-based Linux systems, designed to manage symbolic links that point to different versions of commands or applications. Its primary purpose is to provide a standardized, system-wide method for administrators to choose between multiple implementations of the same generic command (e.g., different Java Development Kits, various text editors like Vim or Nano, or multiple web browsers).

Instead of manually creating or changing symlinks, update-alternatives maintains a central registry of 'alternatives' for each generic command. It operates by creating master symlinks in /etc/alternatives/, which then point to the actual program paths. Public-facing symlinks in directories like /usr/bin/ then point to these master symlinks. This structured approach allows easy switching of the system's default program version, ensuring consistency across the system and avoiding conflicts when installing or removing software packages.

CAVEATS

update-alternatives is a command primarily used on Debian-based Linux distributions (e.g., Ubuntu, Mint). Red Hat-based systems (e.g., Fedora, CentOS, RHEL) use a similar but distinct command simply called alternatives. Misuse of this command, especially when manually setting or removing critical system alternatives, can lead to broken system paths or unexpected behavior for core system commands.

AUTOMATIC VS. MANUAL MODE

Each alternative group can operate in one of two modes:
Automatic Mode: The system automatically selects the alternative with the highest priority value. If the currently selected highest-priority alternative is removed, the next highest is chosen.
Manual Mode: The administrator has explicitly chosen a specific alternative using --set or --config. The system will not automatically change this selection, even if a higher priority alternative is installed, unless the manually selected alternative becomes invalid and the system reverts to auto-selection.

ALTERNATIVE GROUPS AND SLAVE LINKS

update-alternatives manages 'groups' of related binaries or files. For example, a Java Development Kit typically includes java, javac, and javadoc. The --slave option allows these related components to be managed as a single logical unit. When you switch the main (master) alternative (e.g., java), all its associated slave alternatives (e.g., javac, javadoc) are also automatically switched to paths corresponding to the selected master, ensuring a consistent development environment.

CONFIGURATION FILES LOCATION

The configuration data for update-alternatives is typically stored in text files within the directory /var/lib/dpkg/alternatives/. The actual master symbolic links managed by the system, which are pointed to by the public-facing symlinks (e.g., in /usr/bin/), are themselves located in /etc/alternatives/.

HISTORY

The update-alternatives utility was developed as an integral part of the Debian project's dpkg package management system. Its inception addressed a common challenge in Linux distributions: managing multiple co-existing implementations of the same generic command (e.g., different versions of Java or various text editors). It provides a robust, system-wide mechanism for administrators to select default applications without resorting to manual symbolic link management or complex environment variable configurations, thereby preventing conflicts between different software packages that provide the same functionality.

SEE ALSO

Copied to clipboard