SSH Basics on Vagrant VMs

vagrantvirtualbox

I'm building a vagrant machine (Ubuntu 12.04) with the following requirements:

  • SSH access via Vagrant to a user with sudo-privileges.

I've generated public/private keys (via ssh-keygen) on the host and have moved the public key to the authorized_keys file on the guest. And I have tried a password-less SSH.

A couple of things happen I type vagrant ssh with password-enabled SSH keys:

  • I have to type my keyphrase in on the host machine to SSH into the Guest VM.
  • Each time I type in the correct keyphrase it is not accepted.

Which results in the I get the following error message:

SSH authentication failed! This is typically caused by the public/private
keypair for the SSH user not being properly set on the guest VM. Please
verify that the guest VM is setup with the proper public key, and that
the private key path for Vagrant is setup properly as well.

Then I tried password-less ssh.

vagrant ssh
vagrant@127.0.0.1's password: 

??

I never set up a vagrant user! It should be user@hostname as I had set it up and can confirm works when I boot the VM in VirtualBox.

How do I get private ssh keys to work properly with vagrant? What to do on the host, what to do on the guest?

Update

In VirtualBox the VM is live but I can't do any of the following commands from vagrant console in addition to the problems with vagrant ssh:

vagrant up
vagrant halt

The only vagrant command that works is vagrant suspend. When I use it I can actually halt the machine via vagrant halt. Here is the output:

$ vagrant halt
[default] Attempting graceful shutdown of VM...
SSH authentication failed! This is typically caused by the public/private
keypair for the SSH user not being properly set on the guest VM. Please
verify that the guest VM is setup with the proper public key, and that
the private key path for Vagrant is setup properly as well.

Best Answer

I am adding this solution for anyone who comes to this thread:

First open this file on the host machine:

$ sudo vim ~/.vagrant.d/boxes/<yourbox>/include/_Vagrantfile

Verify that config.ssh.private_key_path is set to your private key file. If not update that

ssh to vagrant box with default pwd

$ vagrant ssh (default pwd = ‘vagrant’]

Once in the box:

vagrant@lucid64:~$ sudo visudo -f .ssh/authorized_keys

Add your public key to this file and save the file, and exit the box

vagrant@lucid64:~$ exit

Back to the local machine

$ vagrant halt
$ vagrant up

This worked for me

Related Question