If you’re like me and use NFS to share files between multiple servers, you may occasionally run into a connection issue when you reboot - if the client comes up before the server, it won’t mount the NFS filesystems automatically. If you’re around, you can just remount them manually, but if it happens when you’re away you could be out of luck. One way to make sure this doesn’t happen is to use autofs for your NFS mounts. It’s super easy to set up, and once it’s configured the filesystems will mount and remount automatically whenever they are accessed, as long as the remote system is available.
First, you need to install autofs. On Ubuntu or other debian-based distros, you can install it by opening a terminal and running the following command:
sudo apt install autofs
On Centos, use yum instead
sudo yum install autofs
Once the package is installed, you need to edit a couple of configuration files using your favorite text editor. Open
/etc/auto.master, and add a line like so:
/mnt/nfs /etc/auto.nfsdb --timeout=0
Adjust the path to point to the parent folder that you want your NFS shares to mount in. In my example, I entered
/mnt/nfsso my NFS volumes would be inside that, like
/mnt/nfs/movies. You will only want the parent folder in this file, the actual NFS mounts themselves will be configured in another file. If this path doesn’t already exist, you must create it.
I set the
timeoutto 0 so the filesystem will stay mounted indefinitely, but if you would prefer the filesystem to unmount automatically, you can set the timeout in seconds and the mounts will be unmounted if there is no access in that much time.
Save the file once you’ve adjusted it to match your path and timeout preferences
Next, create the file
/etc/auto.nfsdband open it in your favorite text editor again. Inside this file is where you are going to enter all of the different NFS mounts. For each mount, you will need to add a line similar to the following:
tv -fstype=nfs,ro,timeo=100,noatime 192.168.1.10:/mnt/user/tv
There are a couple things you’ll need to adjust here. The first parameter is the name of the folder to create on your local system. This folder will be created inside the parent folder you defined above. So, in my example, the
tvfolder will be mounted inside the
/mnt/nfsfolder I entered in step 2.
The middle parameter is the options for the filesystem
-fstype=nfsdoesn’t need to change, it just means that this folder will be mounted as an NFS filesystem.
romeans to mount the filesystem read-only. You could adjust this to
rwif you want to be able to write to the filesystem from the client, as long as the server is configured to allow writes.
timeo=100is how long the NFS client should wait before retrying, in deciseconds (1/10 of a second). By default, it is set to 600 (60 seconds), which might be necessary for truly remote filesystems, but on a local network 10 seconds should be more than enough for the server to respond. If you get timeout errors you can adjust this higher.
retrans=2is the number of times the NFS client will attempt to retry the connection before giving up. As long as your local network isn’t having any issues, your files should transfer on the first try.
noatimemeans the access time on the source files won’t be updated when you read them. You can just leave this as is.
The last parameter is the IP and path of the remote NFS filesystem. Adjust it as necessary, following the pattern
<path/to/folder>(note the colon separating IP and path is required).
Add additional lines for every NFS path you want auto-mounted. Once you’ve added all the paths, save the file.
Now that the auto-mounting is configured, you’ll need to disable the standard mounting in
fstab, if you had it configured. Next, stop any running programs that are accessing files inside these mounts. Third, unmount any of the folders that were mounted by fstab. Finally, remove these folders. You only want to remove the child folders (so if your parent folder in step 2 is
/mnt/nfsand you had a child folder
tv, you only want to remove the
tvfolder). Don’t worry, the folders will be recreated shortly.
Now that the folders are gone, you can start the autofs service. Run the following command, which should work in both Ubuntu and Centos and any other distro that used systemd.
sudo systemctl start autofs.service
Verify it started properly with:
systemctl status autofs.service
As long as the status shows
active (running)then you can continue.
If you run a distro that isn’t using systemd, you can start autofs by running
sudo /etc/init.d/autofs start
Change to one of the folders you want mounted and make sure you can see files in the folder.
cd /mnt/nfs/tv && ls
If you see your files, then you’re nearly done. If not, double-check the files above were created properly and try again.
Finally, in Ubuntu or Centos, you need to enable the autofs service so it starts on boot.
sudo systemctl enable autofs.service
With autofs installed and enabled, your NFS mounts should remount automatically if you ever reboot the server. Please note that these filesystems aren’t actually mounted until their first access. If you view the contents of the parent folder from step 2 before you’ve tried to access anything inside the folder, the NFS folders from step 3 might not show up. The moment you, or any other program running on your system, tries to access one of the folders it will be mounted automatically.