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:
To configure another synced folder, you can follow the below syntax:
1 2 3 4 5 |
Vagrant.configure("2") do |config| # other config here config.vm.synced_folder "/home/data/", "/data/shared" end |
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.
1 |
vagrant plugin install vagrant-scp |
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:
1 |
vagrant scp <some_local_file_or_dir> <somewhere_on_the_vm> |
Let’s see an example which we copy a host folder /data/examples/spring-boot to Vagrant VM:
1 |
vagrant scp /data/examples/spring-boot /home/vagrant |
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:
1 |
vagrant scp <some_local_file_or_dir> [vm_name]:<somewhere_on_the_vm> |
Let’s see an example which we copy the above folder to the webserver VM:
1 |
vagrant scp /data/examples/spring-boot webserver:/home/vagrant |
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:
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:
1 |
scp -P 2222 /data/examples/spring-boot/gs-spring-boot-master.zip vagrant@127.0.0.1:/home/vagrant |
And here is an example which copies a folder recursively from host to guest:
1 |
scp -P 2222 -r /data/examples/spring-boot vagrant@127.0.0.1:/home/vagrant |
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:
1 2 3 4 5 |
Vagrant.configure("2") do |config| # ... other configuration config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig" end |
And here is another example which we use Vagrant File Provisioner to copy a folder from host to guest:
1 |
config.vm.provision :file, source: '/home/java-ex', destination: "/tmp/examples" |
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:
Set Name, Number of CPUs, Memory and GUI Mode for Vagrant Virtual Machines
Running Vagrant SSH on Windows
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