How to use the command 'update-alternatives' (with examples)
- Linux
- December 17, 2024
update-alternatives
is a command-line tool used in Unix-like operating systems to manage different versions of commands and their symbolic links. This utility is particularly handy for system administrators who handle various versions of software that provide the same functionality but where one needs to be configured as the default choice. By conveniently maintaining symbolic links, update-alternatives
allows smooth switching between different versions of a software application or utility.
Add a symbolic link:
Code:
sudo update-alternatives --install /usr/bin/java java /opt/java/jdk1.8.0_102/bin/java 1
Motivation:
Sometimes system administrators install multiple versions of an application and want to manage which version is used by default. Using the update-alternatives
command with the --install
option allows them to set up a symbolic link to the command binary, specifying its location and establishing a priority level for selection purposes.
Explanation:
sudo
: Run the command with administrative privileges, required for modifying system paths.update-alternatives
: The command to manage alternative symbolic links.--install
: An option that adds a new link to the list of alternatives./usr/bin/java
: The path where the symlink will be created/updated.java
: The name of the command being managed./opt/java/jdk1.8.0_102/bin/java
: The full path to the command’s binary file.1
: The priority for selecting this alternative. Higher numbers signify greater preference.
Example Output:
update-alternatives: using /opt/java/jdk1.8.0_102/bin/java to provide /usr/bin/java (java) in auto mode
Configure a symbolic link for java
:
Code:
sudo update-alternatives --config java
Motivation:
Users and administrators often need to choose between multiple installed versions of a program for their specific needs. The --config
option prompts the user to select which version of the software should be active by default.
Explanation:
sudo
: Needed to modify system-wide symbolic links.update-alternatives
: The core command.--config
: An option that allows the user to interactively set the default version.java
: The name of the item to configure.
Example Output:
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /opt/java/jdk1.8.0_102/bin/java 1 auto mode
1 /opt/java/jdk1.8.0_102/bin/java 1 manual mode
2 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 2 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Remove a symbolic link:
Code:
sudo update-alternatives --remove java /opt/java/jdk1.8.0_102/bin/java
Motivation:
Over time, administrators may want to prune unused or obsolete versions of software to prevent clutter or inadvertent usage. The --remove
option facilitates this by deleting a specified path from the list of alternatives.
Explanation:
sudo
: Grants permission to alter files in system directories.update-alternatives
: The command being employed.--remove
: An option to cleanly remove a specified alternative from the list.java
: The command name whose entry is to be removed./opt/java/jdk1.8.0_102/bin/java
: The actual path of the alternative that should be removed.
Example Output:
update-alternatives: removing manually selected alternative - switching java to auto mode
update-alternatives: removing /opt/java/jdk1.8.0_102/bin/java to provide /usr/bin/java (java) in manual mode
Display information about a specified command:
Code:
update-alternatives --display java
Motivation:
In a system with multiple versions of the same software, it is crucial to audit the current setup to understand which version is being used. The --display
option provides this overview for a specified command.
Explanation:
update-alternatives
: The command used for querying the alternatives system.--display
: Shows detailed information about the designated symbolic link’s available options and current settings.java
: The specific command to inspect.
Example Output:
java - auto mode
link best version is /usr/lib/jvm/java-11-openjdk-amd64/bin/java
link currently points to /usr/lib/jvm/java-11-openjdk-amd64/bin/java
link java is /usr/bin/java
slave java.1.gz is /usr/share/man/man1/java.1.gz
Display all commands and their current selection:
Code:
update-alternatives --get-selections
Motivation:
Administrators sometimes need a quick glance at all configured applications and which versions are set as defaults. This utilization of update-alternatives
provides a comprehensive overview of the current selections for all managed symbolic links.
Explanation:
update-alternatives
: The main command.--get-selections
: Displays a list of all managed symbolic links along with their current alternatives.
Example Output:
java manual /opt/java/jdk1.8.0_102/bin/java
javac manual /opt/java/jdk1.8.0_102/bin/javac
editor auto /usr/bin/vim.basic
x-www-browser auto /usr/bin/firefox
Conclusion:
The update-alternatives
command is an invaluable tool for system administrators and power users managing multiple versions of software in a Unix-like environment. Through its suite of options, it provides the flexibility to seamlessly control which version users access without altering individual paths or scripts, thereby ensuring system consistency and reducing potential conflicts.