Skip to content

Instantly share code, notes, and snippets.

@benlk
Last active July 24, 2019 19:31
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benlk/d0cf2b3251e6c09758f36ccd14d6894e to your computer and use it in GitHub Desktop.
Save benlk/d0cf2b3251e6c09758f36ccd14d6894e to your computer and use it in GitHub Desktop.

If you've had to re-associate your virtual machine with vagrant, but vagrant ssh now requires a password to connect, this is because the ssh key associated with the box disappeared. You can still get in with the password vagrant (usually), but many workflows will need the automatic connection.

Here's how to regain public/private key authentication to ssh into your vagrant virtual machine:

Run vagrant up, then vagrant ssh-config to find the IdentityFile:

$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile "/Users/blk/.vagrant.d/insecure_private_key"
  IdentitiesOnly yes
  LogLevel FATAL

cd to the directory of that identity file, and look for a .pub or public file. If it doesn't exist, create one:

$ cd /Users/blk/.vagrant.d/
$ ls -l
drwxr-xr-x  4 blk  staff   136B Apr  5 11:26 boxes/
drwxr-xr-x  8 blk  staff   272B Apr 26 19:10 data/
drwxr-xr-x  9 blk  staff   306B May 26  2015 gems/
-rw-------  1 blk  staff   1.6K May 14  2015 insecure_private_key
-rw-r--r--  1 blk  staff   147B Nov 16 12:13 plugins.json
drwxr-xr-x  3 blk  staff   102B May 14  2015 rgloader/
-rw-r--r--  1 blk  staff     3B May 14  2015 setup_version
drwxr-xr-x  2 blk  staff    68B Apr  5 11:26 tmp/

Note the lack of a public key or a .pub file.

$ file insecure_private_key
insecure_private_key: ASCII text
$ ssh-keygen -y -f insecure_private_key > ./insecure_public_key.pub
$ cat insecure_public_key.pub

And that will output the content of the public key. Copy that with your mouse.

$ vagrant ssh
Enter password:
vagrant@vagrant $ cd ~/.ssh/
vagrant@vagrant $ ls
authorized_keys
vagrant@vagrant $ vim authorized_keys

Then press the [escape] key, type :set paste, press [enter], [g] [g] [O], paste the line you copied, [escape] [:] [w] [q]

Those are the vim instructions; you can do it with nano or pico or emacs or ed or cat if you want; just make sure that the copied text ends up on its own line at the end of the file.

Then exit the vagrant machine and reconnect to test.

vagrant@vagrant $ exit
$ vagrant ssh
vagrant@vagrant $ 

Hey, it works!


If this does not work, cd to the folder containing the VM's Vagrantfile while the machine is running. Run ls -al and look for an entry named .vagrant.

If the .vagrant directory exists, cd .vagrant. Then:

/.vagrant$ tree .
.
└── machines
    └── default
        └── virtualbox
            ├── action_provision
            ├── action_set_name
            ├── id
            ├── private_key
            └── synced_folders

3 directories, 5 files

See private_key there? That's going to be useful.

cd machines/default/virtualbox/, then follow the steps listed above, begining with ssh-keygen.

@navono
Copy link

navono commented Apr 13, 2019

how to config ssh in win10? I can not find authorized_keys

@Akiriwe
Copy link

Akiriwe commented Jul 24, 2019

Thank you! Your guide helped me to deal with that!

@benlk
Copy link
Author

benlk commented Jul 24, 2019

Please note that this gist was last revised in 2016, and may not be up-to-date with current versions of Vagrant. It assumes a *nix-like box (Linux, OSX) as a host.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment