使用 update-alternatives 设置默认程序版本

Kevin 教程 1,177 次浏览 31条评论

update-alternatives 是 dpkg 包管理器的其中一个工具,其功能旨在解决默认首选程序的问题。在系统同时安装多个相同功能的程序情况下,可以按需设置指定程序及版本为默认。

update-alternatives 通过创建维护符号链接来确定命令关联程序。默认程序的确定分为自动和手动两种模式。自动模式根据程序自带优先级参数选择(由开发者设定),手动模式由用户自行设置切换。

update-alternatives 用法

update-alternatives [<选项> ...] <命令>
命令参数 执行操作
–install <链接> <名称> <路径> <优先级>
[–slave <链接> <名称> <路径>]
创建程序候选组,添加候选程序参数
<链接>是指向/etc/alternatives/<名称>的符号链接
(例如/usr/bin/pager)
<名称>是该链接组的主名称 (例如pager)
<路径>是程序目标文件的位置 (例如/usr/bin/less)
<优先级>是一个整数,自动模式下数字越高程序优先级越高
–slave 是一个可选项参数,用于设置程序关联的次要链接
–remove <名称> <路径> 从<名称>候选组中删除<路径>项
–remove-all <名称> 删除<名称>候选组记录
–auto <名称> 将<名称>的主链接切换到自动模式
–display <名称> 显示有关<名称>候选组的信息
–query <名称> 机器可读版的 –display <名称>
–list <名称> 显示<名称> 候选组里的所有可用候选项
–get-selections 列出主候选项名称和它们的状态
–set-selections 从标准输入中读取候选项的状态
–config <名称> 列出<名称>候选组中的可选项,并由用户选择要使用哪一个。选择后,链接组将被设置为手动模式
–set <名称> <路径> 将<路径>设置为<名称>的候选项,同时候选组设置为手动模式
–all 对所有选项调用 –config 命令
选项参数 功能作用
–altdir <目录> 更改默认/etc/alternatives/候选项目录
–admindir <目录> 更改默认/var/lib/alternatives/管理目录
–log <文件> 更改日志文件
–force 允许强制用替代链接替换文件
–skip-auto 自动模式下跳过设置正确候选项的提示 (仅与 –config 相关)
–verbose 输出详细操作信息
–quiet 安静模式,不输出信息 (除非出现错误)
–help 显示 update-alternatives 帮助信息
–version 显示 update-alternatives 版本信息

设置默认程序示例

以设置默认文本编辑器为例。先看下editor可执行命令的链接目录情况。

# ls -l /usr/bin/editor
lrwxrwxrwx 1 root root 24 Jan  8 16:22 /usr/bin/editor -> /etc/alternatives/editor
# ls -l /etc/alternatives/editor
lrwxrwxrwx 1 root root 9 Jan  8 16:22 /etc/alternatives/editor -> /bin/nano

上面依次查询的命令最终指向默认编辑器的可执行文件目录,结果为nano编辑器。

使用update-alternatives --display editor命令可以查看当前editor程序候选项的相关参数。

选择切换editor默认程序。下面演示将默认编辑器改为Vim。

# update-alternatives --config editor
There are 3 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /bin/nano            40        auto mode
  1            /bin/nano            40        manual mode
  2            /usr/bin/vim.basic   30        manual mode
  3            /usr/bin/vim.tiny    15        manual mode

Press  to keep the current choice[*], or type selection number: 2    
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in manual mode

切换程序版本示例

首先补充一下,有些程序未必会显示在候选列表里,例如 Python,查询提示不存在。

# update-alternatives --display python
update-alternatives: error: no alternatives for python

需要手动创建python候选组,然后添加已经安装的程序版本,使用--install命令,具体如下。

# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 20
# update-alternatives --install /usr/bin/python python /usr/bin/python3.5 10

添加后再运行update-alternatives --config python命令切换程序版本。

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python2.7   20        auto mode
  1            /usr/bin/python2.7   20        manual mode
  2            /usr/bin/python3.5   10        manual mode

选择后查询是否已经切换成功。

