Vagrant更改默認的SSH端口


 

Vagrant默認轉發宿主的2222端口到虛擬機的22端口(默認設置,無須配置)。在有多個虛擬機並存的情況下,2222端口將不好使。具體表現在:

  1. 啟動第二個虛擬機的時候,會報端口占用錯誤:
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider... Vagrant cannot forward the specified ports on this VM, since they would collide with some other application that is already listening on these ports. The forwarded port to 2222 is already in use on the host machine. To fix this, modify your current project's Vagrantfile to use another port. Example, where '1234' would be replaced by a unique host port:     config.vm.network :forwarded_port, guest: 22, host: 1234   Sometimes, Vagrant will attempt to auto-correct this for you. In this case, Vagrant was unable to. This is usually because the guest machine is in a state which doesn't allow modifying port forwarding. You could try 'vagrant reload' (equivalent of running a halt followed by an up) so vagrant can attempt to auto-correct this upon booting. Be warned that any unsaved work might be lost.
  1. 連接不同機器的SSH,會提示密鑰不符:
$ ssh vagrant@localhost -p2222 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!    @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the RSA key sent by the remote host is   SHA256:OmHXzdSe6B+pO6+xfvvFqKk40HAgzruemVeYbWYdF8c. Please contact your system administrator. Add correct host key in /home/tlanyan/.ssh/known_hosts to get rid of this message. Offending ECDSA key in /home/tlanyan/.ssh/known_hosts:6 RSA host key for [localhost]:2222 has changed and you have requested strict checking. Host key verification failed.

為了解決這個煩人的問題,只有為不同的虛擬機分配不同的主機端口。

根據提示,直接在Vagrantfile中更改轉發端口:

config.vm.network "forwarded_port", guest: 22, host: 3333

然后啟動機器,發現2222端口沒有直接更改為3333,而是新增了3333端口:

$ vagrant reload
==> default: Attempting graceful shutdown of VM...     default: Guest communication could not be established! This is usually because     default: SSH is not running, the authentication information was changed,     default: or some other networking issue. Vagrant will force halt, if     default: capable. ==> default: Forcing shutdown of VM... ==> default: Clearing any previously set forwarded ports... ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration...     default: Adapter 1: nat ==> default: Forwarding ports...     default: 22 (guest) => 3333 (host) (adapter 1)     default: 22 (guest) => 2222 (host) (adapter 1)     ....

沒辦法,只好上網查查怎么回事。在官方的GitHub上,有相關 issue ,標題很有意思:“give a chance to disable default ssh port forwarding”。根據回復,解決辦法是先禁用默認的ssh轉發,再添加自定義轉發:

config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true" config.vm.network "forwarded_port", guest: 22, host: 3333

注意:第一條規則必須有 id: "ssh" ,否則會報如下錯誤:

Forwarded port ‘2222’ (host port) is declared multiple times with the protocol ‘tcp’.

重啟后,即可看到轉發端口已經更改為3333。

參考

  1. https://www.vagrantup.com/docs/vagrantfile/machine_settings.html
  2. https://github.com/mitchellh/vagrant/issues/1922


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM