SSH & Vagrant

Vagrant is a handy tool for creating VMs. It’s a lot like firing up an EC2 instance, but in Vagrant’s case, everything is localized. And best of all, it’s free.

I tend to favor Ubuntu as my preferred flavor of linux; consequently, all production EC2 instances use a customized Ubuntu AMI. Testing various aspects of this system with various software libraries, however, is initially tested locally using Vagrant VMs. What’s more, you can install localized VMs of other operating systems ranging from Debian to OpenSuse to Heroku’s Cedalon.

Firing up a local instance of Heroku’s Cedalon (which is a version of Ubuntu 10.04 with Ruby and Node.js installed) is as easy as typing:

Firing up Heroku’s Cedalon locally

vagrant init heroku
vagrant up

Then you can SSH to that running VM like so:

SSHing to a local VM

vagrant ssh

The command vagrant ssh can be problematic, though, especially if you are automating some aspect of SSH – for example, you are using some library on top of SSH. In that case, you need to use SSH directly rather than through Vagrant.

Luckily, you can SSH normally to local Vagrant instances easily enough. You’ll need to tell SSH which key to use (Vagrant creates one for you), which user to connect as (usually vagrant), and what port to connect to (usually 2222). All of this information can be found via the command vagrant ssh-config.

To use plain Jane SSH when working with Vagrant instances, you’ll first need to execute this command:

Printing out the location of Vagrant’s key

vagrant ssh-config | grep IdentityFile  | awk '{print $2}'

This 3 pronged command ultimately tells you where Vagrant has created a key file. The command awk '{print $2}' prints the second column of the line in the ssh-config string that starts with IdentityFile (which points to its location).

Now that you know where the key file is, you can ssh (assuming the default port hasn’t changed from 2222 – double check your ssh-config to make sure) like so:

Using normal SSH

ssh -i /some/dir/.vagrant.d/insecure_private_key -l vagrant -p 2222

If you happen to fire up different VMs – for example, I have both Heroku Cedalon and normal Ubuntu instances, SSH will probably complain that the remote host identification has changed. This is a valid warning as the RSA fingerprint associated with the local host has indeed changed.

You can suggest that SSH ignore this warning by adding two additional flags. I highly recommend you only do this via the command line and not with some SSH config file as the warning SSH throws is completely legitimate and could be saving you from a man-in-the-middle attack.

Nevertheless, if you are working with localized VMs and need to bypass that warning, use these two flags:

Two additional flags to SSH

-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no

Consequently, when I SSH to a local Vagrant instance, I type something along the lines of:

SSHing to Vagrant instances

ssh -i /some/dir/.vagrant.d/insecure_private_key -l vagrant -p 2222 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no

Finally, to shut down a Vagrant instance, just issue the command:

SSHing to Vagrant instances

vagrant destroy

Vagrant makes it super easy to fire up localized development environments – Vagrant instances boot up faster than EC2 instances and they’re free. And now you know how to use normal SSH to connect to them. Can you dig it?

Reference: SSH & Vagrant from our JCG partner Andrew Glover at the The Disco Blog blog.
Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Leave a Reply

6 + eight =

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books