SSH into Vagrant using a private ip

Introduction

I've recently been looking at using netcat and also understanding how SSH tunelling works. In order to play around with tunelling specifically, I needed an environment that I could SSH into (e.g. so I could proxy a connection through to).

My initial thought was to use Docker and setting it up with an SSH daemon. Annoyingly I couldn't get it to let me SSH into the running Docker container. So due to a lack of time allowance dedicated to this problem, I decided to work around this by using Vagrant instead.

Set-up

Now Vagrant automatically sets you up with a vagrant ssh command, but I still needed an actual dedicated ip that I could use to SSH into the running Vagrant VM. So the solution requires the following items:

Vagrantfile

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.network "private_network", ip: "192.168.50.4"
end

As you can see, we're building a Ubuntu 14.04.2 VM, and we've defined a private ip address (192.168.50.4); although this could be any private ip range (e.g. 172.xx.xx.xxx) instead.

Note: as noted by Simon Thulbourn 192.168/16 is used more for home/small networks

SSH Config

Host 192.168.50.4
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null
  IdentitiesOnly yes
  User vagrant
  IdentityFile /path/to/user/directory/vagrant-ssh/.vagrant/machines/default/virtualbox/private_key
  PasswordAuthentication no

Note: the IdentityFile value can be retrieved from vagrant ssh-config

Example

Now we have the set-up in place, we can build the VM:

vagrant up --provider virtualbox

Once the VM is created and running we can SSH into the box directly using our defined private ip:

ssh -F ./ssh-config vagrant@192.168.50.4

Links