Child pages
  • Quick spin-up of Linux distros
Skip to end of metadata
Go to start of metadata

I regularly find myself wanting to quickly test out things on specific Linux distros. There are many ways to spin up VM, but I want something very quick and disposable.

After some experimenting, I settled on Vagrant, and came up with the following Vagrantfile:

  • A list of all the major distros
  • Using dedicated RFC1918 addresses
  • Adds my SSH public key
  • Default to 512M RAM and 1 CPU
  • Easy to add custom provisioning shellcode 

combined that with some dedicated RFC1918 addresses, and corresponding names.

There are 3 items involved:

Vagrant config

This is done in one Vagrantfile and looks like this:

Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

machines=[
 {
   :hostname => "wheezy",
   :ip => "192.168.77.71",
   :box => "debian/wheezy64"
 },
 {
   :hostname => "jessie",
   :ip => "192.168.77.72",
   :box => "debian/jessie64"
 },
 {
   :hostname => "stretch",
   :ip => "192.168.77.73",
   :cpus => 4,
   :memory => 4096,
   :box => "debian/stretch64"
 },
 {
   :hostname => "trusty",
   :ip => "192.168.77.74",
   :box => "ubuntu/trusty64"
 },
 {
   :hostname => "xenial",
   :ip => "192.168.77.75",
   :box => "ubuntu/xenial64",
   :shellcode => "apt-get -yy install python-minimal",
 },
 {
   :hostname => "bionic",
   :ip => "192.168.77.76",
   :box => "ubuntu/bionic64",
   :shellcode => "apt-get -yy install python-minimal",
 },
 {
   :hostname => "centos6",
   :ip => "192.168.77.77",
   :box => "centos/6"
 },
 {
   :hostname => "centos7",
   :ip => "192.168.77.78",
   :box => "centos/7"
 },
 {
   :hostname => "rhel6",
   :ip => "192.168.77.79",
   :box => "samdoran/rhel6"
 },
 {
   :hostname => "rhel7",
   :ip => "192.168.77.80",
   :box => "generic/rhel7"
 }
]


# To inject into each VM
mypubkey = File.readlines("#{Dir.home}/.ssh/id_rsa.pub").first.strip

Vagrant.configure("2") do |config|
  machines.each do |machine|
    config.vm.define machine[:hostname] do |node|
      node.vm.box = machine[:box]
      node.vm.hostname = machine[:hostname]
      node.vm.provision 'shell', inline: "echo \"#{mypubkey}\" >> /home/vagrant/.ssh/authorized_keys", privileged: false
      if machine.has_key? :shellcode
        node.vm.provision 'shell', inline: machine[:shellcode]
      end
      node.vm.network "private_network", ip: machine[:ip]
      node.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--memory", machine.has_key?(:memory) ? machine[:memory] : 512 ] 
        vb.customize ["modifyvm", :id, "--cpus", machine.has_key?(:cpus) ? machine[:cpus] : 1 ] 
      end
    end
  end
end


DNS

Add this to your /etc/hosts file:


#==== Vagrant distros
192.168.77.71 wheezy
192.168.77.72 jessie
192.168.77.73 stretch
192.168.77.74 trusty
192.168.77.75 xenial
192.168.77.76 bionic
192.168.77.77 centos6
192.168.77.78 centos7
192.168.77.79 rhel6
192.168.77.80 rhel7
#====


SSH configuration

Add this to your .ssh/config file:


# Vagrant distros
Host wheezy jessie stretch trusty xenial bionic centos6 centos7 rhel6 rhel7
  User vagrant
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no



At this moment you should be able to simply do:


vagrant up centos7
ssh centos7

And if you installed some HTTP service, simply go to http://centos7/


When you're done:

vagrant destroy centos7


Happy testing!



  • No labels