Skip to main content

Creating a Vagrant Box

Hands-On Lab

 

Photo of Elle Krout

Elle Krout

Content Team Lead in Content

Length

00:15:00

Difficulty

Beginner

Although a number of base Vagrant boxes are available on the Vagrant box repository, there may be times where we need to create our own virtual machine image that Vagrant can use to provision new environments. And while some of this configuration is provider-specific, there are still a number of tasks that all base boxes require in order to work with Vagrant out of the box.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Creating a Vagrant Box

Introduction

Although a number of base Vagrant boxes are available on the Vagrant box repository, there may be times where we need to create our own virtual machine image that Vagrant can use to provision new environments. And while some of this configuration is provider-specific, there are still a number of tasks that all base boxes require in order to work with Vagrant out of the box.

Solution

Open a terminal session, and log in via SSH using the credentials provided on the lab page.

Install Needed Packages

  1. Switch to root:

    sudo -i
  2. Install the linux-headers package for the appropriate release, build-essential and dkms:

    apt-get install linux-headers-$(uname -r) build-essential dkms -y

Configure Vagrant Users

  1. Create a new vagrant user:

    useradd vagrant -m -G sudo -s /bin/bash
  2. Set the default password for the vagrant user to vagrant:

    passwd vagrant
    
    vagrant
  3. Set the default password for the root user to vagrant as well:

    passwd root
    
    vagrant
  4. Ensure the vagrant user can use passwordless sudo. Open the file:

    visudo
  5. At the bottom of the file, under the # See sudoers(5)... line, enter:

    vagrant ALL=(ALL:ALL) NOPASSWD: ALL

    Save and exit visudo.

Add the Vagrant Public Key

  1. Switch to the vagrant user:

    su - vagrant
  2. Create an .ssh directory:

    mkdir .ssh
  3. Pull down the public key:

    wget https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub
  4. Add it to the authorized_keys file:

    mv vagrant.pub .ssh/authorized_keys
  5. Set the permissions for the new directory and file:

    sudo chmod 700 .ssh
    sudo chmod 600 .ssh/authorized_keys

Conclusion

Congratulations on successfully completing this hands-on lab!