Vagrant: How to Change the Default SSH Port

 By default, vagrant forwards port 2222 of the host to port 22 of the virtual machine (default setting, no need to configure). In the case of multiple virtual machines, port 2222 will not work well. The specific performance is as follows

When starting the second virtual machine, port occupancy error will be reported:

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider... 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 project's 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. You could try 'vagrant reload' (equivalent of running a halt followed by an up) so vagrant can attempt to auto-correct this upon booting. Be warned that any unsaved work might be lost.

When SSH is connected to different machines, you will be prompted that the key does not match:

$ ssh [email protected] -p2222
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @WARNING: REMOTE HOST IDENTIFICATION HAS [email protected] @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the RSA key sent by the remote host is SHA256:OmHXzdSe6B+pO6+xfvvFqKk40HAgzruemVeYbWYdF8c. Please contact your system administrator. Add correct host key in /home/tlanyan/.ssh/known_hosts to get rid of this message. Offending ECDSA key in /home/tlanyan/.ssh/known_hosts:6 RSA host key for [localhost]:2222 has changed and you have requested strict checking. Host key verification failed.

To solve this annoying problem, we have to assign different host ports to different virtual machines

According to the prompt, change the forwarding port directly in the vagrantfile:

config.vm.network "forwarded_port", guest: 22, host: 3333

Then start the machine and find that port 2222 is not directly changed to port 3333, but a new port 3333 is added

$ vagrant reload
==> default: Attempting graceful shutdown of VM... default: Guest communication could not be established! This is usually because default: SSH is not running, the authentication information was changed, default: or some other networking issue. Vagrant will force halt, if default: capable. ==> default: Forcing shutdown of VM... ==> default: Clearing any previously set forwarded ports... ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 (guest) => 3333 (host) (adapter 1) default: 22 (guest) => 2222 (host) (adapter 1) ....

I have no choice but to go online to find out what’s going on. On the official GitHub, there is a related issue with an interesting title: “give a chance to disable default SSH port forwarding”. According to the reply, the solution is to disable the default SSH forwarding first, and then add custom forwarding:

config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true" config.vm.network "forwarded_port", guest: 22, host: 3333

Note: The first rule must have id: "ssh", otherwise the following error will be reported.

Forwarded port ‘2222’ (host port) is declared multiple times with the protocol ‘tcp ‘.

After reboot, you can see that the forwarded port has been changed to 3333.

 

Similar Posts: