Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accidentally deleted authorized_keys on vagrant box

Tags:

linux

ssh

vagrant

I accidentally deleted my authorized_keys file on my vagrant box and I can't SSH into it anymore but I still have a console connected to it.

How do I go about re-making this folder so it allows me to connect again?

I have my .d.vagrant/insecure_private_key on my host machine, do I need to create a public key of that and move that over or do i need to move the private key over too?

like image 437
user1157885 Avatar asked Dec 29 '25 00:12

user1157885


2 Answers

Run the following commands on the host:

cd <vagrant_project_directory>
ssh-keygen -y -f `vagrant ssh-config | grep IdentityFile | rev | cut -d ' ' -f1 | rev`

It will display a public key for the currently configured private key.

Add the result to the ~/.ssh/authorized_keys on the Vagrant box machine.

like image 106
techraf Avatar answered Dec 30 '25 23:12

techraf


run vagrant ssh-config to review which private key the VM is using

If you're using the vagrant insecure key, you can

  1. create the ~/.ssh/authorized_keys with 600 mode
  2. download the vagrant public key into this file

If you have used your own private key, repeat the steps but create the public key from your key.

You dont need to move the private key, it must stay local and is only needed for you to login into the VM (unless you ssh from the VM into other servers like github or other ssh which will need the key, but thats another thing)

like image 39
Frederic Henri Avatar answered Dec 31 '25 00:12

Frederic Henri