I am trying to use my own pair of RSA SSH-2 keys with Vagrant 1.9.5 on VirtualBox 5.1.22 with Windows 7 SP1 host and a CentOS 7.3 guest.
When I execute vagrant up
I get :
Waiting for machine to boot. This may take a few minutes...
SSH address: 127.0.0.1:2222
SSH username: vagrant
SSH auth method: private key
Warning: Connection aborted. Retrying...
Warning: Connection reset. Retrying...
Warning: Connection aborted. Retrying...
Warning: Connection reset. Retrying...
Warning: Connection aborted. Retrying...
Warning: Connection reset. Retrying...
Warning: Connection aborted. Retrying...
...
I have found that the cause is failing to connect to the guest because the required key is not being added to ~/.ssh/authorized_keys but it contains Vagrant's default insecure_private_key
.
This is my Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.boot_timeout = 120
config.ssh.insert_key = false
config.ssh.private_key_path = ["vagrant-setup/keys/my_openssh.key"]
# This is not copying authorized_keys to the guest
config.vm.provision "file", source: "vagrant-setup/.ssh/authorized_keys", destination: "~/.ssh/autorized_keys"
# Setting forward_agent to true and adding the key to Pageant doesn't make any difference
config.ssh.forward_agent = false
config.vm.define "MyMachineName" do |vs|
vs.vm.box = "vagrant-centos-73-x86_64-puppet"
vs.vm.box_url = "https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.3/vagrant-centos-7.3.box"
# The shell script that will execute once just after the VM is created
vs.vm.provision "shell", path: "vagrant-setup/setup.sh"
# Create a private network, which allows host-only access to the machine using a specific IP.
config.vm.network "private_network", ip: "192.168.101.110"
vs.vm.provider "virtualbox" do |vb|
# Enable the GUI of VirtualBox and see whether the VM is waiting for input on startup
vb.gui = false
end
end
end
I have tried copying autorized_keys using vm.provision "shell"
and cp
from the guest. I have tried to change the permissions of autorized_keys on the guest before copying but nothing seems to work because it does not connect. And I have tried to perform the copy inside MyMachineName like vs.vm.provision "file", ...
If I login in once using vagrant ssh
with user+password and I write authorized_keys by hand then afterwards I am able to log in with the SSH key and no password.
vagrant ssh-config
reports
Host MyMachineName
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile C:/MyMachineName/vagrant-setup/keys/my_openssh.key
IdentitiesOnly yes
LogLevel FATAL
Putting the private key into C:\Users\My User Name\.ssh\id_rsa
seems to make some difference, like if Vagrant was still looking for something there despite I explicitly set my own private key, but does not make it work. And it also seems to have a problem with C:\Users\My User Name\
having spaces but since it should not be used then that should not matter.
So the question is How do I make Vagrant work with my own pair of SSH keys without having to tweak the guest VM by hand?
There are plenty of replies at this other question, but most of them come down to put the key in authorized_keys by hand, which is exactly what I am trying to avoid.