Logging into vagrant over ssh

How to log into vagrant using nothing but the command line ssh client.

I wanted to log into a vagrant box over ssh the other day a while ago but was struggling so I left it. Now I just stumbled on how to do it so I thought I would document.

Locate the private SSH key

Vagrant basically creates their own user for logging into vagrant boxes but aside from that vagrant actually creates a unique private key per machine for the user to log in with. When using VirtualBox as a VM provider the private key is located here:

 # Vagrant private key
.vagrant/machines/default/virtualbox/private_key

Check your port

The other thing is that Vagrant sometimes uses a non standard port. This is configurable and in my case the port was set to 2222

Log in

Then you need to use the -i flag to signal which key and the -p flag to signal which port you want and presto! We are logging into vagrant over the normal ssh client.

# Log into vagrant over ssh
$ ssh -i ./.vagrant/machines/default/virtualbox/private_key -p 2222 \
vagrant@127.0.0.1  

Rudi Yardley

Read more posts by this author.