Search code examples
sshvagrantprivate-keyvagrantfilepublic-key

vagrant custom ssh key authentication failure


Below is my Vagrantfile

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

#Virtualbox host and vagrant host/network confs
#
Vagrant.configure("2") do |config|
  config.vm.define "slave" do |slave|
    slave.vm.box = "centos/7"
    slave.vm.hostname = "slave.ansible.com"
    slave.vm.network :private_network, ip: "192.168.99.102"
    slave.ssh.insert_key = false
    slave.vm.boot_timeout = 800
    slave.ssh.private_key_path = ["keys/id_rsa_slave"]
    slave.vm.provision "file", source: "keys/id_rsa_slave.pub", destination: "~/.ssh/authorized_keys"
  end

    config.vm.provider "virtualbox" do |vb|
      vb.cpus = "1"
      vb.memory = "512"
    end

This Vagrantfile sits in the slave folder under my home directory (/user/gokul/slave) and under this, I have keys directory with below keys and appropriate permissions

(base) Gokul:slave gokul$ ls -lt keys/
total 16
-rw-------  1 gokul  gokul   565 May 16 18:30 id_rsa_slave.pub
-rw-------  1 gokul  gokul  2590 May 16 18:30 id_rsa_slave

Permission of the keys directory is also fine

(base) Gokul:slave gokul$ ls -ld keys/
drwx------  4 gokul  gokul  128 May 16 18:30 keys/

Now I run below command to get my vagrant box up

vagrant up

And it hangs at this point failing to authenticate

==> master: Waiting for machine to boot. This may take a few minutes...
    master: SSH address: 127.0.0.1:2200
    master: SSH username: vagrant
    master: SSH auth method: private key
    master: Warning: Authentication failure. Retrying...
    master: Warning: Authentication failure. Retrying...

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.

With debug enabled also I can see that it picks up the private key that I've asked to, however, it fails to authenticate successfully and fails with above error.


Solution

  • Figured it out. The custom key that I am using should be appended to the default vagrant key - ~/.vagrant.d/insecure_private_key

    So this config

    slave.ssh.private_key_path = ["keys/id_rsa_slave"]
    

    Should be changed to

    slave.ssh.private_key_path = ["keys/id_rsa_slave", "~/.vagrant.d/insecure_private_key"]
    

    After making this change, I ran

    vagrant up

    And it came up successfully.