Managing and provisioning VMs
Touch and Go
With Vagrant, you can automate the creation and management of consistent virtual machines that work across platforms.
Automation is the key ingredient for efficiency in any system administration strategy. If your job involves spinning virtual machines (VMs) regularly, you should familiarize yourself with Vagrant [1], which helps you make consistent virtual environments available to your users with a few keystrokes. Vagrant provides a simple and easy to use command-line interface (CLI) that helps automate the creation, editing, running, and deletion of VMs. Moreover, it supports all major virtual platforms, such as VirtualBox and VMware, and plays nicely with all the well-known software configuration tools, such as Chef, Puppet, Ansible, Fabric, and more.
Since VM hypervisors like VirtualBox and VMware have their own CLIs that can be used to automate provisioning, why should you choose Vagrant? Vagrant offers consistency and interoperability. With Vagrant you can define your virtual environment in code and then use it to provision VMs on top of any hypervisor running on any operating system. Additionally, anytime you make changes to the virtual environment, instead of sharing the complete VM that can be worth several gigabytes, you can share a simple text file that can then be used to provision VMs with the changes.
A Rolling Start
While Vagrant can provision VMs with various virtualization software, by default it uses the free and open source VirtualBox.
To get started, the first thing you need to do is install Vagrant and VirtualBox. The Vagrant project recommends downloading the packages for Vagrant [2] and VirtualBox [3] directly from their respective websites, since package managers will probably have outdated versions.
Next, you'll need to create a Vagrant configuration file that defines all the VM's characteristics from a template. You can search for templates based on various operating systems and predefined purposes on the project's website [4]. For example, fire up a terminal and enter:
$ mkdir ~/demo $ cd ~/demo $ vagrant init centos/7
to create a VM based on the CentOS 7.6.181 release inside the ~/demo
directory.
Under the ~/demo
directory, this command creates a file called Vagrantfile
, which is the main configuration file that defines all the VM's attributes. If you want to make changes to a VM, you'll need to edit this file. You'll need to be well-versed with the Vagrantfile's anatomy in order to modify or create one as per your needs.
For now, you can bring up the VM using the default settings, by typing:
$ vagrant up
This command will create the actual VM reading the configuration specified in the Vagrantfile. This tells Vagrant to create a new VirtualBox machine based on the base image specified in the Vagrantfile. It'll do so by copying the virtual hard disk files from the remote server (Figure 1).
Once it's done, you'll have a fully featured CentOS 7 VM running headless in the background. If you get errors regarding missing guest additions, refer to the "Guest Additions Plugin" box to leverage Vagrant's extensive plugin infrastructure to solve the issue permanently.
Guest Additions Plugin
The VirtualBox guest additions is an essential component that is required to take full advantage of Vagrant. However, rolling them into every box and then making sure the boxes are running the latest version of the guest additions is a time consuming task and an unnecessary distraction. A Vagrant plugin can take care of installing and updating the guest additions automatically.
To install the plugin, head to the terminal and run the following Vagrant command:
$ vagrant plugin install vagrant-vbguest
Once Vagrant has been equipped with the plugin, every time you launch a box, Vagrant will check whether the VM is equipped with the guest additions. If the latest version is installed, it'll continue booting the machine. If an update or installation is required, Vagrant will then automatically download all dependent packages and then install the guest additions from the VirtualBox ISO (Figure 2).
Once the VM is up and running, no extra window will pop up on the screen; you'll be returned to the shell prompt, because Vagrant VMs run headless by default. Instead you can access the VM by typing:
$ vagrant ssh [vagrant@localhost ~]$
As you can see, the command uses SSH to connect to the VM. The command will automatically authenticate the SSH sessions and drop you at the SSH shell in the machine. You can now interact with the VM like any other CentOS installation (Figure 3). You can also share files between the host and the VM (see the "Shared Filesystem" box). When you're done working inside the VM, type:
[vagrant@localhost ~]$ exit logout Connection to 127.0.0.1 closed. $
which will drop you back to your host's CLI.
Vagrantfile Anatomy
Before you can make changes to your VMs, you must familiarize yourself with Vagrantfiles. Vagrant is configured separately for each VM, each of which has its own isolated configuration environment. At the crux of each VM is the configuration file, Vagrantfile.
The Vagrantfile is a simple text file that Vagrant reads in order to create the virtual environment. The Vagrantfile describes the various parameters that are necessary to create the VM. Vagrant reads this file to configure and provision the VMs.
Because of this singular Vagrantfile, other users can use Vagrant to automatically and easily create their virtual environment with a single command. Vagrantfiles are portable, which means you can use them to create and provision VMs on every platform that Vagrant supports, including Linux, Windows, and Mac OS X.
As shown previously, the Vagrantfile is created automatically when you issue the vagrant init
command, for example:
vagrant init centos/7
By default, the file is heavily commented out and only exposes a handful of parameters. However, these are the most essential ones, and you can use this Vagrantfile to create a VM with any additional modifications.
Also note that a Vagrantfile is written in Ruby. Even if you aren't well versed in Ruby, you'll be able to configure every aspect of Vagrant without ever learning the programming language.
Boxed In
One of the very first parameters in the Vagrantfile is config.vm.box
. In our example, this parameter will be set to centos/7
.
Because building a VM from scratch is a resource-intensive and time-consuming endeavor, Vagrant uses a base image and clones it to rapidly create a usable machine. In Vagrant terminology, this base image is called a box, and it is distributed in the form of .box
files.
Boxes contain already-installed operating systems (CentOS in my example), so they're usually quite large, ranging from a few hundred megabytes to a few gigabytes. The boxes are downloaded when you issue vagrant up
for the first time. Vagrant saves this box file for future usage, so it won't have to be downloaded again, even when you remove a Vagrant VM.
The required box needs to be specified for every VM using the config.vm.box
parameter in the Vagrantfile. In my example, this is set to centos/7
since this was the box I requested with vagrant init
earlier. Remember that multiple Vagrant environments can have the same config.vm.box
value, which gives you the flexibility to create different VMs for different purposes with the same box image.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.