The Network File System is certainly one of the most widely used network services. Network file system (NFS) is based on the Remote procedure call which allows the client to automatically mount remote file systems and therefore transparently provide an access to it as if the file system is local.
In this scenario we are going to export the file system from the an IP address 10.1.1.50 ( NFS server ) host and mount it on an a host with an IP address 10.1.1.55 ( NFS Client ). Both NFS server and NFS client will be running Ubuntu Linux.
At this point, we assume that the NFS service daemon is already installed on your system, including portmap daemon on which NFS setup depends.
If you have not done so yet simply install nfs-common package on both NFS client and NFS server using using apt-get tool.
The command above will fetch and install all support files common to NFS client and NFS server including portmap.
Additionally we need to install extra package on our NFS server side.
This package is the actual NFS daemon listenning on both UDP and TCP 2049 ports.
Execute rpcinfo -p to check correctness of your NFS installation and to actually confirm that NFS server is indeed running and accepting calls on a port 2049:
Furthermore, before we start exporting and mounting NFS directories, your system needs to actually support network file system. To check whether your system supports NFS grep /proc/filesystems and search for nfs.
If you do not see any output it means that NFS is not supported or the NFS module have not been loaded into your kernel. To load NFS module execute:
When installed correctly, the NFS daemon should be now listening on both UDP and TCP 2049 port and portmap should be waiting for instructions on a port 111.
At this point you should have portmap listening on both NFS server and NFS client:
4. Server export file
All directories we want to share over the network using NFS need to be defined on the server side of this communication and more specifically they need to be defind with /etc/exports file. In the next section you will see most common NFS exports:
4.1. Most common exports options
Here are the most common NFS export techniques and options:
export /home/nfs directory for host with an IP address 10.1.1.55 with read, write permissions, and synchronized mode
export /home/nfs directory for network 10.1.1.0 with netmask 255.255.255.0 with read only permissions and synchronized mode
/home/nfs/ 10.1.1.55(rw,sync) 10.1.1.10(ro,sync)
export /home/nfs directory for host with IP 10.1.1.55with read, write permissions, synchronized mode, and also export /home/nfs directory for another host with an IP address 10.1.1.10 with read only permissions and synchronized mode
export /home/nfs directory for host with an IP address 10.1.1.55with read, write permissions, synchronized mode and the remote root user will be treated as a root and will be able to change any file and directory.
export /home/nfs directory for any host with read only permissions and synchronized mode
export /home/nfs directory for any host within linuxtechtips.com domain with a read only permission and synchronized mode
export /home/nfs directory for hostname foobar with read, write permissions and synchronized mode
4.2. Edit exports file
Now that we have familiarized our selfs with some NFS's export options we can define our first NFS export. Open up your favorite text editor, for example, vim and edit /etc/exports file by adding a line /home/nfs/ *(ro,sync) which will export /home/nfs directory for any host with read only permissions. Instead of text editor you can simply insert your NFS export line into /etc/exports file using echo command:
Be sure that the directory you are about to export by NFS exists. You can also create a file inside the /home/nfs directory which will help you troubleshoot once you mount /home/nfs/ remotely.
NOTE: The default behavior of NFS kernel daemon is to include additional option to your export line which is "no_subtree_check". Be aware of this fact when you attempt to configure your NFS exports further.
4.3. Restart NFS daemon
Once you have edited /etc/exports file you need to restart your NFS daemon to apply any changes. Depending on your Linux distribution the restarting procedure of NFS may differ. Ubuntu and Debian users:
Redhat and Fedora users
If you later decide to add more NFS exports to the /etc/exports file, you will need to either restart NFS daemon or run command exportfs:
5. Mount remote file system on client
First we need to create a mount point:
If you are sure that the NFS client and mount point are ready, you can run the mount command to mount exported NFS remote file system:
In case that you need to specify a filesystem type you can do this by:
You may also get and an error message:
This may mean that your server supports higher NFS version and therefore you need to pass one extra argument to your nfs client mount command. In this example we use nfs version 3:
In any case now you should be able to access a remote /home/nfs directory locally on your NFS client.
The above output proves that a remote NFS export is mounted and that we can access it by navigating to a local /home/nfs_local/ directory. Please notice that the touch command reports that the filesystem is mounted as read-only which was exactly our intention.
6. Configure automount
To make this completely transparent to end users, you can automount the NFS file system every time a user boots a Linux system, or you can also use PAM modules to mount once a user logs in with a proper username and password. In this situation just edit /etc/fstab to mount system automatically during a system boot. You can use your favorite editor and create new line like this within /etc/fstab:
as before you also use echo command to do that:
The Network File System comes with tons of export options. What has been shown here, just barely scratches the surface of NFS. Please visit Linux NFS-HOWTO hosted by linux documentation project or NFS homepage for more details.
8. Appendix A
Following section of this NFS tutorial is going to be devoted to RedHat and Fedora Linux systems which by default block all incoming traffic to a NFS server by engaging firewall using iptables rules. For this reason when the firewall is running on your NFS server, you might get this error when mounting NFS filesytem:
mount.nfs: mount to NFS server '10.1.1.13' failed: System Error: No route to host.
This error message has nothing to do with your NFS configuration, all what needs to be done is either turn off the firewall or add iptables rules to allow traffic on portmap port 111, nfs port 2049 and random ports for other nfs services.
There are two solutions to this problem: easy solution is to turn off the firewall completely and the right solution to add appropriate iptables rules.
8.1. Turn off firewall on Redhat like systems:
The easiest solution is to just turn off the firewall. This will automatically grant access to the nfs daemon to anyone. I would suggest this solution only for testing purposes of your NFS configuration. Enter the following command to stop firewall and clean up all iptables rules:
Now when your NFS settings are correct you should be able to mount nfs filesystem from you client machine.
8.2. Add iptables rules to allow NFS communication
This is a more complex but right solution to the above problem. First we need to set static port for nfs services such as rquotad, mountd, statd, and lockd by editing /etc/sysconfig/nfs file. Add or uncomment following lines in your /etc/sysconfig/nfs file:
Restart you NFSD daemon with following commands:
Use rpcinfo command to confirm a validity of your new ports settings:
Save your current iptables rules into iptables-rules-orig.txt :
Create file called iptables-nfs-rules.txt with the following content:
Apply new rules with iptables-restore, where the single argument will be an iptables-nfs-rules.txt file:
NOTE: this will create a new set of iptables rules. If you have already defined some iptables rules previously, you may want to edit iptables-rules-orig.txt and use it with iptables-restore command instead.
Save these new rules, so you do not have to apply new rules for nfs daemon next time you restart your server:
Now your server is ready to accept client nfs requests. Optionally, you may restart iptables rules / firewall with the following command: