Vagrant cannot forward the specified ports on this VM
Asked Answered
K

3

40

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?

Komatik answered 9/6, 2014 at 19:49 Comment(0)
H
89

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.

Hauberk answered 10/6, 2014 at 0:47 Comment(7)
This works, but it seems like one shouldn't have to jump through hoops like this to solve the basic problem of having two vm environments running at the same time. I'm confused as to why the action recommended by the original error message (changing the forwarded port in Vagrantfile) doesn't work.Charente
It's more VirtualBox's fault then Vagrant's. You cannot change port forwarding during the life of a VM. So when you pause a vm, it will release the socket, but when you resume it and that socket is taken, you cannot change it on the fly and need to restart VM.Hauberk
When I use vagrant reload, I get the same error message.Amphitryon
Just vagrant reload on both vagrant machines did the job for me!Nikolas
Thanks! I've added this info to the vagrant docs! See github.com/mitchellh/vagrant/pull/7299/filesSigmatism
Thanks, it helped me.Amp
Great, makes a ton of sense. Since when you use vagrant suspend you're keeping the machine state and when vagrant up again the machine will simply get the same openSSH configs (port 2222) that was previously set. Thanks this did the trick or me.Wylde
T
18

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.

Thyroxine answered 28/7, 2014 at 20:21 Comment(3)
Ok cool. Thanks for the information. It's definitely a good reference point.Komatik
Great answer, reload is simple solution, but this helps even with suspended machines. thanksTally
in my case vagrant reload done the trick... awesomeAmiamiable
W
0

My computer is Windows 10, and I solved this problem by disabling the 8080 port. because it is said that "the forwarded port 8080 is already in use on the host machine." So I edit the Vagrantfile and comment the port 8080.

Warner answered 1/1, 2023 at 9:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.