Search This Blog

Showing posts with label Vagrant. Show all posts
Showing posts with label Vagrant. Show all posts

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

Saturday, 31 December 1988

The 5 things to know before you run Vagrant

Vagrant is a simple and elegant tool for creating and managing virtual machines. Think of it as a wrapper around your virtualization provider like VMware or VirtualBox.

For me it was quite interesting because with such a tool, I could focus on working in the “environment” rather than spend time creating and managing virtual machines. For example, in my IDS lab I have a sensor machine, server and analyst workstation. With Vagrant, I use a configuration file to define these 3 machines, bring them up and manage them as a group. When my work is done, I bring down my IDS setup, destroy the machines (with the relief that Vagrant will allow me to recreate them when necessary) and then maybe bring up my malware analysis lab which would have Windows 7 workstation and REMnux. All this can be managed using Vagrant through just a handful of commands.

That said, I thought I would write an article providing a brief overview of Vagrant. To begin with, here’s a simplified visualization of what Vagrant actually does:

vagrant complete

#1 - The VagrantFile

The VagrantFile is an ASCII based text file where you specify the configuration of the virtual machine you require. If you need 10 machines for your lab then you need to specify them sequentially in the VagrantFile.
For every VM you can specify:

  1. Physical properties of the machine (CPU, RAM etc)
  2. Network Interfaces
  3. Shared Folders (Folders shared between host and the VM)
  4. Hostname/IP Address
  5. Details of software to be installed on the VM – In Vagrant terms this is called provisioning.

Here is a snippet from the VagrantFile, to create a simple Ubuntu virtual machine. The syntax is based on Ruby. But even if you don’t know Ruby at all it is fairly straight to understand and implement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#- This line indicates the beginning of configuration. 
Vagrant.configure(2) do |config|
  
#- Begin configuration for my-ubuntu-machine
config.vm.define "My Ubuntu Machine" do |my-ubuntu-machine|
  
#- Specifying the “box” required for this machine and the URL to get it from. 
 my-ubuntu-machine.vm.box="precise64"
 my-ubuntu-machine.vm.box_url ="http://files.vagrantup.com/precise64.box"
  
#- Setting the hostname, IP and SSH port forwarding 
 my-ubuntu-machine.vm.hostname="ubuntu-machine"
 my-ubuntu-machine.vm.network "private_network", ip: "10.1.1.10"
 my-ubuntu-machine.vm.network "forwarded_port", guest: 22, host: 2210 
  
#- Provisioning or software/config update – I am just running apt-get update to get the latest versions of packages 
 my-ubuntu-machine.vm.provision "shell", inline: -SHELL
 #!/usr/bin/env bash
 apt-get update /dev/null 21
 SHELL
end 
  
#- Add more VMs as required…
  
#- Below line indicates end of configuration.
end

 

#2 – Vagrant Boxes

A box is file that represents a virtual machine. Think of it as an appliance that you can plug in to Vagrant and start running. Boxes are available for download from vagrantbox.es and the Hashicorp site. In addition to downloading boxes you can also create your own boxes and use them.

#3 – Virtualization Provider

This would typically be any software that provides an emulated environment for running different operating systems. Popular options are Vmware Player/Workstation/Fusion, and VirtualBox. Vagrant works free and fine with VirtualBox. However if you want to use it with VMware, you need to purchase additional licenses.

#4 – Vagrant Software

The Vagrant software:

  1. Reads the VagrantFile,
  2. Downloads the boxes (if not already available)
  3. Installs the required applications – For this it takes the help of
  4. Creates VMs in the Virtualization provider
  5. Provisions the machines as required - For simple software installations you can use shell scripts. Where shell scripts get complicated, Vagrant also provides support for Configuration management tools such as Chef, Puppet, Salt and Ansible.
  6. Provides capabilities for managing the VMs using the “vagrant” command.

#5 – Plug-ins

Vagrant provides extensibility with the use of Plug-ins (not shown in above diagram). As a developer you might be interested to add new vagrant commands, provide easier options to configure a host, add support for provisioners etc. In fact, Vagrant itself ships with a set of core plug-ins that work behind the scenes to configure and bring up your VM. You can find the list of core plug-ins here. Plug-ins are developed using Ruby and are implemented as self-contained packages (Rubygems).


To know more about Vagrant refer the Vagrant documentation or the very readable book by Mitchell Hashimoto – Vagrant Up and Running.