1. Overview

This tutorial will show how to copy files and folders from host to guest in Vagrant.

2. Copy Files and Folders from Host to Guest in Vagrant

2.1. Using Synced Folders or Shared Folders

Synced folders or Shared folder enable Vagrant to sync a folder on the host machine to the guest machine, allowing you to continue working on your project’s files on your host machine but use the resources in the guest machine to compile or run your project.

By default, Vagrant will share our project directory (the directory with the Vagrantfile) to /vagrant. Let’s see an example of the defaut shared folder created by Vagrant on VirtualBox:

copy files and folders from host to guest in Vagrant - Default Synced Folders In Vagrant

Default Synced Folders In Vagrant

To configure another synced folder, you can follow the below syntax:

For more detail, you can read this basic usage page.

In short, to copy files and folders from host to guest in Vagrant, we simply can drop them into the synced folder which can be our project directory by default or our other configured ones.

2.2. Using Vagrant Plugin vagrant-scp

vagrant-scp is a Vagrant plugin that allows us to copy files and folders to Vagrant virtual machines via SCP. Firstly, let’s install the plugin first by opening terminal or command prompt (on Windows) and run the following command. If you already installed the plugin, you can skip this step.

Please see another related article about using Vagrant ssh on Windows.

Next, let use the plugin to copy files and folders from host to guest in.

If we have just a single Vagrant VM, we can copy files and folders over like this:

Let’s see an example which we copy a host folder /data/examples/spring-boot to Vagrant VM:

Note that on above example, as we just have a single VM, we don’t need to specify its name clearly in the command. If we have multiple VMs, the plugin will not know to which VM the files or folders should be copied. Therefore, we have to specify the name of VM in those cases as below syntax:

Let’s see an example which we copy the above folder to the webserver VM:

2.3. Using SCP

Another way to copy files and folders from host to guest in Vagrant is to use to SCP. Firstly, make sure you have scp installed on your host. If you don’t have it, you can install the openssh-clients package if you’re using Linux or install Cygwin if you’re using Windows.

Secondly, we need to know the IP address and SSH port of Vagrant VM to which we want to copy files or folders. By default, the IP address is 127.0.0.1 and the SSH port is 2222. Besides, we can identify that information by looking at the output on the terminal or command prompt where we start the Vagrant VM. Let’s see an example:

Copy files and foldes from host to guest in Vagrant - Vagrant Default IP Address and SSH Port

Vagrant Default IP Address and SSH Port

Lastly, after having those things, we can start to copy files and folders from host to guest in Vagrant by referencing below examples:

Let’s copy the file /data/examples/spring-boot/gs-spring-boot-master.zip from host to the /home/vagrant on guest using SCP:

And here is an example which copies a folder recursively from host to guest:

2.4. Using the Vagrant File Provisioner

File Provisioner is a Vagrant provisioner which allows you to upload a file or directory from the host machine to the guest machine. Let’s see below example which we will replicate our local ~/.gitconfig to the vagrant user’s home directory on the guest machine so you will not have to run git config –global every time we provision a new VM:

And here is another example which we use Vagrant File Provisioner to copy a folder from host to guest:

4. Conclusions

The article has just shown us how to copy files and folders from host to guest in Vagrant by using different approaches such as using synced or shared folders, vagrant-scp plugin and scp command. And we also see that we can use the same approaches to copy files or folders from guest to host in Vagrant.

Below are other related articles for your references:

Vagrant Tutorial

Set Name, Number of CPUs, Memory and GUI Mode for Vagrant Virtual Machines

Running Vagrant SSH on Windows

Basic Vagrant Commands

How to Add a Vagrant Box from Local or Remote

Where Does Vagrant Store Its Boxes After Downloading?

How To Change Vagrant Virtual Machine Name

0 0 vote
Article Rating