NFS: Network File System

This is a short tutorial on configuring a NFS server and connecting a client or two. If you want a more complete tutorial, check out the official tutorial on SourceForge.

This tutorial assumes you have a basic knowledge of networking as well as mounting file systems under Linux. If you aren't up to speed on mounting within Linux, read the mounting tutorial first.

What is NFS?

NFS is an acronym for Network File System. NFS allows you to mount a remote file system into your local root file system, providing fast access to network resources as if they were stored on your local disk. NFS is the most commonly used protocol for sharing files in Unix/Linux environments, there are alternatives, but we'll focus on NFS for now.

In order to use NFS, you need an NFS server (host) that contains the directories to share. The client will then connect to this host to mount the file system.

Configuring the server

First we need to start the NFS daemon, and turn it on at startup with the commands:

service nfs start
chkconfig nfs on

/etc/exports

Once the service is running, it's time to tell it what to share, this is done in the /etc/exports file. Entries in the export file are in the following format:

/directory network1(options) machine1(options)

For example, to share a directory out to anyone with the IP address 192.168.2.x or the computer name 'dingle' you would add a line:

/home 192.168.2.0/24(rw,sync) dingle(rw,sync)

The 'network1' or 'machine1' client names can be a fully qualified DNS name, an IP address or IP range. IP addresses are the preferred method. You can also use wildcards, for example '*.savagereactor.co.uk' or '192.168.', there were problems with wildcards on kernels 2.2 that were fixed in 2.2.19.

Options can be as follows, to use multiple options, separate them with commas:

ro
The shared directory is read-only, any write attempts will fail.

rw
The shared directory is read-write, clients can write to the directory depending on the file/directory permissions.

no_root_squash
This can be a big security risk, use with caution. By default if root on a client accesses a share, they access it with the user permissions of 'nobody' on the server, when this option is enabled, the client's root account has root access to the share.

no_subtree_check
If only part of the file system is shared, a routine called subtree checking verifies that a file requested by a client is in the appropriate part of the file system. If you are exporting the entire file system, using this option will speed up transfers.

sync
The NFS server tells a client machine a write operation is complete once it has passed the write operation to the file system, if the server reboots before the file system writes the data, data corruption can occur. The sync option prevents this from happening.

To apply the changes run the command 'exportfs'. If you receive a command not found, you can restart the NFS daemon using 'service nfs restart'.

Connecting a Client

To access the share from a client, you simply mount the share to a local directory:

mount machinename:/share_dir /mnt/point

The mount point needs to exist, the machine name can be the server's IP address or it's fully qualified DNS name.

Mount at Bootup with fstab

You can get NFS shares to mount automatically at system start up using fstab. Open /etc/fstab and create an entry similar to the one shown below.

#device mountpoint fs-type options dump fschk
machine_name:/share_dir /mnt/point nfs rw 0 0

The option specified in the above example (rw) sets the mount to read-write mode. I also recommend using hard and intr options explained below.

NFS mount options

soft
If a file request fails, NFS reports an error to the calling process. Most don't like this and can result in corruption and data loss.

hard
Programs accessing NFS mounts will hang if the server becomes unavailable, the process cannot be killed unless you specify the intr option. When the server becomes back online, the program will continue.

For more information on configuring fstab, see the mounting tutorial. There is also a process for optimizing the performance of NFS by setting the rsize and wsize options, I'm not going into that process here, but you can find it on the official NFS tutorial, see the related information below.

You should now have a working NFS server with at least one connected client, enjoy.

Related Information: