Sharing Files Between Your Guest and Host Machines

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:05
    with Emma Jane Westby
  • 0:08
    In the previous lesson, if we wanted to edit any
  • 0:11
    of the files on our server, we had to log into the server
  • 0:14
    and use some crazy command line tool called vi or Vim.
  • 0:18
    Now I happen to actually really like Vim.
  • 0:21
    But I recognize that this is not probably
  • 0:24
    part of your normal tool kit.
  • 0:26
    The whole advantage of having Vagrant is that you can use
  • 0:28
    your normal tool kit on the host machine, but then
  • 0:31
    also have a server which is running alongside.
Loading ...

Sharing Files Between Your Guest and Host Machines

Loading...

Typically when we work with a Web server, we edit our files locally, and then "push" them up to our Web server (perhaps using SCP, rsync, Git push, or maybe even FTP). The same will be true with our Vagrant server: you need a way to get files from your host machine, to the guest server. You could either use SCP, but we will enable file sharing to make life a lot easier. With file sharing enabled you will be able to drap-and-drop files into specific directories within your Vagrant instance using your local file manager (e.g. Finder or Windows Explorer). The files will be immediately shared, and recognized by your server, allowing you to continue working on the files with your favorite IDEs and code editing tools without the upload step. Technically, enabling file sharing isn't necessary, but I think you'll agree it is a lot more convenient than SCPing files to your guest server.

Lesson Outcomes

By the end of this lesson you will be able to share files between your host and guest machines without the use of SCP. After enabling file sharing, you should be able to copy a file into the shared folder in your Vagrantfile and have it automatically appear inside the Vagrant instance. e.g. you can load a static web page created on your local machine without having to use SCP.

Lesson Summary

As long as you're using basic file sharing (no NFS), it should work "out of the box" for Windows 8, and OSX.

  1. Configure synced folder in Vagrantfile.
  • Locate and remove: # config.vm.synced_folder "../data", "/vagrant_data"
  • In its place, add: config.vm.synced_folder "docroot", "/var/www"
  1. Create a new directory using the name identified in the file Vagrantfile.
  • $ mkdir docroot
  1. Reload the vagrant instance (and restart the server). The contents of /var/www will appear in docroot.
  • $ vagrant reload
  • $ vagrant ssh
  • $ sudo /etc/init.d/apache start
  • $ exit
  1. Edit the Web page from the host machine.
  2. Refresh the browser to see the changed file.

Gotchas

This lesson was recorded using VirtualBox 4.3.6 and Vagrant 1.4.3. There is a known issue for file sharing with VirtualBox 4.3.10. If you get the following error message: "Failed to mount folders in Linux guest. This is usually because the "vboxsf" file system is not available." you have encountered this bug. Consider downgrading VirtualBox to 4.3.8, or follow the troubleshooting tips in the issue queue for your particular host machine.

If you are having problems with file permissions / ownership, try editing the permisisons from the host machine, not the guest machine. This is most noticeable when you are creating a new Drupal instance and there are folders / files created automatically by the system. Vagrant won't give a warning to explain why it won't change permissions, it simply doesn't show any changes after running chommands such as: chmod +w <directory_name>.

If you are still having problems with silent file permissions, update your Vagrantfile as follows:

  1. Locate the setting for config.vm.synced_folder
  2. Add a server-friendly owner, by adding the following to the end: , :owner => "www-data"
  3. Add a server-friendly group, by adding the following to the end: , :group => "www-data"

If you're STILL having problems, update your Vagrantfile to give more permissive defaults.

  1. Locate the setting for config.vm.synced_folder and add the following to the end: , :mount_options => ['dmode=775', 'fmode=664']
  2. The final line should be something like this (all one line): config.vm.synced_folder "docroot", "/var/www", :owner => "www-data", :group => "www-data", :mount_options => ['dmode=775', 'fmode=664']
Downloads: 
Log in or sign up to download companion files.

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.