LinuxCommandLibrary

alternatives

Manage default commands between multiple versions

TLDR

View documentation for the original command

$ tldr update-alternatives
copy

SYNOPSIS

alternatives --install <link> <name> <path> <priority> [ --slave <slink> <sname> <spath> ... ]
alternatives --config <name>
alternatives --display <name>
alternatives --auto <name>
alternatives --set <name> <path>
alternatives --remove <name> <path>
alternatives [ --query <name> | --check | --version | --help ]

PARAMETERS

--install <link> <name> <path> <priority>
    Registers a new alternative into the system. <link> is the generic symbolic link (e.g., /usr/bin/java), <name> is the symbolic name for this group (e.g., java), <path> is the absolute path to the actual file (e.g., /usr/lib/jvm/java-11/bin/java), and <priority> is an integer; higher values indicate a preferred alternative for automatic selection.

--slave <slink> <sname> <spath>
    Used with --install to register dependent 'slave' links. When the master link is switched, all associated slave links are also updated. <slink> is the slave symbolic link (e.g., /usr/bin/javac), <sname> is its symbolic name (e.g., javac), and <spath> is its actual path (e.g., /usr/lib/jvm/java-11/bin/javac).

--config <name>
    Allows interactive selection of the active alternative for the specified <name> group. It presents a numbered list of available choices for the user to pick from.

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

--auto <name>
    Switches the specified alternative group to 'automatic' mode. In this mode, alternatives selects the highest priority alternative available.

--set <name> <path>
    Manually sets the active alternative for <name> to the specified <path>. This action automatically switches the group to 'manual' mode.

--remove <name> <path>
    Removes the specific alternative <path> from the <name> group. If <path> is the only alternative in the group, the group and its master link are removed entirely.

--query <name>
    Displays information about an alternative group in a machine-readable format, useful for scripting. Similar to --display but structured differently.

--check
    Verifies the integrity of the alternatives system by checking the symbolic links and database entries. It can identify and fix some inconsistencies.

--version
    Displays the version information for the alternatives utility.

--help
    Shows a summary of command-line options and usage.

DESCRIPTION

The alternatives command provides a robust system for managing multiple versions of commands, files, or services on a Linux system. It allows administrators to maintain a single, consistent path (e.g., /usr/bin/java) that can dynamically point to different underlying implementations (e.g., Java 8, Java 11, or OpenJDK).

This utility helps resolve conflicts when multiple packages provide the same functionality, ensuring that system components and user scripts always use the desired version without hardcoding paths. It achieves this by creating and managing symbolic links in specific directories (like /etc/alternatives) and updating them based on user or system configuration. When a new alternative is installed, it registers itself with the alternatives system, along with a priority value. Users can then switch between available options either interactively or by setting a specific path. This mechanism is crucial for system flexibility, maintainability, and providing a consistent user experience across different software versions.

CAVEATS

The alternatives command typically requires root privileges to perform most operations (e.g., --install, --set, --remove). Incorrect usage, such as specifying non-existent paths or removing critical system alternatives, can lead to system instability or broken commands. Users should also be aware of the distinction between the alternatives implementation (primarily found on Red Hat-based systems like RHEL, CentOS, Fedora) and the similar update-alternatives command found on Debian-based systems (like Debian, Ubuntu). While their functionality is very similar, their command names, option syntax, and underlying directory structures (e.g., where configuration files are stored) can differ.

IMPLEMENTATION DIFFERENCES: ALTERNATIVES VS. UPDATE-ALTERNATIVES

While functionally similar, the alternatives command (Red Hat, CentOS, Fedora) and update-alternatives (Debian, Ubuntu) are distinct implementations. Key differences include their executable names, typical installation locations of master and slave links (e.g., /etc/alternatives/ on RHEL, /etc/alternatives/ for master links and /var/lib/dpkg/alternatives/ for configuration files on Debian), and minor variations in command-line options. Users should consult the man page specific to their distribution.

MASTER AND SLAVE LINKS

The alternatives system operates with a 'master' link and optional 'slave' links. A 'master' link (e.g., /usr/bin/java) is the primary command or file being managed. When this master link points to a specific alternative (e.g., /usr/lib/jvm/java-11/bin/java), any associated 'slave' links (e.g., /usr/bin/javac, /usr/bin/javadoc) are automatically updated to point to their corresponding files within the same alternative installation (e.g., /usr/lib/jvm/java-11/bin/javac, /usr/lib/jvm/java-11/bin/javadoc). This ensures consistency when switching between different versions of a software suite.

HISTORY

The concept of an 'alternatives' system originated in Debian Linux with the update-alternatives utility, developed to manage common commands and files provided by multiple packages. This system was designed to allow users and administrators to easily switch between different versions or implementations of a service (e.g., different text editors, mail transfer agents, or Java runtimes) without manually managing complex symbolic links. Red Hat-based distributions subsequently adopted a similar system, implementing their version under the command name alternatives. Both implementations serve the same core purpose: providing a centralized and consistent mechanism for selecting default programs and files across the system, simplifying package management and user configuration.

SEE ALSO

ln(1), symlink(2), update-alternatives(8), dpkg(1), rpm(8)

Copied to clipboard