Ubuntu中update-alternatives命令(版本切换)

时间:2022-12-11 15:19:32

1、update-alternatives 命令选项介绍:display、install、remove、config


2、display命令:(查看命令链接信息)

# update-alternatives --display name(命令名称)

$ update-alternatives --display gcc
gcc - auto mode
link currently points to /usr/bin/gcc-4.7
/usr/bin/gcc-4.7 - priority 100
/usr/bin/gcc-4.8 - priority 50
Current 'best' version is '/usr/bin/gcc-4.7'.

3、install命令:(增加命令链接)

# update-alternatives --install link name path priority
# link -> 链接原目录 name -> 链接符名称
# path -> 链接新目录 priority -> 优先级

$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.7 100
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50

$ update-alternatives --display gcc
gcc - auto mode
link currently points to /usr/bin/gcc-4.7
/usr/bin/gcc-4.7 - priority 100
/usr/bin/gcc-4.8 - priority 50
Current 'best' version is '/usr/bin/gcc-4.7'.

4、remove命令:(删除命令链接)

# update-alternatives --remove name path
# name, path 与 install命令一样

$ sudo update-alternatives --remove g++ /usr/bin/g++-4.8

$ update-alternatives --display gcc
gcc - auto mode
link currently points to /usr/bin/gcc-4.7
/usr/bin/gcc-4.7 - priority 100
Current 'best' version is '/usr/bin/gcc-4.7'.

5、config命令:(配置命令链接)

# update-alternatives --config name

$ update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

Selection Path Priority Status
------------------------------------------------------------

* 0 /usr/bin/gcc-4.7 100 auto mode
1 /usr/bin/gcc-4.7 100 manual mode
2 /usr/bin/gcc-4.8 50 manual mode

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

6、auto和manual模式转换
默认为auto模式,将manual模式改为auto模式:

# update-alternatives --auto name
$ update-alternatives --auto gcc