I have vagrant vm centos 7 running ssh on XXXX port (not default 22)
How can I connect to XXXX port using “vagrant ssh” command
I tried this but did not work.
config.vm.network :forwarded_port, guest: 22, host: 2222, id: "ssh" , disabled: true
config.vm.network :forwarded_port, guest: xxxx, host: 2223, auto_correct: true
config.ssh.port = 2223
2
Answers
It might depend on the provider you’re using, but setting
config.ssh.guest_port=XXXX
along with a port forwarding does the trick for me when using Virtualbox as a provider.With that you shouldn’t even have to specify
config.ssh.port
, as Vagrant will detect the port forwarding settings automatically.See also vagrantfile documentation
Wandering what configuration @su_li used exactly, I tried different possibilities and the following code came to work as expected :
The guest OS (the VM) runs sshd on port 54321.
The host OS will send ssh request to port 2222.
With this configuration, all requests to port 2222 on the host side will be forwarded to port 54321 on the guest side.
And all responses from port 54321 from the guest side will be forwarded to port 2222 on the host side.
Note that the
id: "ssh"
part is necessary if you want to override the default ssh port forwarding configuration.