Tuesday, September 7, 2010

NFS Client - Server Configuration

NFS - Network File Service - is a System V managed service which is basically used to serve files from a remote computer to your local machine. It consists of two parts; the server part and the client part

The Server Configuration:
Two main RPM packages are needed to configure NFS as a server on your RedHat machine: 
portmap - needed because NFS server is an RPC (Remote Procedure Call) service.
nfs-utils - Which contains the essential files and utilities like the exportfsshowmount, scripts installed in the /etc/rc.d/init.d/ directories like nfs etc which form a part of the NFS server

The NFS server software is depended on three facilities for its work:
  • portmap : Which maps the calls made from the other machines to the correct RPC service.
  • nfs (in kernel): Translates NFS requests into requests on the local filesystem and
  • rpc.mountd : Which mounts and unmounts filesystems. 
All the above three run as daemons and are started at boot time from the portmap and nfs System V initialization scripts. See /etc/rc.d/init.d/ directory. 

To verify that these services are running, do eaither of the following:
# rpcinfo -p
service nfs status
service portmap status
To verify that these services are running on a remote server (say myserver), do:
rpcinfo -p myserver
Once you have made sure the above services are running, the next step is to decide which all directories and filesystems are to be made available for sharing across the network via NFS. 
This is listed in the /etc/exports file.
#FILE: /etc/exports
/engineering *.myserver.com(ro,sync) otherserver.india.com(rw,sync)
/root/presentations macmot.dc.com(rw,sync)
The above listing is a part of my /etc/exports file. Each line contains one exported directory and its access permissions. For example, the first line exports the /engineering directory to all the clients in the 'myserver.com' domain with read-only access and 'otherserver.india.com' with read-write access. And the data is synchronized on to the disk on each access. The third line shows that you can also give a valid IP address/subnet mask to specify a range of addresses to which the particular directory is exported. 
Note: You can use wild cards like * and ? for the purpose. Care should be taken to see that the options are not seperated from the hostnames with white space. If white space exists between a hostname and an option, it is treated as two distinct export destinations and the option will apply to a "world export". 

Another thing of significance is that all entries in the /etc/exports are exported with root_squashingturned on. This ensures that a person having root access on a remote machine is not given root accessto the files in the server machine.This can be negated by using the no_root_squash option. 

Once you have finished editing the /etc/exports file, you have to make the NFS server read the/etc/exports file. This you can do eaither by rebooting the machine or you can run the command : 
exportfs -a
This exports all the shares listed in the /etc/exports file to the NFS server. You have to run this command each time you make changes to the /etc/exports file.
You can check the proper operation of your NFS server by running eaither of the two commands:
exportfs -v
service nfs status
Client side configuration : 
The main job here is to mount the NFS share exported by the remote computer (let us sayserver_one). This can be achieved by a few different ways: 
  1. Specify it in the /etc/fstab file.
  2. Use the autofs daemon to mount NFS shares on demand and unmount them when idle.
  3. You can mount the NFS shares manually as root, using the mount command. 
But before that, you have to know which are the directories exported by the remote NFS server. This is achieved by the command: 
showmount -e
Where hostname is the remote NFS server hostname. When you mount an exported directory from an NFS server, you can access it as if it were local to your machine. 
#File: /etc/fstab
server_one:/sales /mnt/pub nfs defaults 0 0
The above listing shows that the /sales directory from the NFS server server_one is mounted locally at/mnt/pub directory. The /etc/rc.d/init.d/netfs script will mount any network filesystems that are configured to be mounted at boot time such as the /etc/fstab listing above. 

Some NFS specific options that can be used with mount or in /etc/fstab include:
  • rsize=8192 and wsize=8192 - will speed up NFS thoughtput considerably.
  • soft - Processes return with an error on a failed I/O attempt.
  • hard - will block a process that tries to access an unreachable share.
  • intr - allows NFS requests to be interrupted or killed if the server is unreachable.
  • nolock - Disables file locking (lockd) and allows interpolation with older NFS servers. 
For example:
mount -t nfs -o rsize=8192,wsize=8192,soft,rw server_one:/sales /mnt/sales
The kernel automounter facility, autofs, provides the ability to mount the NFS shares on demand and unmount them when they are idle in a way that is transparent to the end user. Check whether you have the autofs RPM installed in your machine. Then turn it on using the command: 
chkconfig autofs on
Now edit the file /etc/auto.master to mirror your configuration. Check man auto.master for the details of the syntax. Lastly start the autofs service: 
service autofs start
Note: You have to restart the autofs daemon each time you make changes to the/etc/auto.master file. Now you have successfully configured the NFS server and client.