How to Change the Symbolic Link Priority of Specific Command using update-alternatives

Posted on

This is another article related with the usage of ‘update-alternatives’ command. This command is normally available in the Linux-based operating system distribution. In this article, the usage of the ‘update-alternatives’ command is quite specific. The command ‘update-alternatives’ is also useful to change the symbolic link priority of the corresponding program location. It is useful to define a program name where there are many program path or location available. But there can be only one path or location that can represent the actual location of the program executed at a time.

What is the deciding factor for ‘update-alternatives’ tool to select the symbolic link to use ?. The answer is the priority value. Every symbolic link has its own priority value. Using the priority value, ‘update-alternatives’ tool decide which symbolic link to use. The symbolic link with the lowest priority value will be chosen by the ‘update-alternatives’ tool. Every time the program name executes, ‘update-alternatives’ tool will choose the symbolic link with the lowest priority value associated with that program name. In the end, the operating system will execute the program located in the path pointed by that symbolic link.

The following are steps for changing the priority of the symbolic link. It will affect ‘update-alternatives’ tool for choosing the symbolic link since the priority value changes. These steps are compatible in Debian or Ubuntu Linux-based operating system distribution :

1. Check the availability of ‘update-alternatives’ tool.

If the ‘update-alternatives’ tool is available, the command will also be available. Look in the article titled ‘How to Install update-alternatives tool for maintaining symbolic link of a command’ in this link to get more information about the availability of ‘update-alternatives’ tool.

2. List the symbolic link associated with that program name.

Execute the following command to list the symbolic link with the following pattern :

update-alternatives --display program_name

In this case, based on the above pattern for an example the command is :

root@hostname:~# update-alternatives --display java
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
/opt/jdk1.8.0_181/bin/java - priority 1
/usr/lib/jvm/java-11-openjdk-amd64/bin/java - priority 2
root@hostname:~# 

3. To demonstrate ‘update-alternatives’ tool for changing the priority of symbolic link, switch priority value between the two symbolic link listed above. The following is the pattern of the command :

update-alternatives --config program_name

4. Check the status of the symbolic link’s priority value in the current condition by typing the command as follows :

update-alternatives --display program_name

The example in this article is changing the symbolic link value for ‘java’ program :

root@hostname:~# update-alternatives --display java
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
/opt/jdk1.8.0_181/bin/java - priority 1
/usr/lib/jvm/java-11-openjdk-amd64/bin/java - priority 2
root@hostname:~#

5. Execute the following command according to the above pattern to change or to switch the priority value for an example :

root@hostname:~# update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).
  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   2         auto mode
  1            /opt/jdk1.8.0_181/bin/java                    1         manual mode
  2            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   2         manual mode
Press  to keep the current choice[*], or type selection number: 1
update-alternatives: using /opt/jdk1.8.0_181/bin/java to provide /usr/bin/java (java) in manual mode
root@hostname:~#

6. Recheck the value of symbolic link’s priority value after executing the previous step. If the command is successfully executed, the priority value of the symbolic link must be changed. Execute the following command according to the above pattern to change or to switch the priority value for an example :

root@hostname:~# update-alternatives --display java
java - manual mode
  link best version is /usr/lib/jvm/java-11-openjdk-amd64/bin/java
  link currently points to /opt/jdk1.8.0_181/bin/java
  link java is /usr/bin/java
/opt/jdk1.8.0_181/bin/java - priority 1
/usr/lib/jvm/java-11-openjdk-amd64/bin/java - priority 2
root@hostname:~# 

Leave a Reply