What Is the Alternatives System?
alternatives creates, removes, maintains, and displays information about the symbolic links comprising the alternatives system. In a way, the alternatives system is a more maintainable way to manage symbolic links throughout your system.
It is possible for several programs fulfilling the same or similar functions to be installed on a single system at the same time. For example, many systems have several text editors installed at once. This gives choice to the users of a system, allowing each to use a different editor if desired, but makes it difficult for a program to make a good choice of editor to invoke if the user has not specified a particular preference.
The alternatives system aims to solve this problem. A generic name in the filesystem is shared by all files providing interchangeable functionality. The alternatives system and the system administrator together determine which actual file is referenced by this generic name. For example, if the text editors ed(1) and nvi(1) are both installed on the system, the alternatives system will cause the generic name /usr/bin/editor to refer to /usr/bin/nvi by default. The system administrator can override this and cause it to refer to /usr/bin/ed instead, and the alternatives system will not alter this setting until explicitly requested to do so.
The generic name is not a direct symbolic link to the selected alternative. Instead, it is a symbolic link to a name in the alternatives directory (/etc/alternatives/), which in turn is a symbolic link to the actual executable file referenced.
Example:
For the purpose of this guide, I will choose emacs as the program that we will want to create an alternatives link for. There are two different versions of the emacs text editor. We have the generic emacs, "a powerful, customizable, self-documenting, modeless text editor", which in a way does not really need an introduction. And also we have a more minimal and server-friendly version of emacs, called emacs-nox, which is a less bloated version of emacs for running on a server and in the terminal with no X-Windows support.
If for any reason, you need to use emacs on a server, instead of vi, then you would probably need to use emacs-nox since it is smaller and more server-friendly. (In general, using emacs on a server is not recommended). However, you wish to keep the generic name /usr/bin/emacs in case other programs want to refer to it, but you also want this generic name to point to emacs-nox executable and not the generic, more bloated emacs. Here is where the alternatives system comes into play. Using this system, you can keep the generic name emacs but still choose to use the more desirable executable provided by the emacs-nox package. Here is how it is done.
Install emacs-nox:
Run the following command to install the emacs-nox package on your system:
yum install emacs-nox
And then run this to make sure that an alternative is not already configured to link the generic name emacs to the emacs-nox executable:
/usr/sbin/alternatives --remove emacs /usr/bin/emacs-24.3-nox
Now if you try to run emacs, you will get this error:
emacs
-bash: /usr/bin/emacs: No such file or directory
This simply means that the generic name emacs is not yet pointing to any executable. So let's make sure that we will point it to the emacs-nox executable which is located here /usr/bin/emacs-24.3-nox.
Creating The Symlink:
The command to create such symlink has this general format:
/usr/sbin/alternatives --install <link> <name> <path> <priority>
This command has four parts/arguments (Highlighted in red):
• <link> A location for the "generic" symlink. In our case /usr/bin/emacs
• <name> A name for the alternative. You can use anything here, but to make it more descriptive I will simply put "emacs".
• <path> The binary to be executed when this symlink is called. We want /usr/bin/emacs-24.3-nox to be our executable program.
• <priority> A priority level to indicate which alternative is preferred. This is more for when there are multiple executables configured for a generic link. In those cases, you can give priority to different executables using this number, but since this is a required value, so we need to provide a number here to ensure the command runs successfully. I will give this value an arbitrarily high number like 100.
So now our command would look like this:
/usr/sbin/alternatives --install /usr/bin/emacs emacs /usr/bin/emacs-24.3-nox 100
After running the above command, you should be able to run emacs by simply calling the generic name "emacs". Now if you look at the generic link /usr/bin/emacs and see where does it point to you will see this relationship:
/usr/bin/emacs -> /etc/alternatives/emacs* -> /usr/bin/emacs-24.3-nox*
This means that now you have successfully configured emacs to point to emacs-24.3-nox executable using the alternatives system. You can confirm the same information using these two commands:
/usr/sbin/alternatives --list | grep -Ei emacs
emacs manual /usr/bin/emacs-24.3-nox
/usr/sbin/alternatives --config emacs
There is 1 program that provides 'emacs'.
Selection Command
-----------------------------------------------
*+ 1 /usr/bin/emacs-24.3-nox
Why Alternatives?
You might ask, why not simply create a symlink or modify the environment variables to achieve the same result? Although that's a legitimate question and in certain cases, those solutions might be preferable, the alternatives system is much more than simple symlink creation.
The alternatives command has many more features, including the ability to symlink dependent components when a specific alternative is chosen, creating priority-based symlinks, creating group symlinks, and much more. In addition to handling a lot of the heavy lifting, the alternatives system keeps your inconsistencies centralized. You can check in on what alternatives are in place, so you don't have to remember from day to day that emacs is actually emacs-24.3-nox, or that python isn't /usr/bin/python3.6 but /usr/bin/python2.7, and so on. All you have to do is just run the following command and see all the symlinks on your system in one place:
/usr/sbin/alternatives --list
Or if you want a more visually pleasing format, then you can run this command instead:
tree -a -L 4 -CA /etc/alternatives/
/etc/alternatives/
├── emacs -> /usr/bin/emacs-24.3-nox
├── emacs.etags -> /usr/bin/etags.emacs
├── emacs.etags.man -> /usr/share/man/man1/etags.emacs.1.gz
├── ld -> /usr/bin/ld.bfd
├── libnssckbi.so.x86_64 -> /usr/lib64/pkcs11/p11-kit-trust.so
├── links -> /usr/bin/elinks
├── links-man -> /usr/share/man/man1/elinks.1.gz
├── mta -> /usr/sbin/sendmail.postfix
├── mta-aliasesman -> /usr/share/man/man5/aliases.postfix.5.gz
├── mta-mailq -> /usr/bin/mailq.postfix
├── mta-mailqman -> /usr/share/man/man1/mailq.postfix.1.gz
├── mta-newaliases -> /usr/bin/newaliases.postfix
├── mta-newaliasesman -> /usr/share/man/man1/newaliases.postfix.1.gz
├── mta-pam -> /etc/pam.d/smtp.postfix
├── mta-rmail -> /usr/bin/rmail.postfix
├── mta-sendmail -> /usr/lib/sendmail.postfix
├── mta-sendmailman -> /usr/share/man/man1/sendmail.postfix.1.gz
├── ncman -> /usr/share/man/man1/ncat.1.gz
└── nmap -> /usr/bin/ncat
Comments
0 comments
Article is closed for comments.