Solve "[email protected]: Permission denied (publickey)" error

Published at 08 Oct 2024

When we move a Vagrant VM from one computer to another, we encounter the "Permission denied (publickey)" error.

This error occurs when Vagrant cannot access the guest machine using its SSH private key.

To solve this we should go to the folder where the Vagrantfile is located, next we should access the following folders.

Note: The path might be slightly different if you're not using VirtualBox as your provider.

.vagrant\machines\default\virtualbox

In this folder you will find a file named "private_key", you should delete it.

Next, open your provider software, Virtualbox, and boot your VM.

Once it's booted, access the terminal and execute the following commands:

cd ~/.ssh
curl https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub > authorized_keys

This will allow using the Vagrant's default public key when accessing the guest.

Let's try it, we execute the following Vagrant command:

vagrant reload

Once Vagrant tries to boot the guest, a new SSH key will be generated in order to replace the default one.

With these few steps, we will be able to use as usual our guest machine.