# python --version
Python 3.5.3

评论列表

  1. #1

    Hi, very nice website, cheers!
    ——————————————————
    Need cheap and reliable hosting? Our shared plans start at $10 for an year and VPS plans for $6/Mo.
    ——————————————————
    Check here: https://www.reliable-webhosting.com/

  2. #2

    which pills contains dapoxetine https://dapoxetine.confrancisyalgomas.com/

  3. #3

    does naltrexone need a prescription https://naltrexoneonline.confrancisyalgomas.com/

  4. #4

    dapoxetine brand in india https://salemeds24.wixsite.com/dapoxetine

  5. #5

    careprost not working anymore https://carepro1st.com/

  6. #6

    price for vidalista at cvs https://vidalista.mlsmalta.com/

  7. #7

    cialis tadalafil http://www.lm360.us/

  8. #8

    information on hydroxychloroquine https://hydroxychloroquine.mlsmalta.com/

  9. #9

    stromectol pills https://ivermectin.webbfenix.com/

  10. #10

    doctors say hydroxychloroquine works https://hydroxychloroquine.webbfenix.com/

  11. #11

    ivermectin human use https://ivermectin.mlsmalta.com/

  12. #12

    generic of viagra http://droga5.net/

  13. #13

    max vidalista dose 40 mg https://vidalista40mg.mlsmalta.com/

  14. #14

    visit doctor without insurance https://medpills.bee-rich.com/

  15. #15

    visit doctor without insurance https://edmeds.buszcentrum.com/

  16. #16

    viagra priligy levitra online https://ddapoxetine.com/

  17. #17

    albuterol sulfate hfa inhaler cost https://amstyles.com/

  18. #18

    generic cialis capsules https://wisig.org/

  19. #19

    aralen 250 mg tablets https://hydroxychloroquinee.com/

  20. #20

    hydroxychloroquine risks and side effects https://hydroxychloroquine.lm360.us/

  21. #21

    hydroxychloroquine risks and side effects https://hydroxychloroquine.wisig.org/

  22. #22

    cenforce sildenafil citrate tablets http://cavalrymenforromney.com/

  23. #23

    cialis generic tadalafil walmart https://atadalafil.online/

  24. #24

    cialis generic tadalafil walmart https://tadalafil24.online/

  25. #25

    ivermectin tablets for sale https://ivermectin1st.com/

  26. #26

    I needed to draft you one little bit of observation so as to say thank you once again for those incredible opinions you’ve documented here. It is quite pretty generous of people like you to deliver freely exactly what a number of people could possibly have made available as an e book to end up making some cash for themselves, most importantly given that you could possibly have done it if you ever desired. Those suggestions in addition worked to become good way to realize that some people have the identical keenness similar to my personal own to realize significantly more when it comes to this matter. I believe there are several more pleasant sessions up front for people who scan your site. https://gemerekliler.com/

  27. #27

    I intended to compose you a bit of note in order to give many thanks again considering the breathtaking concepts you’ve contributed above. It’s extremely generous of people like you in giving easily all a few people could have advertised as an e-book in order to make some bucks for their own end, certainly considering that you could have done it if you considered necessary. The concepts additionally acted as the easy way to be certain that many people have a similar dream just as mine to find out a little more concerning this condition. Certainly there are a lot more enjoyable instances ahead for folks who discover your blog. https://jlsmithjax.com/

  28. #28

    cost of cialis daily dose https://cialzi.com/

  29. #29

    plaquenil hydroxychloroquine who manufactures https://hhydroxychloroquine.com/

  30. #30

    Write extra; that’s all I include to say. It looks as though you counted on the video to be able to make your point. An individual really know what you’re discussing, why waste your intellect on just posting video tutorials to your blog page if you may be giving people something enlightening to examine?
    Life Experience Degrees

  31. #31

    Hi my loved one! I wish to say that this post is amazing, great written and include approximately all important infos.

    I’d like to look more posts like this. Life Experience Degree

发表评论

电邮地址用于 Gravatar 头像显示,不会被公开可见。