Update-alternatives (or alternatives) in Linux

Source: Internet
Author: User

Alternatives and update-alternatives seem to be the same tool (different for each Linux distribution), so here's an example of update-alternatives. Update-alternatives is a very useful tool in Linux, not only in the Debian series, but also in the Redhat series.

Linux has grown to today, and there are so many available software. This will naturally have some software functions roughly the same. For example, the same editor, there are Nvi,vim,emacs,nano and so on. In most cases, such a functionally similar software is installed in the system at the same time, and can be executed with their name. For example, to perform VIM, simply type Vim under the terminal and press ENTER. However, in some cases we need to invoke one of these programs with a relatively fixed command. For example, when we write a script, just write down editor and don't want to worry about what the editor is. In Debian, for example, it provides a mechanism to solve this problem, and update-alternatives is used to implement this mechanism.

Before explaining the details of Update-alternatives, let's take a look at the examples already available in the system. Open the terminal and execute the following command:

As we can see, the executable command of editor is actually a symbolic connection, it points to/etc/alternatives/editor, and/etc/alternatives/editor is a symbolic connection, which points to/bin/nano. This way, when we enter editor and enter, the nano will be executed. The reason to bother to build such two links is to achieve the features mentioned above: Easy script programming and system management.

Let's look at the features of Update-alternatives. The first parameter to be introduced is --display . It allows us to see all the optional commands for a command. Execute code:

$ sudo update-alternatives--Display editor


You can see all the commands on my machine that can be used to link to the editor.

Let's talk about --config . This option allows us to select one of the commands, the code is as follows:

$ sudo update-alternatives--config editor

In the example above, I changed the ED to 1 for the Emacs24 Editor, which represents 3.

Speaking of which, we are going to introduce some concepts. First, update-alternatives is normally called automatically by installation scripts such as Postinst and prerm, so there are two types of alternative: automatic and manual. The initial state of each alternative is automatic, and if the system discovers that the administrator has manually modified a alternative, its status is automatically changed to manual, so that the installation script does not update it. If you want to change a alternative back to Automatic, just execute the code:

$ sudo update-alternatives--auto editor

You can do it. Let's take a look at the official command options:


Did you notice? I saw "name" on it. How do you write your name? This is the second concept we are going to introduce:

    • General name-this refers to a series of "common" names (including absolute paths) for programs with similar functions, such as/usr/bin/editor.

    • Link-this refers to the name of a alternative in/etc/alternative, such as editor.

    • Alternative-as implies, this refers to the path of an optional program (including an absolute path), such as/usr/bin/vim.

--auto,--display and--config are all with link. The third concept we want to talk about is priority. This is relatively simple, of course, the higher the priority of the program the better! The last concept is the Lord and slave alternative. Think about it, you link/usr/bin/editor to vim, but how do you feel when you see the manpage of Emacs when you execute the man editor? This leads to the concept of Lord and slave alternative: When the alternative of the master is updated, the alternative will be updated.

After saying these four important concepts, we introduce the other two options, others to see the document.

The first one is --install. Its format is:sudo update-alternatives--install gen link alt pri [--slave sgen slink Salt] ...

Gen,link,alt,pri is what we have said above. If you need to get from alternative, you can add--slave to the back. If you add a new alternatives to an already existing alternative group, the command adds the alternatives to the list of existing alternative groups and uses the new optional command as a new command; A new automatic alternative group will be set up.

If we add a wrong alternative. I don't want this alternative. In this case, you can execute the following command, the code example:

Update-alternatives--remove name Path

Name is a name in/etc/alternatives, which is the link above, and path is the absolute pathname of the optional program name that you want to delete (rest assured, this simply removes the program from the list and does not really remove the program's executable from the hard disk). If you remove a program that is being linked from a alternative group and the group is still not empty, Update-alternative automatically replaces the original program with an optional program with another priority. If this group becomes empty, then even the alternative group will be removed. If the deleted program is not linked, only the information about the program will be removed.

Note: The Update-alternatives tool is ideal for installing Java, especially for multiple versions of Java.



Update-alternatives (or alternatives) in Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.