Understanding the alternatives Command
alternatives (or update-alternatives) is a GNU-licensed command-line tool that provides functionality to create, remove, manage, and query symbolic links. In other words, it allows defining default versions or paths for specific commands through symbolic links. Note that Debian-based Linux distributions only provide the update-alternatives command (update-alternatives was originally a Debian perl script, and Red Hat reimplemented it in C as alternatives to drop the perl dependency), and there are some differences in functionality compared to RedHat-based Linux commands, but this article covers only common functionality and options. Examples are based on RedHat.
For easier understanding, I’ll use Maven installation on the system and connecting it via symbolic links. Assume Maven is installed at /usr/lib/apache-maven-*. Of course, other applications are also possible.
Creating Symbolic Links (–install)
The --install action creates symbolic links. On RedHat, alternatives creates symbolic links in /etc/alternatives/<name> by default, and stores mode, priority, link, and path information in /var/lib/alternatives/<name>. When a symbolic link is created for the first time, a symbolic link to /etc/alternatives/<name> is created at the <link> path. (<link>->/etc/alternatives/<name>-><path>)
The --slave option is used to manage additional commands along with the master symbolic link. For example, when creating a symbolic link for the java command, you can also manage additional commands like javac, javadoc, etc. The --slave option can be defined multiple times.
| |
| |
Note: To change the paths where alternatives manages symbolic links and metadata, use the --altdir <directory> and --admindir <directory> command options.
| |
For example, when registering Java you can bundle the related commands together like this.
| |
Removing Symbolic Links (–remove)
The --remove action removes created symbolic links. Specifically, it removes from alternatives metadata, and if there are no connectable symbolic links within the <name> link group, both /etc/alternatives/<name> symbolic link and /var/lib/alternatives/<name> are also destroyed. If there are other alternatives, alternatives automatically updates to that link.
| |
| |
Setting Auto Mode (–auto)
The --auto action sets alternatives-registered symbolic links to be automatically selected within the link group. It preferentially selects the link with the highest <priority> value, and if the automatically selected link is removed, it connects to another alternative link.
| |
| |
Setting Manual Mode (–config)
The --config action allows users to arbitrarily select from alternatives-registered symbolic links within the link group.
| |
| |
Viewing Symbolic Link Groups (–display)
The --display action shows detailed information about alternatives-registered symbolic link groups. You can see command paths connected to master/slave symbolic links, current mode, currently selected link and other available links, and symbolic link priority values.
| |
| |
Viewing All Symbolic Link Groups (–list)
--list shows information about all symbolic links registered in alternatives.
| |
| |