I am trying to connect with SSH from my MacBook to a Vagrant machine.

vagrant ssh works fine, but I'm wondering why it is failing when I use ssh from the Mac Terminal.

Here is the error it throws:

Raghavendras-MacBook-Pro:~ raghu$ ssh -p 2222 vagrant@
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
  • Please edit your question to add the output of what you see when you run ssh -vvv -p 2222 vagrant@ – JakeGould Dec 11 '16 at 5:39

Because you are issuing a "plain" ssh command and in result you are trying to login to the VM using your default id_rsa+id_rsa.pub key-pair.

Unless you explicitly added ~/.ssh/id_rsa.pub from the MacBook to the ~/.ssh/accepted_keys of the vagrant account on the VM, your default key-pair is unknown to the VM and will be rejected with the "Permission denied" message you get.

You can use a key that Vagrant creates and injects into the machine upon creation. Run the following from the project directory, replacing <machine_id> with the name you configured in the Vagrantfile (also assuming you are using VirtualBox, otherwise change that part of the path too):

ssh -i .vagrant/machines/<machine_id>/virtualbox/private_key -p 2222 vagrant@

The command ssh vagrant@ -p 2222 should work fine, but sometimes Vagrant changes the port it’s forwarding to the localhost so the -p 2222 won’t work.

So double-check what port SSH is being forwarded on/from and use that port number instead.

A simple way to check that is to vagrant ssh from your Vagrant setup directory and then in another shell/terminal window run this command:

ps aux | grep ssh

There should be a line in there that shows you exactly how the SSH connection was established that looks like this:

ssh vagrant@ -p 2226 -o Compression=yes -o DSAAuthentication=yes -o LogLevel=FATAL -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /path/to/Vagrant/.vagrant/machines/nodejs/virtualbox/private_key

Ignore the bulk of that command unless you care about password-less access. Just note this first part:

ssh vagrant@ -p 2226

That is the command—in my example—where the forwarded SSH post is 2226. That works in my case. And if you are prompted for a password for some reason, remember the default Vagrant user is vagrant and the default password for that user is… vagrant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.