alternatives 與 update-alternatives

最後更新: 2021-08-09

介紹

 


update-alternatives (Debian)

 

可以有選擇的 Group 放在

/etc/alternatives

查看 editor 這 Group 有什麼選擇

update-alternatives --list editor

/bin/ed
/bin/nano
/usr/bin/vim.basic
/usr/bin/vim.tiny

查看 editor 這 Group 現在選了那 editor (vi, nano ...)

update-alternatives --display editor

editor - status is auto.
 link currently points to /bin/nano
/bin/ed - priority -100
 slave editor.1.gz: /usr/share/man/man1/ed.1.gz
/bin/nano - priority 40
 slave editor.1.gz: /usr/share/man/man1/nano.1.gz
/usr/bin/vim.tiny - priority 10

 * priority 大勝

選擇自己喜歡的 editor

update-alternatives --config editor

There are 5 alternatives which provide `editor'.

  Selection    Alternative
-----------------------------------------------
          1    /bin/ed
*+        2    /bin/nano
          3    /usr/bin/vim.tiny
          4    /usr/bin/vim.basic
          5    /usr/bin/mcedit-debian

* 是指 default, + 則是你選了什麼

--install 使用

查看 python3 有無用 alternatives

ll /usr/bin/python3

lrwxrwxrwx 1 root root 9 Dec 21  2013 /usr/bin/python3 -> python3.4*

ll /usr/bin/python3    # 使用了 alternatives

lrwxrwxrwx 1 root root 25 Mar  8 16:18 /usr/bin/python3 -> /etc/alternatives/python3

設定及使用

--install link name path priority

  • link        the generic name for the master link (在 /usr/bin 內的 link)
  • name     the name of its symlink in the alternatives directory
  • path       the alternative being introduced for the master link
  • priority   大勝

# 用回系統 Default 那個

update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.4 100

update-alternatives --install /usr/bin/python3 python3 /opt/py3.10/bin/python3.10 110

update-alternatives --list python3

/opt/py3.10/bin/python3.10
/usr/bin/python3.4 

P.S.

update-alternatives --install /usr/bin/pip3 pip3 /opt/py3.10/bin/pip3.10 110

/opt/py3.10/bin/python3.10 -m pip install --upgrade pip

pip3 --version

pip 23.0.1 from /opt/py3.10/lib/python3.10/site-packages/pip (python 3.10)

 


alternatives (Centos)

 

ls /etc/alternatives

lrwxrwxrwx 1 root root 27 Apr  8  2011 mta -> /usr/sbin/sendmail.sendmail
lrwxrwxrwx 1 root root 23 Apr  8  2011 mta-mailq -> /usr/bin/mailq.sendmail

Usage

alternatives --display <name>
alternatives --config                      # configuration menu for choosing the master link
alternatives --install
alternatives --remove
alternatives --set name path

Example

alternatives --display mta

mta - status is auto.
 link currently points to /usr/sbin/sendmail.sendmail
/usr/sbin/sendmail.sendmail - priority 90
 slave mta-pam: /etc/pam.d/smtp.sendmail
 slave mta-mailq: /usr/bin/mailq.sendmail
 slave mta-newaliases: /usr/bin/newaliases.sendmail
 slave mta-rmail: /usr/bin/rmail.sendmail
 slave mta-sendmail: /usr/lib/sendmail.sendmail
 slave mta-mailqman: /usr/share/man/man1/mailq.sendmail.1.gz
 slave mta-newaliasesman: /usr/share/man/man1/newaliases.sendmail.1.gz
 slave mta-aliasesman: /usr/share/man/man5/aliases.sendmail.5.gz
 slave mta-sendmailman: /usr/share/man/man8/sendmail.sendmail.8.gz
/usr/sbin/sendmail.postfix - priority 30
 slave mta-pam: /etc/pam.d/smtp.postfix
 slave mta-mailq: /usr/bin/mailq.postfix
 slave mta-newaliases: /usr/bin/newaliases.postfix
 slave mta-rmail: /usr/bin/rmail.postfix
 slave mta-sendmail: /usr/lib/sendmail.postfix
 slave mta-mailqman: /usr/share/man/man1/mailq.postfix.1.gz
 slave mta-newaliasesman: /usr/share/man/man1/newaliases.postfix.1.gz
 slave mta-aliasesman: /usr/share/man/man5/aliases.postfix.5.gz
 slave mta-sendmailman: /usr/share/man/man1/sendmail.postfix.1.gz
Current `best' version is /usr/sbin/sendmail.sendmail.

alternatives --config mta

There are 2 programs which provide 'mta'.

  Selection    Command
-----------------------------------------------
*  1           /usr/sbin/sendmail.sendmail
 + 2           /usr/sbin/sendmail.postfix

Enter to keep the current selection[+], or type selection number:

設定後:

lrwxrwxrwx 1 root root 26 Sep 16 12:19 mta -> /usr/sbin/sendmail.postfix
lrwxrwxrwx 1 root root 22 Sep 16 12:19 mta-mailq -> /usr/bin/mailq.postfix

# alternatives --install link name path priority

  • name is the generic name for the master link,
  • link is the name of its symlink
  • path is the alternative being introduced for the master link

alternatives --install /usr/sbin/sendmail mta /usr/sbin/sendmail.postfix 30

alternatives --set mta /usr/sbin/sendmail.postfix

alternatives --auto mta

 

 


Centos Change Default Editor

 

i.e.

cat <<EOF >>/etc/profile.d/nano.sh
export VISUAL="vim"
export EDITOR="vim"
EOF

 

Creative Commons license icon Creative Commons license icon