4

First of all, i set up a separate ssh key for my vagrant box. And i have this setup on my ~/.ssh/config

Host    vag_ubuntu14
HostName 127.0.0.1
Port    2222
User    vagrant
IdentityFile ~/.ssh/vag_ubuntu14/id_rsa

And i copied the public key to the vagrant box's ~/.ssh/authorized_keys with this command.

cat ~/.ssh/vag_ubuntu14/id_rsa.pub | ssh -p2222 vagrant@127.0.0.1 'cat > ~/.ssh/authorized_keys'

So when running ssh vag_ubuntu14 works as expected. But running vagrant ssh to ssh to the vagrant box doesn't work. It produces authentication failure.

Here's my current Vagrant file with the path to the private key already specified.

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|

  config.vm.box = "ubuntu14_04"

  config.vm.provider "virtualbox" do |vb|
    vb.name = "Ubuntu 14.04"
  end

  config.vm.provision :shell, path: "provision/bootstrap.sh"

  config.ssh.private_key_path = '/home/chris/.ssh/vag_ubuntu14/id_rsa'

end

But when i run vagrant ssh-config, it doesn't respect the custom path to the private key that i specified on my Vagrantfile.

Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/chris/ubuntu14_04/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL
theUnknown777
  • 541
  • 1
  • 7
  • 16

2 Answers2

7

For anyone who got the same problem. I've found that the solution is very simple.

For your custom configuration of the location of the private key on your Vagrantfile to be honored. You must first delete the default private key.

You can see the location of the private key by running:

`vagrant ssh-config`

Delete the private key as specified on the IdentityFile.

Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/chris/ubuntu14_04/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

When you've deleted the private key that comes pre-installed on your vagrant box, then just specify the location of your new private key on Vagrantfile.

config.ssh.private_key_path = 'location of your private key'

To check that your new private key is the one that is read, then run vagrant ssh-config again.

theUnknown777
  • 541
  • 1
  • 7
  • 16
  • After trying various solutions for hours, this finally solved the problem for me. I am still confused though as to why my `vagrant ssh-config` lists two `IdentityFile` entries – Marco Jul 15 '15 at 16:40
  • According to the vagrant docs: You can also specify multiple private keys by setting this to be an array. This is useful, for example, if you use the default private key to bootstrap the machine, but replace it with perhaps a more secure key later. (https://www.vagrantup.com/docs/vagrantfile/ssh_settings.html) – Aine Mar 29 '18 at 08:02
0

What worked for me! (may be its patch or Jugad)

In VagrantFile

   config.ssh.private_key_path = 'FULL PATH OF PRIVATE KEY'

eg.

  config.ssh.privae_key_path = 'C:/Users/ajs_n/.ssh/private_key'

Care: path separators subject to OS.

ajay_full_stack
  • 494
  • 7
  • 13