Skip to Content

Mount NFS shares on a running Vagrant box

Posted on

I use Vagrant boxes on Virtualbox for pretty much all my development work these days. Often I need to mount a new shared folder to the box I’m working on, but updating the Vagrantfile and rebooting takes too long to my liking.

It’s much faster to mount with NFS directly when it’s a one-off!

This example assumes that your Vagrant box (or Virtualbox instance) has the IP address 33.33.33.58 and can access the host machine at 33.33.33.1.

If you don’t know the IPs, you can use the hostname -I command on your Linux guest machine, and ifconfig | grep inet on your host to look for them. Search for the IPs that are in the same range on both machines.

On your host machine

First edit the /etc/exports file on your macOS or Linux host. This file defines the list of exported directories and which remote IPs are allowed to access them. Add the following line:

"/Users/stevenrombauts/foobar” 33.33.33.58 -alldirs -mapall=stevenrombauts:staff

This defines a new export for the /Users/stevenrombauts/myfiles directory. Make sure to put in your username for the -mapall argument so the NFS server knows which user to map file permissions to on the host.

Now verify the exports file before continuing:

nfsd checkexports && echo "Exports file is valid"

and restart the NFS service to pick up the changes:

sudo nfsd restart

You can use the showmount command to review all the available NFS mounts on the host:

showmount -e 33.33.33.1

The new entry should be in that list.

On your Linux guest

Make sure that an NFS client is installed on your Linux machine:

cat /proc/filesystems | grep nfs

The output should contain nfs references, for example:

nodev nfs
nodev nfs4
nodev nfsd

If they’re missing, you’ll need to install the NFS package. You can do so on Ubuntu with sudo apt-get install nfs-common or on CentOS with sudo yum install nfs-utils yum install nfs-utils.

Now create the directory where you want to mount the NFS share to:

mkdir -p /home/vagrant/foobar

If it already exists, make sure it’s empty.

Now mount the shared directory. Make sure to replace the host IP and correct the directory paths:

sudo mount -t nfs -v -o rw,vers=3,udp,tcp 33.33.33.1:/Users/stevenrombauts/foobar /home/vagrant/foobar

Check the contents of the mounted directory to make sure the command was successful:

ls -alh /home/vagrant/foobar

For your information, to get the NFS status on the guest machine you can run this command:

nfsstat -m

All done, ready to continue coding!

comments powered by Disqus