Setting up an NFS File Server
An NFS file server can provide a variety of services to Linux machines on a network. It can provide a place to store files for a machine to use or to write to, it can be used to allow machines to boot a root filesystem image stored on the NFS server, it can provide a place to store filesystem images when they're captured from MTD flash, or it can be connected to a desktop system to provide a common file store.
NFS stands for Network File System. NFS has been in existence since 1984, and is now on version 4.1. Sun Microsystems was the original developer behind NFS, creating the first version in 1984 but only using it internally for the first five years. Version 2 was released in 1989, and was the first version Sun released to the public. Version 3 was released in 1995, and added support for files larger than 2 GB, asynchronous writes, performance improvements and support for TCP. Version 4 was released in late 2000 and includes enhancements which are mostly useful for servers. Version 4.1 was released in 2010, and includes enhancements for clustered server usage. Version 4.2 is currently in development. For embedded use, version 3 works well and is simpler to configure than version 4, so version 3 is described here.
NFS Server Fundamentals
An NFS server requires some services to run. These are a portmapper daemon, RPC support, and a security service. NFS also requires tools, both for monitoring and for mounting filesystems. Finally, NFS servers require a configuration file.
NFS Services and Tools
On Ubuntu, Debian, and similar systems, the services and tools required by an NFS server can be installed with the following command:
On other distributions, check with your package manager for similar packages.
Configuring the NFS Server
The NFS server's shares are configured in the
/etc/exports file. Each share takes a list of option names to configure its behavior. Before configuring the NFS server, the following decisions need to be made (and a list of options created) based on the intended use(s) of the NFS share being created:
- Do NFS clients need the ability to write to the NFS share? If so, it needs to be exported
rw. Otherwise, export it as
- If the filesystem will be writeable, is performance or data integrity more important? If performance,
asyncwill provide more performance at the risk of lost or corrupt data should the NFS server shut down unexpectedly during a write operation. Otherwise,
syncshould be used.
- Is reliability or security more important? If reliability is, use
no_subtree_check. Otherwise, use
- Is user level access control required? If not, getting the NFS server working is much simpler with the
Once the above decisions have been made for a share that's being created, the new NFS share needs to be configured in the
/etc/exports file. There are three parts to the configuration line that specifies a share:
- location - specifies the directory on the NFS server's filesystem which will be exported as an NFS share.
- address - specifies an IP address, a range of IP addresses, a hostname, or a hostname wildcard of machine(s) which are allowed to connect to this NFS share.
- options - the list of options gathered above. Each option is separated by a comma, with no spaces.
For example, to export the
/srv/fs directory to all machines on a
192.168.1.0/24 network, read-only, with reliability and performance options turned on:
To export the
/srv/logs/ directory as a writeable NFS share with data integrity and reliability options on the
To export the
/srv/homes directory as a writeable NFS share with data integrity, reliability and user level access control on the
Adding any of the lines listed above will create the NFS share specified by the line, assuming the directory specified in the line exists. After adding an export line to
/etc/exports, run the following command to notify the NFS server of the configuration change:
In order to test to ensure that an NFS share was exported correctly, an NFS client needs to be installed on a second machine. If this machine is an EMAC board, the NFS client software should already be installed. On a Debian or Ubuntu system, the following command will install the needed packages:
Now, to mount the
/srv/fs share exported above with a server address of
192.168.1.20 on the local directory, /srv/nfs:
To verify that it mounted:
In the output is the line which shows the mount. The files in the directory can be listed:
Configuring a Client
In the testing section is shown a method of mounting an NFS share on a temporary basis. For many uses of an NFS server, however, having the NFS client automatically mount the NFS share on boot is preferred (or required). To enable this behavior, the NFS share needs to be configured rather than mounted directly.
/etc/fstab file provides information to the operating system regarding what devices need to be mounted where.
fstab stands for, "Filesystem Table." Each line of the file represents one mountpoint. To configure a system to mount an NFS share on boot, a line needs to be added to this file similar to the following:
192.168.1.20:/srv/fs /srv/nfs nfs ro,async 0 0
The above line configures the client system to connect to the
/srv/fs share from the example in the configuring section above. To mount the
/srv/logs share from the same section:
10.0.0.30:/srv/logs /srv/logs nfs rw,sync 0 0
After a line is entered into the
/etc/fstab file, it can be mounted either this way:
or this way:
If there is an error, check to ensure the directory for the mount on the client machine exists. For a share which is mounted
touch command can be used to create an empty file in the NFS share from the client machine. For example:
Mounting an NFS Filesystem Temporarily
The method of mounting an NFS share which was shown in the Testing section, above, is the method used to temporarily mount a filesystem. Temporarily, here, means that it will stay mounted until either the
umount command is called on the mountpoint, or the system is rebooted.
In this example, the mountpoint is
The NFS filesystem is useful for making files available to remote machines, whether the client machines only need to be able to read them or write to them. A use of NFS shares which is very helpful for embedded systems is to provide a network root filesystem. See Booting with an NFS Root Filesystem for more information.