How to use the command update-alternatives (with examples)

How to use the command update-alternatives (with examples)

The update-alternatives command is a convenient tool for maintaining symbolic links to determine default commands. It allows users to easily configure and manage symbolic links for different versions of a program or command. This can be useful when multiple versions of a command are installed on a system and the user wants to switch between them.

Code:

sudo update-alternatives --install /usr/bin/java java /opt/java/jdk1.8.0_102/bin/java 1

Motivation: This use case is used to add a symbolic link for the java command, pointing it to the specified location /opt/java/jdk1.8.0_102/bin/java. This allows the user to easily switch between different versions of Java on their system.

Explanation:

  • sudo: This command is used to run the update-alternatives command with administrative privileges.
  • --install: This option specifies that we want to add a symbolic link.
  • /usr/bin/java: This is the path to the symlink that will be created.
  • java: This is the command name of the symlink.
  • /opt/java/jdk1.8.0_102/bin/java: This is the path to the actual command binary.
  • 1: This is the priority of the symlink. In case there are multiple symlinks for the same command, the one with the highest priority will be the default.

Example output:

update-alternatives: using /opt/java/jdk1.8.0_102/bin/java to provide /usr/bin/java (java) in auto mode

Code:

sudo update-alternatives --config java

Motivation: This use case is used to configure the symbolic link for the java command. It allows the user to select the desired version of Java from the available options.

Explanation:

  • sudo: This command is used to run the update-alternatives command with administrative privileges.
  • --config: This option is used to configure the selected symbolic link.

Example output:

There are 3 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   1111      manual mode
  3            /usr/lib/jvm/java-8-openjdk-amd64/bin/java    1081      manual mode

Press <enter> to keep the current choice[*], or type selection number:

Code:

sudo update-alternatives --remove java /opt/java/jdk1.8.0_102/bin/java

Motivation: This use case is used to remove the symbolic link for the java command. This can be useful when a particular version of Java is no longer needed and the user wants to remove it from the system.

Explanation:

  • sudo: This command is used to run the update-alternatives command with administrative privileges.
  • --remove: This option is used to remove the specified symbolic link.
  • java: This is the command name of the symlink.
  • /opt/java/jdk1.8.0_102/bin/java: This is the path to the actual command binary.

Example output:

No output will be displayed if the symbolic link is successfully removed.

Use case 4: Display information about a specified command

Code:

update-alternatives --display java

Motivation: This use case is used to display detailed information about the symbolic link for the java command. It provides useful information like the command name, path to the symlink, and the currently selected alternative.

Explanation:

  • --display: This option is used to display detailed information about the specified command.
  • java: This is the command name of the symlink.

Example output:

java - auto mode
  link best version is /opt/java/jdk1.8.0_102/bin/java
  link currently points to /opt/java/jdk1.8.0_102/bin/java
  link java is /usr/bin/java
  slave java.1.gz is /usr/share/man/man1/java.1.gz
/usr/lib/jvm/java-11-openjdk-amd64/bin/java - priority 1111
/usr/lib/jvm/java-8-openjdk-amd64/bin/java - priority 1081
/opt/java/jdk1.8.0_102/bin/java - priority 1

Use case 5: Display all commands and their current selection

Code:

update-alternatives --get-selections

Motivation: This use case is used to display all the commands managed by update-alternatives and their currently selected symbolic link.

Explanation:

  • --get-selections: This option is used to display all the managed commands and their currently selected symbolic link.

Example output:

java                     /opt/java/jdk1.8.0_102/bin/java
javac                    /opt/java/jdk1.8.0_102/bin/javac

Conclusion:

The update-alternatives command is a powerful tool for managing and configuring symbolic links for default commands on a Linux system. It allows users to easily switch between different versions of a command and provides a convenient way to add, remove, and display information about symbolic links. By using this command, users can have more control over their command line environment and select the desired version of a command for their needs.

Related Posts

How to use the command ruget (with examples)

How to use the command ruget (with examples)

The ruget command is an alternative to wget written in Rust.

Read More
How to use the command `hdparm` (with examples)

How to use the command `hdparm` (with examples)

The hdparm command is used to get and set SATA and IDE hard drive parameters.

Read More
How to use the command git annex (with examples)

How to use the command git annex (with examples)

Git Annex is a command-line tool that allows users to manage files with Git without checking their contents in.

Read More