In a previous article, I went over how to setup a network share using the traditional client/server approach. It’s a great way to reduce unwanted file duplication, and share items in a secure manner. But, what if we have a lot of NFS shares and we don’t want to clog up our /etc/fstab or we only want certain shares mounted on demand? Enter AutoFS.
The nice thing about AutoFS is that the logic does not change on the server side. We only need to install and configure the autofs utility. First thing is to run
sudo dnf install autofs
systemctl status autofs
This is totally fine for right now–just like with the nfs-server service. We can bring it up later when we’re ready. The first question to ask yourself is do you want an indirect or direct mapping. This will effect how you configure your Master Map file. Basically, there are two configuration components to autofs:
Master Map File-The main configuration file. By default /etc/auto.master
–There is only one master map file. If you do not want to use the default file (some admins say it can
overwritten with an update) then you would create a new file with the .autofs extension. Example,
/etc/auto.master.d/newmap.autofs
Map File–This will be specifically referenced in the Master Map file, and you can have multiple of these.
Deciding whether you want an indirect or direct mapping will change the syntax of the Master Map
file and the Map file. The naming convention for this file is auto.name. Example, auto.sharefiles.
Now, a direct mapping in the Master Map file (/etc/auto.master) will look like this:
/- auto.sharefiles
The first part (/-) tells the Master Map to look to the Map file for the absolute path to your mount point. The second part (auto.sharefiles) is the map file with the remaining configuration values.
The map file could look something like this:
/mnt/nfs_shares/share_files -ro 192.168.223.134:/export/nfs1
Look a little familiar? First part is the mount point (autofs will actually make the directories too). Second part is the mount options (these are optional), and third part is the source server and NFS share.
So, what is an indirect mapping then? Let’s set one up in the same environment I used before. I’m going to share user Henry’s home folder to server 2 (the client server)
sudo vi /etc/export
Let’s restart the nfs service and check what we’re sharingsudo systemctl restart nfs-serve
rshowmount -e
And let’s check that server 2 (client) is picking up the changes.showmount -e 192.168.223.135
Now to setup the Master Map file. I am going to call my Map file auto.henry (we haven’t made it yet, I am deciding on the naming convention now), and I am going to start the mount point at /this/is/fun. When we make the Map file, we will put in the last directory.vi /etc/auto.master
Time to make the Map file.
vi /etc/auto.henry
The breakdown is a little different with an indirect mapping. The first part is the relative path from the Master Map file. So, it will make the mount point /this/is/fun/stuff. I skipped the mount options because I am fine with the settings on the nfs server (rw,sync,no_root_squash). Finally, the nfs share IP address and share directory. Finally, turn the autofs service on.
sudo systemctl enable --now autofs
And the moment of truth.
Since we have rw permissions on here, any changes we make on server 2 (client) will be written back to Henry’s home directly. Currently, there is a file “henry_file.txt” that has no content to it.
Let’s add some text to the file and add another empty file to Henry’s home folder.sudo vi /this/is/fun/stuff/henry_file.txt
And we’ll make another file.sudo touch /this/is/fun/stuff/server2_file.txt
Another moment of truth. Let’s go back to server 1 and check Henry’s home folder.
So, mounting users’ home directories to other servers (clients) seem a little handy–especially if it’s on-demand with autofs. But, if you had 20, 30, 50 or 100+ users, it seems a little impractical. This is where indirect mappings really shine. Indirect mappings support the use of wildcards, so we can tell the nfs server to export the entire /home directory, and the Map file can use a wildcard to individually mount each users’ home folder. Let’s demonstrate.
I’m going to make changes on the nfs server so it exports everyone’s home directory and not just Henrys.
Make sure to restart the nfs-server service and check that changes are being picked up. On server 2 (client), we’re going to make changes to the /etc/auto.master file and create a new Map file auto.home. Note, in a production environment, it would be good to remove the auto.henry map file because we are no longer using it.sudo vi /etc/auto.master
We have to make the new map file, and the configuration is going to be a little different than before.vi /etc/auto.home
Here, we are using two wildcards. One for each users home directory mount point, and for each home directory being shared from the nfs-server. Restart the autofs service and head to the mount point.
At first glance, it may not seem like it’s working, but if you add the user’s name at the end of path……
And another user.
And one more.
Now if we go back and check the /server1/users directory.
This is where the autofs auto mounter capability really shows itself. The home user directories aren’t available until we specifically call them; then, once they are mounted (or cached) if you will, they will be available to us. The default timeout is 300 seconds (5 minutes) and can be changed in the /etc/autofs.conf file.
Hopefully this article was able to clear up any confusion around autofs or expand on what you already knew.