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 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.
Contents
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:
user@ldc:~$ sudo apt-get install nfs-kernel-server nfs-common portmap
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 asro
- If the filesystem will be writeable, is performance or data integrity more important? If performance,
async
will provide more performance at the risk of lost or corrupt data should the NFS server shut down unexpectedly during a write operation. Otherwise,sync
should be used. - Is reliability or security more important? If reliability is, use
no_subtree_check
. Otherwise, usesubtree_check
. - Is user level access control required? If not, getting the NFS server working is much simpler with the
no_root_squash
option.
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 address(options)
Here:
- 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:
/srv/fs 192.168.1.0/255.255.255.0(ro,async,no_subtree_check,no_root_squash)
To export the /srv/logs/
directory as a writeable NFS share with data integrity and reliability options on the 10.0.0.0/8
network:
/srv/logs 10.0.0.0/255.0.0.0(rw,sync,no_root_squash)
To export the /srv/homes
directory as a writeable NFS share with data integrity, reliability and user level access control on the 172.16.0.0/16
network:
/srv/homes 172.16.0.0/255.255.0.0(rw,sync)
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:
user@ldc:~$ exportfs -a
Testing
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:
user@ldc:~$ sudo apt-get install nfs-common portmap
Now, to mount the /srv/fs
share exported above with a server address of 192.168.1.20
on the local directory, /srv/nfs:
user@ldc:~$ sudo mount 192.168.1.20:/srv/fs /srv/nfs
To verify that it mounted:
user@ldc:~$ mount ... 192.168.1.20:/srv/fs on /srv/nfs type nfs (ro,vers=3,addr=192.168.1.20,clientaddr=192.168.1.142)
In the output is the line which shows the mount. The files in the directory can be listed:
user@ldc:~$ ls /srv/nfs filea fileb filec filed
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.
The /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:
user@ldc:~$ sudo mount -a
or this way:
user@ldc:~$ sudo mount /srv/nfs
If there is an error, check to ensure the directory for the mount on the client machine exists. For a share which is mounted rw
, the touch
command can be used to create an empty file in the NFS share from the client machine. For example:
user@ldc:~$ touch /srv/logs/test_file user@ldc:~$ ls /srv/logs test_file
Summary
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.