Automount/autofs is a Linux daemon which allows for behind the scenes mounting and unmounting of NFS exported directories. Basically with autofs NFS shares will be automatically mounted when a user or system attempts to access their resources and disconnected after a period of inactivity. This minimizes the number of active NFS mounts and is generally transparent to users.
First we’ll install autofs and include the NFS client:
# sudo apt-get -y install autofs nfs-common
Now we need to define autofs maps, which are basically configuration files which will tell autofs what types of NFS mounts to define. With autofs there are two types of maps, direct and indirect. With direct maps we define a list of filesystems to mount that will not share a common higher level directory on the client. With indirect maps they share a common directory hierarchy, and a bit less overhead is required. The advantage with direct maps is that if the user runs a common such as “ls” on the directory structure above the directory will show up, whereas with indirect maps they have to actually access the contents of the directory itself. This can cause some confusion for users because running “ls” on a directory containing indirect mounts will not show the autofs directories until after the contents within have been accessed. Indirect maps may also not be available by browsing the directory structure with a GUI file manager, you’ll need to specifically type in the path to get to it. In this example I’ll show the use of both types of maps.
First we need to define a master map, which basically will tell us what indirect/direct maps we want to use and the appropriate config files to read. Edit the “auto.master” file and append this content:
# sudo vi /etc/auto.master
# directory map
The first entry is an indirect map, all of the mounts will be created under /server1 directory and the configuration will be read from “auto.server1”. With direct maps we use a special character “/-” and will read the config from “auto.direct”.
Time to set up our indirect maps:
# sudo vi /etc/auto.server1
apps -ro server1:/nfs/apps
files -fstype=nfs4 server1:/nfs/files
The first column represents the subdirectory to be created under “/server1”. The second shows the host and NFS export, with apps mounted as read only. Notice with files that we specify NFSv4, obviously the NFS share must be compatible with NFSv4. Not specifying an “fstype” should revert autofs to using NFSv3.
Now we’ll do a direct map:
# sudo vi /etc/auto.direct
Setting up a direct map is basically like configuring an NFS mount in the “/etc/fstab” file. Include the full directory name from /, and include the host and NFS export names. The options that we used with the indirect maps above can also be used on direct map mounts if desired.
While I don’t believe it is explicitly required, I have had difficulty accessing NFS shares unless the directory for the direct map is created manually before running autofs:
# sudo mkdir /mnt/data
Now we need to configure automount/autofs to start when the system starts:
# sudo update-rc.d autofs defaults
I am using the “update-rc.d” command, which has very similar functionality to the “chkconfig” utility in the Red Hat world. It will create links in the various runlevel directories to the daemon’s initialization script. Autofs is the daemon name, and using the defaults options tells the system to start autofs in runlevel 2-5, and stop in 0, 1, and 6. Most of the startup runlevels are not used with Debian/Ubuntu, runlevel 2 is the one that matters most to us.
Now restart the autofs daemon to pickup the new configuration:
# sudo service autofs restart
Browse to the newly established autofs mounts or type in the full path where you mounted the NFS export if using indirect maps. The files on your NFS host should now be available on your client!
Recently the latest version of Scientific Linux 6 was released. Scientific Linux is a distribution which uses Red Hat Enterprise Linux as its upstream and aims to be compatible with binaries compiled for Red Hat Enterprise. I am really impressed with the quality of this distro and the timeliness with which updates and security fixes are distributed. Thanks to all the developers and testers on the Scientific Linux team! Now let’s move on to configuring an NFS server on RHEL/Scientific Linux.
In my environment I will be using VMware ESXi 4.1 and Ubuntu 10.10 as NFS clients. ESXi 4.1 supports a maximum of NFS v3 so that version will need to remain activated. Fortunately it appears as though out of the box the NFS server on RHEL/Scientific Linux has support for NFS v3 and v4. Ubuntu 10.10 will by default use the NFSv4 protocol.
First make a directory to place the NFS export mount and assign permissions. Also open up write permissions on this directory if you’d like anyone to be able to write to it, be careful with this as there are security implications and anyone will be able to write that mounts the share:
# mkdir /nfs
# chmod a+w /nfs
Now we need to install the NFS server packages. We will include a package named “rpcbind”, which is apparently a newly named/implementation of the “portmap” service. Note that “rpcbind” may not be required to be running if you are going to use NFSv4 only, but it is a dependency to install “nfs-utils” package.
# yum -y install nfs-utils rpcbind
Verify that the required services are configured to start, “rpcbind” and “nfslock” should be on by default anyhow:
# chkconfig nfs on
# chkconfig rpcbind on
# chkconfig nfslock on
Configure Iptables Firewall for NFS
Rather than disabling the firewall it is a good idea to configure NFS to work with iptables. For NFSv3 we need to lock several daemons related to rpcbind/portmap to statically assigned ports. We will then specify these ports to be made available in the INPUT chain for inbound traffic. Fortunately for NFSv4 this is greatly simplified and in a basic configuration TCP 2049 should be the only inbound port required.
First edit the “/etc/sysconfig/nfs” file and uncomment these directives. You can customize the ports if you wish but I will stick with the defaults:
# vi /etc/sysconfig/nfs
We now need to modify the iptables firewall configuration to allow access to the NFS ports. I will use the “iptables” command and insert the appropriate rules:
# iptables -I INPUT -m multiport -p tcp --dport 111,662,875,892,2049,32803 -j ACCEPT
# iptables -I INPUT -m multiport -p udp --dport 111,662,875,892,2049,32769 -j ACCEPT
Now save the iptables configuration to the config file so it will apply when the system is restarted:
# service iptables save
Now we need to edit “/etc/exports” and add the path to publish in NFS. In this example I will make the NFS export available to clients on the 192.168.10.0 subnet. I will also allow read/write access, specify synchronous writing, and allow root access. Asynchronous writes are supposed to be safe in NFSv3 and would allow for higher performance if you desire. The root access is potentially a security risk but AFAIK it is necessary with VMware ESXi.
# vi /etc/exports
Configure SELinux for NFS Export
Rather than disable SELinux it is a good idea to configure it to allow remote clients to access files that are exported via NFS share. This is fairly simple and involves setting the SELinux boolean value using the “setsebool” utility. In this example we’ll use the “read/write” boolean but we can also use “nfs_export_all_ro” to allow NFS exports read-only and “use_nfs_home_dirs” to allow home directories to be exported.
# setsebool -P nfs_export_all_rw 1
Now we will start the NFS services:
# service rpcbind start
# service nfs start
# service nfslock start
If at any point you add or remove directory exports with NFS in the “/etc/exports” file, run “exportfs” to change the export table:
# exportfs -a
Implement TCP Wrappers for Greater Security
TCP Wrappers can allow us greater scrutiny in allowing hosts to access certain listening daemons on the NFS server other than using iptables alone. Keep in mind TCP Wrappers will parse first through “hosts.allow” then “hosts.deny” and the first match will be used to determine access. If there is no match in either file, access will be permitted.
Append a rule with a subnet or domain name appropriate for your environment to restrict allowable access. Domain names are implemented with a preceding period, such as “.mydomain.com” without the quotations. The subnet can also be specified like “192.168.10.” if desired instead of including the netmask.
Append these directives to the “hosts.deny” file to deny access from all other domains or networks:
And that should just about do it. No restarts should be necessary to apply the TCP Wrappers configuration. I was able to connect with both my Ubuntu NFSv4 and VMware ESXi NFSv3 clients without issues. If you’d like to check activity and see the different NFS versions running simply type:
Good luck with your new NFS server!
Generally my preference is to use Linux as an NFS server. On the internet you will see frequent reference to the belief that NFS works better on Linux/UNIX. Recently I decided to try and set up NFS services on Windows to see how well it would perform. In this tutorial I will set up Services for UNIX 3.5 on a Windows 2003 R2 server and configure it using the User Name Mapping service to allow a VMware ESXi to use it as a datastore for VMs or backups via non-anonymous connections.
First off grab the Windows Services for UNIX (SFU) installation files here. Extract the files from the download file and run the setup.
Select Custom Installation and click Next.