Search This Blog

Saturday 4 July 2015

Using PuTTY to access Vagrant VMs from Windows

 
If you are running Vagrant on a Windows host, connecting to the Vagrant managed machines is not very straight forward. This is so because Windows does not natively support SSH. I usually use one of these options to interact with my Vagrant VMs from Windows:
  1. Get SSH for Windows
    You can do this by installing Cygwin or Git for Windows. After installation, set the location of ssh.exe in your PATH Environment variable. Lastly, just type “vagrant ssh <<machine name or IP>>” from the Windows command prompt.
  2. Use WinSCP
    WinSCP is useful to transfer files between your host and the VMs. Of course, you can also use Shared Folders feature of VirtualBox which is also easily configurable through Vagrant.
  3. Use an SSH Client like PuTTY  -->  the focus of this article.

Decide on the Host Name and the Port

When using PuTTY, you can either connect directly to IP address of the VM or through the localhost (via port forwarding):
SSH Port of Virtual Machine

Just give the configured IP of the VM and connect over port 22 (the standard SSH port).
If you do not want to remember the VM’s IP then add a hostname entry for the IP addres in the %systemroot%\system32\drivers\etc\hosts file.


Forwarded port of Host Machine
Forwarded ports can be of two types.
First is the default port that Vagrant opens on the host machine. You can find out the default forwarded port by running the “vagrant ssh-config” command.
The second is an explicitly specified forwarded port in the VagrantFile:
config.vm.network "forwarded_port", guest: 22, host: 8000  This indicates that any traffic to port 22 of your VM will also be forwarded to port 8000 of your host machine.


clip_image002 clip_image004


Option 1 - Login with username/password

Once you connect, you will get the login prompt. The default username/password is vagrant/vagrant
image

 

Option 2 - Login with SSH Keys

An easier way to connect than using passwords is using SSH keys. So how does this work?
When you manage a VM through Vagrant, it automatically does the following things on the host:

  1. Create a public-private key pairs
  2. Copy the keys into the ~vagrant/.ssh directory
  3. Adds the public key to the ~vagrant/.ssh/authorized_keys file

The private key generated is then copied on to the local machine to enable automatic connection through SSH. In this section we’ll see how to find out the location of the VM’s private key in your host and then use it to connect to the VM via PuTTY.

Step 1 - Identify the location of Vagrant’s private key for your VM

To find out the location of the private key go to the host directory containing your VagrantFile and type “vagrant ssh-config”. This command will give you the entire SSH configuration.You will get the private key location in the “IdentityFile” field. To run this command you need the VM to be up and running.

Alternately, you can just drill down in to the directory containing the VagrantFile and locate the file named “private_key”. For example, in my case the VagrantFile is present in “F:\Virtual Machines\ELK1” directory. I found the private key file in “F:\Virtual Machines\ELK1\.vagrant\machines\logstash-server\virtualbox\private_key”.

clip_image006

 

Step 2 - Use PuTTYgen to convert the private key to PPK format

Vagrant stores the private key in a OpenSSH format. However PuTTY only supports keys that are in PPK format (PuTTY Private Key). So now we will have to convert the private key from OpenSSH format to PPK format.
There is a tool called PuTTYgen to do this. The steps to be followed are given below.
(Note: You can optionally choose a passphrase for your private key. I am not doing so because I am just running a home lab here.)
putty key

 

Step 3 - Configure PuTTY to use the private key

Now that you have the private key in PPK format, configure PuTTY to use the same:
putty 2

 

Step 4 - Just connect!

Everything done right, you can now use PuTTY to connect directly to your Vagrant VM without any username and password!
image

3 comments: