windows环境创建多台虚拟机Vagrant

#Vagrant默认转发宿主的2222端口到虚拟机的22端口(默认设置,无须配置)。
在有多个虚拟机并存的情况下,2222端口将不好使。具体表现在:
启动第二个虚拟机的时候,会报端口占用错误:
$ 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.

连接不同机器的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。
参考
https://www.vagrantup.com/docs/vagrantfile/machine_settings.html
https://github.com/mitchellh/vagrant/issues/1922
转载:https://ssrvps.org/archives/4573

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值