4
votes

I'm trying to set up two VMs with Vagrant, and one of them needs to be able to ssh into the second one using the private key.

I defined both VMs inside one vagrantfile, started them up and then copied the private key from the .vagrant directory into my first VM. I was then able to ssh into the second VM without a password.

The problem is that this is not stable, Vagrant recycles this key each time I do vagrant up and creates a new one. This obviously breaks the ability to ssh from the first VM to the second one until I copy the private key again.

I don't particularly care which key exactly Vagrant uses, I just want it to be stable so that I can always ssh between my VMs. How can I do that?

1

1 Answers

1
votes

You have 2 possibility to do that :

  1. have a shell provisioner to run every time you boot the VM to copy the new generated key, so basically every time you run vagrant up you will copy the ssh key into the VM so you will still be able to ssh into the second VM.

  2. force vagrant to use a key, add the following parameter to your Vagrantfile where you will store your own custom key

    config.ssh.private_key_path = "custom_key_file"
    config.ssh.forward_agent = true
    

so Vagrant will use this key file when it boots the VM and you can copy this key into your VM to ssh into the second