Create a shared directory in local network using autofs + NFS

Hi fellows,

Use a shared directory is a very useful feature when working on network not only to share files between hosts but also, when executing a distributed program (such as a crawler or a parallel program), to gather all data into a single directory in a given machine. NFS
can handle this issue. It can be installed through apt-get tools:

sudo apt-get install nfs-kernel-server

It creates the file /etc/exports that specifies which filesystems should be exported. Create a directory, add it to exports file and restart nfs server:

hostA# sudo mkdir /share
hostA# sudo echo "/share *(rw,sync)" >> /etc/exports 
hostA# sudo /etc/init.d/nfs-kernel-server restart

To test if it is working you can use mount command with nfs filesystem. So, login on a remote host and mount it manually:

hostB# mkdir /share
hostB# sudo mount -t nfs hostA:/share /share

To avoid need to mount it every time NFS server restart, you can setup NFS client host to mount it “automagically”. One alternative is add it to /etc/fstab to be mounted on startup. Another option, recommeded for secure reasons, is use autofs tool with guest mode (only mount when accessed), which not requires mounting all hosts in /etc/fstab. It also can be installed through apt-get tools:

hostB# sudo apt-get install autofs

The autofs settings are performed by /etc/auto.* files, in the client side. So create a file, for instance, /etc/auto.share, put the base directory to be used to mount all network hosts, add this file to file /etc/auto.master and reload autofs:

hostB# sudo echo "*  -fstype=nfs   &:/share/&" > /etc/auto.share
hostB# sudo echo "/share /etc/auto.share --timeout=30" >> /etc/auto.master
hostB# sudo /etc/init.d/autofs reload

Let’s change the /etc/exports file, in NFS server side, to it export not the base directory but the host directory:

hostA# sudo sed -i "s/share/share\/hostA/" /etc/exports

Now if you change the directory to /share/hostA, in NFS client side, it will try to mount it through NFS on hostB, however it will give a “Permission denied error”. I’m not sure, but I suppose it is due that the /share/hostA is under autofs directory, so the turnaround to it was create a ext3 partition, instead of a directory in / partition (not covered here, some alternatives to manage partitions are fdisk and gparted). And don’t forget to mount it explicitly (regards to logytech support!).

See ya