How to fix Vagrant error: `private_key_path` file must exist:

人盡茶涼 提交于 2019-12-05 06:12:32

I fixed the problem by replacing the path to insecure_private_key by hard coding the path to the insecure_private_key file.

So it went from:

config.ssh.private_key_path = [
    customKey,
    "#{ENV['HOME']}/.vagrant.d/insecure_private_key"
]

To:

config.ssh.private_key_path = [
    customKey,
    "C:/Users/My.User/.vagrant.d/insecure_private_key"
]
Lyndon

It looks like it's because you may have performed a vagrant destroy which deleted the insecure_private_key.

But the vagrant file looks up the puphpet\files\dot\ssh files, if they are there, it looks for the insecure_private_key.

delete (rename) the id_rsa files in puphpet\files\dot\ssh

this fixed it for me!

You can also just delete all the files in the puphpet folder rm -rf puphpet/files/dot/ssh/* and the vm should regenerate them when you run vagrant provision.

When you are sharing your puphet configuration to your teammates, hardcoding the private_key_path is not advisable as per the accepted answer.

My host computer is windows so i have added a new environment variable VAGRANT_HOME with value %USERPROFILE% since this is where my /.vagrant.d folder resides. When you add this variable just make sure that you close command prompts that are open so the variable will be applied

Hope this helps

I'm not sure what's wrong with your Vagrant installation, but this line:

vagrant_home = (ENV['VAGRANT_HOME'].to_s.split.join.length > 0) ? ENV['VAGRANT_HOME'] : "#{ENV['HOME']}/.vagrant.d"

is what sets up the variable that is later on used here:

config.ssh.private_key_path = [
  customKey,
  "#{vagrant_home}/insecure_private_key"
]

The reason this is happening is that as of Vagrant 1.7, it generates a unique private key for each VM you have. There's, what I consider to be, a bug in that Vagrant completely ignores user-defined private_key_path if it detects that it generated a unique key previously.

What PuPHPet is doing here is letting Vagrant generate its unique SSH key, then once the VM boots up and has SSH access, it goes in and generates another key to replace it.

The reason we're replacing it is because this new Vagrant feature only works on OSX/Linux hosts, due to Windows not having the required tools.

My way works across all OS because it does the SSH key generation within the VM itself.

All this is semi-related to your question, but the answer is that something's wrong with your Vagrant installation if those environment variables have not been defined.

Adding to PunctuationMark's answer you can also set the VAGRANT_HOME environment variable in your Vagrantfile: ENV['VAGRANT_HOME'] = ENV['USERPROFILE']

Paromita Sengupta

Editing this following line in Vagrantfile worked for me.

PRIVATE_KEY_SOURCE      = '~/.vagrant.d/insecure_private_key'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!