Overriding the Default Forwarded SSH Port in Vagrant
TLDR:
|
|
What’s the problem? If running multiple virtual machines managed by Vagrant, SSH port collision will happen:
|
|
The example is correct but incomplete. By default the SSH port (22) on the guest machine is forwarded to port 2222 on the host machine:
|
|
Changing to another port in Vagrantfile does not solve the problem. For example, switching from 2222 to 2322:
|
|
In this situation, you are forwarding both ports 2222 and 2322 to the SSH port on the guest machine, as shown in VirtualBox port forwarding settings:

When bringing up the machine, there will be multiple host machine ports forwarding to the same SSH port.
|
|
The SSH configuration shows the same outcome:
|
|
Of course, you can do:
|
|
This works, but we like the simple vagrant ssh command, and we just want to retain a single port 2322.
The solution is appending id: "ssh", and it must be specified in order to override the default forwarded SSH port. See < https://github.com/mitchellh/vagrant/issues/3232#issuecomment-39354659>:
|
|
After appending id: "ssh", the SSH configuration has also been updated:
|
|
Now, you are good to go:
|
|