When Vagrant sets up a virtual machine it will set up a port on your local machine that you can use to connect to the box via SSH. By default this is usually port 2222, which Vagrant maps to port 22 on the virtual machine. All this is essentially transparent so that when you type vagrant ssh you connect to the box without any problems. Vagrant handles all of the port matching and key finding behind the scenes.
Out of curiosity I found myself needing to connect to the box directly via the IP address I set in the Vagrantfile rather than use the vagrant ssh command. I know that Vagrant creates a link to the server with the username of 'vagrant' and a private key that is found in the .vagrant.d directory in your user folder. This is the same private key that has been in use since the beginning of Vagrant so it will be the same on any machine with Vagrant installed. So my first test was with a standard SSH connect command, but also passing in the Vagrant insecure key using the -i flag. This failed with the following output.
$ ssh [email protected] -i /your/user/directory/.vagrant.d/insecure_private_key
Received disconnect from 192.168.100.100: 2: Too many authentication failures for vagrant
After a bit of digging I found that although I was passing in the key there were a few other options that I needed to include in order to get things working. This lead me to discover the vagrant ssh-config command. You can use this command to output a valid SSH configuration that can be placed into your SSH config file to allow connection to the virtual machine. Here is some sample output (the reference to 'default' is the name of the host in your Vagrantfile).
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /your/user/directory/.vagrant.d/insecure_private_key
IdentitiesOnly yes
LogLevel FATAL
ForwardAgent yes
By putting this into your .ssh/config file you can connect to the virtual machine via SSH in the following way.
ssh [email protected]
However, what we are doing here is essentially using the local machine to route us to the correct machine based on it's port number. I still wanted to connect to the box via the IP address that I had set for it. This meant using the -o flag to pass in the extra SSH config parameters to the command.
So assuming that the virtual machine's IP address is 192.168.100.100 then you would connect to it in the following way.
ssh [email protected] -i /your/user/directory/.vagrant.d/insecure_private_key -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o IdentitiesOnly=yes
You can simplify this slightly by using your SSH config file to automatically add these parameters to your SSH call when you run it. This assumes the box IP address is the same as above, but you can also include wildcards in the Host header to include a number of Vagrant boxes (based on their IP address).
Host 192.168.100.100
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
IdentitiesOnly yes
User vagrant
IdentityFile /your/user/directory/.vagrant.d/insecure_private_key
PasswordAuthentication no
This allows you to connect to the box using the following command (without so many of the extra options present).
ssh [email protected]
Comments
Submitted by Dan Shumaker on Tue, 06/03/2014 - 17:55
PermalinkSubmitted by giHlZp8M8D on Wed, 06/04/2014 - 10:40
PermalinkSubmitted by Kaplan on Wed, 12/17/2014 - 18:42
PermalinkSubmitted by Kaplan on Wed, 12/17/2014 - 19:04
PermalinkSubmitted by lusob on Tue, 02/24/2015 - 08:43
PermalinkSubmitted by giHlZp8M8D on Tue, 02/24/2015 - 09:16
PermalinkSubmitted by Wolph on Wed, 02/25/2015 - 15:00
PermalinkSubmitted by giHlZp8M8D on Thu, 02/26/2015 - 10:54
PermalinkSubmitted by Dustin Malone on Tue, 03/21/2017 - 22:29
PermalinkSubmitted by Pedro de Carvalho on Sun, 03/26/2017 - 04:20
PermalinkAdd new comment