Vagrant: vagrant@ Permission denied (publickey)

Submitted by oioioooi on 29/11/2023 - 12:29

There are a number of solutions on the Internet, however here goes the one that helped in my case.

Make sure that the private key used to ssh to the guest machine has correct line endings. This affects in most cases Windows machines as the key is created using DOS line engings, but must use UNIX line endings.

This is fixed in vagrant 2.4.1. Update your vagrant executable or read below.

The private key we need is located in same directory as the Vagrantfile.

Navigate to the directory where the vagrant machine files are placed and open the key file. I'm using Cygwin and Vim for that purpose (use favorite editor that is able to change line endings):

$ vi .vagrant/machines/eo/virtualbox/private_key

Once the file opens, to change the line endings, type:

:set ff=unix

Save the file:


Your vagrant ssh should work now.

Note: Peculiar that the ssh client shipped with Windows 11 works just fine with keys that use DOS line engings. Among the solutions is to use the Windows Terminal app and ssh from there.

Alternative to vim

As an alternative to using vim to change line endings is to use the dos2unix utility:

$ dos2unix .vagrant/machines/eo/virtualbox/private_key

How this was discovered

If we run the vagrant ssh command with the --debug flag, we can see the actual command that is invoked:

DEBUG safe_exec: Converted - Command: `"D:\\cygwin64\\bin/ssh.EXE"` Args: `["vagrant@", "-p", "2222", "-o", "LogLevel=FATAL", "-o", "Compression=yes", "-o", "DSAAuthentication=yes", "-o", "IdentitiesOnly=yes", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", "-o", "PubkeyAcceptedKeyTypes=+ssh-rsa", "-o", "HostKeyAlgorithms=+ssh-rsa", "-i", "D:/web/ee_vagrant/.vagrant/machines/eo/virtualbox/private_key"]`

We can clearly see which key is attempted to be used for this ssh session. Parsing the structure above into a terminal command, several tests have been performed using different shells and ssh clients. Some of them worked (Windows ssh client) while all others didn't.

Opening the file in Vim brought the attention to line endings, which eventually was the issue at hand.