Automatically mount/unmount an NFS directory with “autofs”

The goal is to make an NFS-exported directory permanently available on a client. Normally this would be done by modifying the file “/ etc / fstab” so that the NFS directory is automatically included at system startup. However, “fstab” has the disadvantage that mounting fails if no network is available at system startup. Usually you create a script that hangs the directory only when a network connection was detected. Or you can enter the mount command in the file “/etc/rc.local”.
But we want a solution that really works, while avoiding error-prone pull-ups. The establishment of NFS often fails because it does not permanently get the shares on the clients. Therefore we integrate the exported directory with the tool “autofs”.
With “autofs” partitions can be mounted automatically when needed and automatically unmount after a long period of non-use.
Note: autofs uses automount. In addition, the exported directory is always mounted on first access and not automatically at system startup. This is not a problem if you accept that there will be a short delay on first access.
Solution: Permanently mount NFS directory on a client
First, we need to install “autofs” on the client:
sudo apt-get install autofs
Then we create a map file:
sudo nano /etc/auto.nfs
There we enter the exported directory:
public -fstype = nfs, rw, retry = 0 192.168.1.2:/home/public
Then save and close the file.
Then we open the master file in which the map files are entered.
sudo nano /etc/auto.master
At the end of the file we add the following line:
/home/nfs/etc/auto.nfs
Then save and close the file.
Then the modified map file must be reloaded by a restart of “autofs”. It will be taken into account for future integration.
sudo service autofs restart
Now you can test if the exported directory is included.
mount
View of the directory:
ls /home/nfs/public
Here, the contents of the exported directory should be displayed by the NFS server. Make sure you have at least one file in it to see on the client if the mount worked. If the directory on the server is empty, you can not tell the difference between mounted and not mounted on the client.
Whether “autofs” really works, you can only check by restarting the client.
sudo reboot
As a rule, the shared directory should be available after the first access.
ls /home/nfs/public
Troubleshooting: NFS client
If the integration of the exported directories does not work, check if any NFS shares are available.
showmount -e 192.168.1.2
If so, then there may be a problem with “autofs”. To do this, check the syslog file, which may contain error messages about “automount”.
sudo cat /var/log/syslog | grep -i automount
Leave a Reply