Vagrant cannot forward the specified ports on this

2019-03-09 16:38发布

问题:

I have Vagrant in use for one box profile. Now I want to use Vagrant for another box (b2), but it says that bioiq's instance is consuming the forwarded port 2222 (which it is).

Now, if I configure b2 with the below, Vagrant still tries to use 2222.

Vagrant.configure("2") do |config|

  config.vm.box = 'precise32'
  config.vm.box_url = 'http://files.vagrantup.com/precise32.box'

  config.vm.network :forwarded_port, guest: 22, host: 2323

  # Neither of these fix my problem 
  # config.vm.network :private_network, type: :dhcp 
  # config.vm.network :private_network, ip: "10.0.0.200"
end

I've tried various ways from other SO questions to set the :forwarded_port (see here and here). I also tried this Google Group post, to no avail. I keep getting this message.

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 projects 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.

I don't know why Vagrant consistently ignores my directives. The posted configuration doesn't work. Has anyone overcome this?

回答1:

In case of ssh port, Vagrant solves port collisions by itself:

==> ubuntu64: Fixed port collision for 22 => 2222. Now on port 2200.

However, you still can create unavoidable collision by:

  1. Creating first vagrant env (it will get port 2222 for ssh)
  2. Suspend that env (vagrant suspend)
  3. Create second vagrant env (it will again get port 2222, since it is now unused)
  4. Try bringing first environment up again by vagrant up

You will get the error message you are getting now.

The solution is to use vagrant reload, to let vagrant discard virtual machine state (which means it will shut it down the hard way - so be careful if you have any unsaved work there) and start the environment again, solving any ssh port collisions on the way by itself.



回答2:

I've just run into a problem on current versions of Mac OSX (10.9.4) and VirtualBox (4.3.14) where the default ssh port 2222 is both unused and unbound by vagrant up. It was causing the sanity check ssh connection to timeout indefinitely.

This isn't the exact same problem, but an explicit forward resolved it:

config.vm.network :forwarded_port, guest: 22, host: 2201, id: "ssh", auto_correct: true

This suggestion comes from a comment on the Vagrant GitHub issue 1740.

It's not clear whether the port forwarded to 22 is being detected or if the ID is used, but it's working for me.



回答3:

If problem persists, check solution here for other Vagrantfiles being used:

How to debug "Vagrant cannot forward the specified ports on this VM" message