Vagrant: Port-forwarding collision when bringing up suspended instances

Created on 15 May 2013  路  10Comments  路  Source: hashicorp/vagrant

I'm still experiencing the #1472 issue when bringing up machines with saved state. The SSH forwarding ports collide and Vagrant bails. vagrant reload (or vagrant halt && vagrant up) gets it to detect and fix the port collision.


Environment:

  • OS X 10.8.3
  • Vagrant 1.2.2
  • VirtualBox 4.2.12 r84980.

Reproduction:

  1. Bring up A.
  2. Suspend A.
  3. Bring up B.
  4. Attempt to bring up A.

Result:

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.


Expected:

Vagrant notices the port collision and switches the port forwarding, allowing vm A to be brought up.

Most helpful comment

This configuration worked for me. I was able to start two different VMs from suspend using a different SSH host port.

# Share SSH locally by default
config.vm.network :forwarded_port,
  guest: 22,
  host: 2201,
  id: "ssh",
  auto_correct: true

https://groups.google.com/forum/#!topic/vagrant-up/HwqFegoCXOc

All 10 comments

I'm experiencing the same issue

OS X 10.8.3
Vagrant 1.2.2
VirtualBox 4.2.12 r84980

:+1:

Tangent: I even tried setting this up with config.vm.network :forwarded_port, host: 2200, guest: 22 and noticed that vagrant will ignore this directive (well, set it in addition to) the other ssh port.

Ah hah. So this is unfortunately working as intended for VirtualBox. VirtualBox doesn't support changing forwarded port mappings when the VM is not powered off. Therefore, you must do a full reload.

If you use the VMware provider, the forwarded ports will auto-correct because it does support changing these while the VM is suspended.

Is there any way to define this when creating the vm? I have two vm's for two different repos that need to be able to run at the same time, but setting the port as @searls tried doesn't work. Any way to change 2222 as the default port

I'm on the same boat with @searls and @flomotlik. Is there a way to change the SSH port?

This configuration worked for me. I was able to start two different VMs from suspend using a different SSH host port.

# Share SSH locally by default
config.vm.network :forwarded_port,
  guest: 22,
  host: 2201,
  id: "ssh",
  auto_correct: true

https://groups.google.com/forum/#!topic/vagrant-up/HwqFegoCXOc

That seems to work well. Thanks @dmp1ce!

For the record, I do seem to be able to delete and recreate port-forwards on suspended vm's with VirtualBox 4.3.
I'm not sure if there's more to it than that, but when Vagrant failed to resume I ran the modifyvm commands manually and was then able to resume:

$ VBoxManage modifyvm `cat .vagrant/machines/default/virtualbox/id` --natpf1 delete ssh
$ VBoxManage modifyvm `cat .vagrant/machines/default/virtualbox/id` --natpf1 'ssh,tcp,127.0.0.1,3207,,22'
$ VBoxManage --version
4.3.6r91406

I'm not sure I understand why vagrant can't change the port forwards for a suspended VirtualBox machine if the VBoxManage commands that @rwstauner lists above work. (Contrary to @mitchellh's report on July 23, 2013 before closing this thread, that is.)

Same issue on OSX. After you set the host port to something else in the Vagrantfile, it seems you need to run vagrant reload for it to take effect. Works for me now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jsirex picture jsirex  路  3Comments

barkingfoodog picture barkingfoodog  路  3Comments

StefanScherer picture StefanScherer  路  3Comments

luispabon picture luispabon  路  3Comments

janw-me picture janw-me  路  3Comments