Explore Advanced Vagrant Features

Explore Advanced Vagrant Features

In First Steps with Vagrant, you learned how to install Vagrant, set up and run a basic virtual machine. Now, we will cover a few more complex features, such as synced folders, provisioning scripts and running multiple virtual machines at the same time.

Synced Folders

Synced folders allow us to share folders and files between the virtual machine and the host machine. The changes are immediate and are applied in both directions???changes made on the host machine will be visible on the virtual machine and vice versa. Synced folders are mapped in Vagrantfile:

Vagrant.configure("2") do |config|  # other config here  config.vm.synced_folder "home/myuser/sites", "/var/www"end 

The first path is the directory location on the host machine, while the second path is the location of the directory on guest machine (virtual machine). The default owner of the folder on the virtual machine is ?vagrant? (the root user), so we often need to manually specify the owner:

config.vm.synced_folder "home/myuser/sites", "/var/www", owner: "www-data", group: "www-data" 

If multiple synced folders are needed:

config.vm.synced_folder "home/myuser/sites", "/var/www", owner: "www-data", group: "www-data"config.vm.synced_folder "home/myuser/documents", "/home/vagrant/documents" 

We can also change the file and directory permissions:

config.vm.synced_folder "home/myuser/public", "/home/vagrant/public", :mount_options => ["dmode=777", "fmode=666"] 

Sometimes the default shared folder implementation (Virtualbox shared folders) will have high performance penalties. In those cases, it is possible to change syncing mechanism to NFS, RSync or SMB. Read more about it here.


Vagrant offers a wide variety of boxes. A box is a downloadable virtual box, which contains an operating system and some pre-installed software. In most cases, one of the many available boxes will fit your needs. But sometimes, you might want to install additional software or change the configuration. That?s the situation in which you will use a provisioning script, i.e. a script that will automatically execute predefined commands upon starting the virtual machine.

A provisioning script is usually a shell script, which can be written inline in Vagrantfile, saved to an external file or called from an external link:

Vagrant.configure("2") do |config|  config.vm.provision "shell", path: "script.sh"endVagrant.configure("2") do |config|  config.vm.provision "shell", path: "https://example.com/provisioner.sh"end 

Ansible, Chef, Docker and Puppet provisioners are also available.

Creating Multiple Virtual Machines

Using this feature, we can accurately represent and test systems with multiple servers. As with all other features, they are configured in Vagrantfile:

Vagrant.configure("2") do |config|  # This configuration applies to all virtual machines  # Set the box to Ubuntu 14.04 64-bit OS  config.vm.box = "ubuntu/trusty64"  # Map port 8080 on the host to port 80 on the virtual machine  # http://localhost:8080 should open the default Apache page on the virtual machine  config.vm.network "forwarded_port", guest: 80, host: 8080  # This configuration is applied only to the virtual machine named ?web?  config.vm.define "web" do |web|    # LAN IP address for the virtual machine    web.vm.network :private_network, ip: ""  end  # This part is applied only to the second virtual machine named ?db?  config.vm.define "db" do |db|    # LAN IP address for the virtual machine    db.vm.network :private_network, ip: ""  endend 

To run a virtual machine, you would enter vagrant up web or vagrant up db. To SSH into a virtual machine, you would use vagrant ssh web?or vagrant ssh dbNext Steps Explore available Vagrant boxes?or learn how to make a custom one.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes