In the previous article, we have seen how to set up vagrant in your Linux distribution. In this article, you will learn how to create a Vagrant project to install a vagrant box, configure a guest machine, and understand core aspects of vagrant and important configurations and commands.
What is Vagrant Box?
The vagrant box is a package format for the vagrant environment. When you download or package a virtual machine to be used in vagrant it will be in box format. When I say boxes throughout this series I am just referring to a virtual machine.
There are two ways you can get the boxes. You can create your own custom box and share it among others and use an identical environment or download the boxes from the vagrant site.
There are boxes hosted on the vagrant site which is already packaged and ready to use. On the site, you can search for different box flavors (CentOS, Ubuntu, Arch, etc..) and you also have the option to choose a Provider (here we using VirtualBox as provider).
Setting Up Vagrant Project Environment
Let’s start creating a project and download a vagrant box. Open the terminal and create a project directory by running the following command. You can create this directory in any location you want in your file system.
$ mkdir ~/vagrant_project
Now move into the newly created directory and run the vagrant init command, which will create a vagrant configuration file in your project directory.
$ vagrant init
Now open the vagrant file, which is a template file created by the vagrant when you run the vagrant init command. This file is where we are going to store all our box-related configuration information.
$ cat Vagrantfile
Install a Virtual Machine with Vagrant and VirtualBox
At the moment all you need to care about is modifying a single parameter “config.vm.box”. This parameter will take care of downloading the box from the vagrant site. I am going to download the Ubuntu20.04 box. In the vagrant, site search for “Ubuntu20” and set the Provider to Virtualbox as shown in the below image.
Click the box from the site and you can get the box name to configure in the vagrant file.
Now pass the box name as the argument with the vagrant init command which will automatically set the box name in the Vagrant file.
$ vagrant init generic/ubuntu2004 ==> Create a vagrant config file $ vagrant init -m generic/ubuntu2004 ==> Create a vagrant config file without helper and comments $ vagrant init -f generic/ubuntu2004 ==> Override existing vagrant file
Now you have configured the mandatory parameter to download a box. Now run the “vagrant up command”, which will read the Vagrant configuration file to download and provision the box.
$ vagrant up
If everything goes well you will see the output as shown in the below image. It is important to understand these outputs to get to know about your virtual machine configurations.
Configures Guest Machines According to Vagrantfile
Let’s break down the output from the vagrant up command.
1. The first line is going to be your Provider name, Virtualbox is the default provider and it is pointing to that.
Bringing machine 'default' up with 'virtualbox' provider...
2. All your boxes and vagrant-related files will be stored under the
.vagrant.d directory under your home folder. Vagrant will see if the named box (generic/ubuntu2004) is already available under this directory to import the box if not it will connect with the vagrant site and will download the box. In this case, I already have ubuntu2004 downloaded from the vagrant site so it is just importing the box.
$ ls -l ~/.vagrant.d/boxes/
3. When your box boots up it will check for updates. To turn this off you can set the following parameter in your Vagrantfile.
config.vm.box_check_update = false
4. By default your virtual machine network mode will be set to NAT and port forwarding will be enabled for you to SSH from the Host machine. This is configurable and we have options to select different networking modes.
5. Each box has only two users available. One is the root and the second is the vagrant user. The default username and password are vagrant.
Password-based authentication is turned off by default and a public-private key pair is automatically generated and is copied to your virtual machine during boot up and is used to connect to your box.
Username = vagrant Password = vagrant
You can run the following command to check the ssh configuration for the box. From the below image you can password authentication is turned off and the user is set to vagrant and port forwarding is set to 2222.
$ vagrant ssh-config
6. Some boxes will try to mount a shared folder to your VM. You need guest additions installed in your VM for the shared folder to work. Your current directory (project directory) from the local machine will be mounted with /vagrant in the remote machine.
From all the above steps you might now have an idea what vagrant is doing. All these steps will be taken care of by us when we are manually setting up a VM in virtualbox. Here vagrant is taking care of all these steps in an automated way.
Now you might have a single or multiple VM running and to check the status run the following command. You can get the name, provider, state, id, and directory information of all the vagrant boxes in your machine using:
$ vagrant global-status --prune
Now that you may have a fair amount of understanding of what vagrant does. In the next article, we will see important vagrant commands and how to connect with a box and work on it.