Vagrant with NFS and public_network

Vagrant with NFS and public_network. You can use NFS in vagrant, but it requires a private_network to work; This is fine if you are doing all your devlopment from a single machine and dont need to access the vagrant box from your network, but if you need to run some testing from an extrenal machine (eg you have Kali on a laptop and want to perform an sqli scan) then you will need a public_network.

Vagrant with NFS and public_network is a great way to share directories from your host machine with your vagrant box – you can edit code and it is instantly reflected inside your vagrant environment, allowing for very fast development time and testing of ideas / branching / debugging, etc. Using multiple networks in a vagrant machine gets round the caveat of NFS requiring a private network and having access to the vagrant box from your network.

Fortunately you can define more than one network in a vagrant machine (as you can in a real machine).

# original private_network needed for NFS
  config.vm.network "private_network", ip: "192.168.100.100"
  config.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)", ip: "192.168.1.20"

This will add two networks to your vagrant machine.

You can then add multiple NFS shares (note that if you are on OSX Sierra you will need to specify the bsd__nfs_options as below, and also vers=3 for NFSv3, or you will ned up with duplicate files and the shared content not matching the actual content! ):

I use the following config to share my devops scripts (which have their own git repo) with my vagrant machine and also have my current project shared inside vagrant. In work we have a shared config that has 5 or 6 different projects – system wide libraries, current project, shared unit tests, test data repo, devops helper code, etc

Vagrant with NFS and public_network

# share the current folder in /var/www/vhosts/development
    config.vm.synced_folder ".", "/var/www/vhosts/development", 
    create: true, 
    type: 'nfs', 
    mount_options: ['actimeo=2', 'vers=3', 'tcp'],  
    linux__nfs_options: ['rw','no_subtree_check','all_squash','async'],
    bsd__nfs_options: ['rw','no_subtree_check','all_squash','async']

    # share another folder
    config.vm.synced_folder "~/devops", "/var/www/vhosts/devops",
    create: true, 
    type: 'nfs', 
    mount_options: ['actimeo=2', 'vers=3', 'tcp'],  
    linux__nfs_options: ['rw','no_subtree_check','all_squash','async'],
    bsd__nfs_options: ['rw','no_subtree_check','all_squash','async']

I hope you enjoyed my Vagrant with NFS and public_network tutorial – you might like my post on DVWA in a vagrant box.

Leave a Reply