Vagrant has set the ssh port forwarding to host 2222 by default, but for a private network set up, is this really necessary?
It causes more trouble than it gives, I am using the docker provider, and it can not easily change the port forwarding for a running container, I often get into a situation where two containers both have a port forwarding set to 2222, and only one can start. Actually, the 2222 port is never used, so can this default be removed for a private networking setup?
Hi @weynhamz
Vagrant uses SSH (or WinRM for Windows) to communicate with the VM for nearly everything beyond the initial boot. You can configure SSH forwarding in the Vagrantfile: https://docs.vagrantup.com/v2/vagrantfile/ssh_settings.html
@sethvargo Hi, I don't want to change the forwarding to anything else, I just want to disable the port forwarding completely, but I still want to be able to ssh into the guest also I want the vagrant be able to ssh into the guest to do provision, is that possible?
Hi @weynhamz
You can set disabled: true on the forwarded port:
config.vm.network :forwarded_port, guest: 22, disabled: true
Hi @sethvargo
I have tried this setting
config.vm.network :forwarded_port, guest: 22, disabled: true
it gives
vm:
* Forwarded port definitions require a "host" and "guest" value
* Ports to forward must be 1 to 65535
then, I changed it to
config.vm.network :forwarded_port, guest: 22, host: 2222, disabled: true
it gives
vm:
* Forwarded port '2222' (host port) is declared multiple times
with the protocol 'tcp'.
then I changed it to
docker.vm.network :forwarded_port, guest: 22, host: 2222, id: "ssh", disabled: true
It starts with no error, but the default ssh port forwarding is still there
Any hint?
By the way, I tested this with docker provider, so like I said I am looking for a way to completely disable this default ssh port forwarding.
It seems that virtualbox provider requires a default ssh port forwarding to be set up in order to login into the virtual machine, but for a docker command, the port forwarding is not necessary and even a bit redundant, so Is that possible to make the ssh port forwarding default to the virtualbox provider, but not the other providers?
Some info for anyone else landing to this page...
$ vagrant --version
Vagrant 1.8.6
$ docker --version
Docker version 1.13.1, build 092cba3
Trying to setup a Vagrantfile to work with both VMs and docker containers, so for the docker provider I don't want to disable ssh port forwarding but I just need to be able to change the default 2222.
I have tried config.vm.network :forwarded_port, guest: 22, host: 2222, id: 'ssh', disabled: true and played around with config.ssh.port, config.ssh.guest_port but 2222 is still used.
Also config.ssh.private_key_path does not seem to have any effect, it always tries to use paswword authentication.
For anyone interested I've quickly worked around the above issues with:
#add this to the Dockerfile
RUN echo 'vagrant:vagrant' | chpasswd
# add this to Vagrantfile
node.vm.provider 'docker' do |d|
d.image = 'my-vagrant-centos'
d.has_ssh = true
d.privileged = true
end
node.ssh.username = 'vagrant'
node.ssh.private_key_path = '/home/acr/w/bsf/bsf-elk-vagrant/ssh/id_rsa'
#set has_ssh = false
vagrant up node1 --no-provision
#set has_ssh = true
vagrant reload node1
#container is created, vagrant just stop/starting it => no more 2222
vagrant provision node1
Is there a better way to have multi-docker Vagrant file working correctly?
Is the docker provider really used? I am getting the feeling that there is not much community around vagrant+docker-provider setup.
It seems that still no right answer in this issue.
I tried this code and it works fine:
config.vm.network "forwarded_port", guest: 22, host: your_port, id: "ssh"
I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Most helpful comment
It seems that still no right answer in this issue.
I tried this code and it works fine:
config.vm.network "forwarded_port", guest: 22, host: your_port, id: "ssh"