Saturday, March 31, 2018

NFS setup for Rpi

This will show you how to setup an NFS server and client for the Raspberry Pi. We need one Pi to act as the NFS server and one or more other Pis will be the clients. I have called my fictitious NFS server “pistore” and it is using an IP address of 192.168.0.99.


This tutorial was done using the current Raspbian release at the time of writing - Stretch 9.4.


All of this is done using the command line or terminal session so you can do this even on a “headless” Pi - that is one that doesn’t have a screen or keyboard.


We are using nano to edit some files. To save your changes in nano press Control-O and it will prompt for the filename with the one you used to open it as a default. Just press the Enter key. To exit nano press Control-X.



Configure NFS Server


1.      Install NFS server
At a command prompt type:
sudo apt update
sudo apt install nfs-kernel-server -y
 
2.      Create the NFS server shared folder
Create a mount-point. You can use an existing directory if you want.
sudo mkdir /mnt/nfsserver


Give it liberal permissions.
sudo chmod -R 777 /mnt/nfsserver
 
3.      Edit the NFS exports file
This is where you configure the paths to share and their permissions.
sudo nano /etc/exports


The syntax is: Share directory + space + IP address + Access
Where
/mnt/nfsserver represents the path to share on the Raspberry Pi. The share directory.


IP represents the IP address or range


Access restricts certain types of access like read and write (rw) or read only (ro). It is contained within brackets
/mnt/nfsserver IP(access)


Using a asterisk you can grant access to all IP addresses which is not recommended.
/mnt/nfsserver *(rw,sync,no_subtree_check)


You can also specify a single Pi by its IP address
/mnt/nfsserver 192.168.0.34(rw,sync,no_subtree_check)


You can choose to restrict access to a whole IP range like your entire home network. The /24 represents the subnet mask of your network.
/mnt/nfsserver 192.168.0.0/24(rw,sync,no_subtree_check)


If you want read-only access for your NFS clients change the rw to ro
/mnt/nfsserver 192.168.0.0/24(ro,sync,no_subtree_check).


Its recommended to use (rw,sync,no_subtree_check) for access, unless you want to grant read-only access. exportfs will give warning messages if subtree_check is not specified.


 
4.      Tell NFS to read the exports file
sudo exportfs -a
 
5.      Check it started
To check the status of the service you can do the following:
sudo service nfs-server status


It should look something like this
Mar 30 20:39:02 pistore systemd[1]: Starting NFS server and services...
Mar 30 20:39:02 pistore systemd[1]: Started NFS server and services.


If it has errors they are likely to be exportfs lines that it doesn't like. There should only be a single space between the path /mnt/nfsserver and the IP address. The options contained within the brackets should be after the IP address (ie there is no space between the IP address and the bracket).


After you've fixed up the exports file you can restart it by:
sudo service nfs-server start
 
6.      Check NFS is exported
To check you are exporting the share use the following command.
sudo exportfs


It should respond with something like this:
/mnt/nfsserver  192.168.0.0/24
 
7.      Firewall
If you are using a firewall you will need to open port 2049 (tcp and udp) so the clients can access your NFS server.


The NFS server should have a static IP address otherwise it may end up with a different IP address every time it starts up and the clients won’t be able to find it.


 
Configure NFS Client
This is done on each of client Pis.

1.      Add the name of the NFS server

Because I don't remember IP addresses all that well I am going to add pistore, the name of my fictitious NFS server, to /etc/hosts, that way I can just refer to it by its name.
sudo nano /etc/hosts


Add the IP address and name to the end of the file
192.168.0.99  pistore 


2.      Create the mount point
Create a mount-point for our NFS share, this is the local directory you use to access the directory on the NFS server
sudo mkdir -p /mnt/nfs

Change owner of the NFS mount point to user pi
sudo chown -R pi:pi /mnt/nfs

3.      Test it
Test mounting the NFS share on your Raspberry Pi. Replace the first section with the IP address of your NFS server or use its name and its share path.
sudo mount pistore:/mnt/nfsserver /mnt/nfs

4.      Automount the share
To make the NFS share mount automatically on boot-up edit the fstab file.
sudo nano /etc/fstab


Add this line but adjust your IP/NFS server name and mount points. rw stands for read/write so if you only want read access change the rw to ro (read only)
pistore:/mnt/nfsserver   /mnt/nfs   nfs    rw  0  0



Now reboot the Pi and see if you can access /mnt/nfs



2 comments:

Michel Emming said...

Are you booting all your pis from this nfs? Just thinking theoretically about the problems of wearing down the sd cards to fast with BOINC running on the pis. Does this help, work and scale up well with more Pis or is there a limitation?

Mark G James said...

The Pis are booting off the SD card. I found the Pi won’t allow more than 16 NFS connections at a time. I haven’t been able to get past this limit.

Wear is a problem. I have set BOINC checkpoints to 5 mins which seems to help but they get slower and I end up replacing the SD cards somewhere between 6 and 12 months.