(guest@joequery.me)~ $ |

Vagrant SSH errors with multiple VMs: Port 2222 in use

My development machine contains multiple vagrant boxes. I recently had a need to run both virtual machines at once, and I encountered the following error when spinning up the second box:

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.

Since port 22 is used as your SSH port on your host machine, vagrant uses port 2222 by default for ssh communication between. (For the extra curious, here's where port 2222 is defined as the ssh default within the vagrant source)

Solution

The following solution worked for Vagrant version 1.7.2. If you use an older version of vagrant and upgrading is not an option, see the references at the bottom of the article.

The following Vagrantfile configuration directive will prevent the port forwarding conflict:

config.vm.network :forwarded_port, guest: 22, host: 2223, id: 'ssh'

The important thing here is to make the host option any value other than 2222. Additionally, if you plan on having more than 2 vagrant boxes up and running simultaneously, each host option will need to be unique across all Vagrantfiles.

References

Tagged as vagrant

Date published - May 13, 2015