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.
1. Install NFS server
At a command prompt type:
sudo apt update
sudo apt install nfs-kernel-server -y
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/nfsserverThis 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.
sudo exportfs -a
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 startTo 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/24If 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.
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 serversudo mkdir -p /mnt/nfs
sudo chown -R pi:pi /mnt/nfs
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
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