Updated: This will work for both Ubuntu 12.04 and 14.04
After looking at many guides, I was unable to get it to work. Either the guides were poorly worded or confusingly made for me. Hopefully this helps someone in a similar situation as me.
In my setup I wanted to share 2 things, my external drive that has a ton of stuff on it mounted in /mnt/data, and the /var/www which hosts my internal webpage on Apache. Shared so I can edit webpages on my main box. I also wanted to use autofs instead of fstab to mount the shares on the client to learn how to get around network connectivity issues. fstab will wait for network conenctivity in order to mount those shares. This is ok except on wireless connections. Since they are user level, fstab/mount will fail to detect any network connections.
- Install NFS server:
sudo apt-get install nfs-kernel-server
- Edit /etc/exports to set up what directories you want shared on your network, mine looks like this:
sudo vim /etc/exports
/mnt/data 192.168.1.0/24(rw,no_subtree_check,async) /var/www 192.168.1.0/24(rw,no_subtree_check,async)
Note: Change the 192.168.1.0/24 part to match your network.
- Start the server.
sudo service nfs-kernel-server start
First is to figure out where exactly you would want the directories to appear. I decided to put them inside of /mnt/servername (don’t put them directly in /mnt. It will mess up any mounts that are already there).
- Install the nfs client and automount utilities:
apt-get install nfs-common autofs
- Edit /etc/auto.master to define the file for the NFS shares.
sudo vim /etc/auto.master
- I commented “+dir:/etc/auto.master.d” and “+auto.master” for this setup as they just produced non-fatal errors anyway.
- Add the line at the bottom similar to this:
/mnt/servername /etc/auto.nfs --ghost
- Note: /mnt/servername is the “key”, where the directories will appear when mounted. The ghost option creates the directories and makes them visible for easier use. /etc/auto.nfs is the location of the file we will create next. The name can be whatever you want.
- Edit /etc/auto.nfs
sudo vim /etc/auto.nfs
- Make it look something like this, modifying for your environment of course:
www 192.168.1.999:/var/www data 192.168.1.999:/mnt/data
This will create 2 directories, /mnt/servername/www and /mnt/servername/data, and map them to the exports defined in the server earlier. I used my server IP address to avoid any DNS issues that might be lurking, but in reality you might want to use the server name instead.
- Restart the autofs service.
sudo service autofs restart
A quick note about the async option in /etc/exports:
async This option allows the NFS server to violate the NFS protocol and reply to requests before any changes made by that request have been committed to stable storage (e.g. disc drive). Using this option usually improves performance, but at the cost that an unclean server restart (i.e. a crash) can cause data to be lost or corrupted.
sync Reply to requests only after the changes have been committed to stable storage (see async above).
I’m using async as I expect to not have many crashes (or read/writes since it’s just me), and the server happens to be an old laptop with a good battery, so power outages won’t be a problem. In a more larger environment with a large amount of use or where crashes/power outages are likely, stick to sync.