Turn Raspberry Pi Into a Network File System Version 4 (NFSv4) Server

56K5811

Intro: Turn Raspberry Pi Into a Network File System Version 4 (NFSv4) Server

Introduction

Network FIle System(NFS) can simultaneously run in version 2, 3, 4. NFS version 4(NFSv4) has several improvements over the NFSv2 and NFSv3. The improvements I like most are:

  • NFSv4 makes configuring firewall simple because NFSv4 uses only one port (default to 2049) while NFSv2 and NFSv3 use 4 randomly changing ports
  • NFSv4 provides strong security with the implementation of Kerberos while NFSv2 and NFSv3 don't

In my home network, all the computers are installed with Linux OS that supports NFSv4. Therefore, it is advantageous to configure NFS server to be ONLY operating in version 4 and disable NFSv2 and NFSv3.

After completing this instructable, you might be interested in learning how to use the automounter.

Scope

This instructable will show:

  • How to install NFSv4 server
  • How to enable configure NFSv4 server
  • How to disable NFSv2 and NFSv3
  • How to run NFSv4 server
  • How to test NFSv4 server

This instructable will NOT show:

My system specification

Linux rpipro 3.18.7+ #755 PREEMPT Thu Feb 12 17:14:31 GMT 2015 armv6l GNU/Linux

STEP 1: Install NFS

Open terminal emulator in Raspberry Pi

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade

Install NFS

sudo apt-get install nfs-common nfs-kernel-server

STEP 2: Share a Directory

Open terminal emulator in Raspberry Pi

Make a directory to be shared

sudo mkdir -p /srv/nfs4/share
cd /srv/nfs4/share

Create a stub file or put any file in

sudo vi hello.txt

Type the following or whatever:

Hello NFS

Save the file

Add the above directory to be shared and exported

sudo vi /etc/exports

Append the following (192.168.CCC.DDD is the ip address of the client that can read and write to the shared directory):

/srv/nfs4/share 192.168.CCC.DDD(rw,sync,no_subtree_check)

Save the file

STEP 3: Modify NFS Init Scripts

The /etc/init.d/nfs-kernel-server script starts the NFS daemon in either NFSv2, NFSv3 and NFSv4 simultaneouly or without NFSv4. The script cannot make NFS operate in NFSv4 only. I modified the script to make it start the daemon in either NFSv2, NFSv3 and NFSv4 simultaneouly or without NFSv4 or NFSv4 only.

Open terminal emulator in Raspberry Pi

cd /etc/init.d

Make backup of nfs-kernel-server script and config file

sudo cp /etc/init.d/nfs-kernel-server /etc/init.d/nfs-kernel-server.pristine
sudo cp /etc/default/nfs-kernel-server /etc/default/nfs-kernel-server.pristine

Download the nfs-kernel-server.script that I uploaded

Read the script

Replace current script with downloaded one

sudo cp path/to/download/nfs-kernel-server.script /etc/init.d/nfs-kernel-server

Download the nfs-kernel-server.cfg that I uploaded

Read the config file
Replace current script with downloaded one

sudo cp path/to/download/nfs-kernel-server.cfg /etc/default/nfs-kernel-server

Make backup of nfs-common parameter file.

cd /etc/default 
sudo cp nfs-common nfs-common.pristine 
sudo vi nfs-common 
Change:
NEED_STATD= 
to: 
NEED_STATD="no" 
Change: 
NEED_IDMAPD= 
to: 
NEED_IDMAPD="yes" 

Save the file

STEP 4: Run the NFSv4 Daemon

Open terminal emulator in Raspberry Pi

Start the NFS service

sudo service nfs-kernel-server start 
[ ok ] Exporting directories for NFS kernel daemon.... 
[....] Starting NFS kernel daemon: nfsdrpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused) 
rpc.nfsd: address family inet6 not supported by protocol TCP 
rpc.nfsd: unable to set any sockets for nfsd
 failed!

Explanation:
The nfsd fail to start. This failure may not happen for some Raspberry Pi. inet6 refers to implementation ipv6 protocols. This issue can be fixed by loading ipv6 module Specify that ipv6 module is to be loaded at boot time

sudo vi /etc/modules 

Append the following:

ipv6 

Reboot the Raspberry Pi

Check that ipv6 module is loaded

lsmod | grep ipv6 

ipv6 351566 8

Start the NFS service one more time
sudo service nfs-kernel-server start

[ ok ] Exporting directories for NFS kernel daemon....

[....] Starting NFS kernel daemon: nfsdrpc.nfsd: Checking netconfig for visible protocols.

rpc.nfsd: Enabling inet udp.

rpc.nfsd: Enabling inet tcp.

rpc.nfsd: Enabling inet6 udp.

rpc.nfsd: Enabling inet6 tcp.

rpc.nfsd: knfsd is currently down

rpc.nfsd: Writing version string to kernel: -2 -3 +4

rpc.nfsd: Creating inet TCP socket.

rpc.nfsd: Creating inet UDP socket.

rpc.nfsd: Creating inet6 TCP socket.

rpc.nfsd: Creating inet6 UDP socket.

[ ok td.

Explanation:

The message "Writing version string to kernel: -2 -3 +4" is important. The minus sign indicates that NFSv2 and NFSv3 support is dropped. And the message at the end was OK. The NFSv4-only daemon has started!

STEP 5: Test With NFSv2, NFSv3 and NFSv4 Clients

Open terminal emulator in a second Raspberry Pi or any Linux system

Install NFS client software

sudo apt-get update 
sudo apt-get install nfs-common

nfs-common package provides a program called /sbin/showmount. showmount show mount information for an NFS server by sending NFSv2 and NFSv3 request.

Use showmount to send NFSv3 request to the NFSv4 server deployed in the previous steps(#192.168.XXX.YYY is the IP address of the NFSv4 server)

showmount -e 192.166.XXX.YYY

clnt_create: RPC: Port mapper failure - Unable to receive: errno 111 (Connection refused)
Explanation: The error message indicates that the NFSv4 server did not respond to any request from NFSv2 and NFSv3 clients as expected.

Make NFSv4 client send mount request to NFSv4 (#192.168.XXX.YYY is the IP address of the NFSv4 server)

sudo mount.nfs4 192.168.XXX.YYY:/ /mnt 

View the content of the hello.txt file that was created in the previous step

cat /mnt/srv/nfs4/share/hello.txt
  Hello NFS

9 Comments

Worked like a charm. My One RPi is a 64bit Bullseye running ZFS and the other RPi is a 32bit Buster (PROD) running CUPs, PiHole, Transmission. Now the data downloaded from the client is downloaded directly on the ZFS filesystem using the NFS client, no changes to client Rpi. Instructions were straight forward, however browser refused to download the .cfg file, I got around that using wget on the Pi.

I make nfs server and client on two raspberry pi's 3 (raspbian jessie) when I made file (server side) on my shared place (/home/nfs) I can't see them on client side when I use x window file menager, but when I use terminal, go-> /home/nfs then "ls" I can see that the flie exist. From the other side, when I made file on client /home/nfs appear immediately on server /home/nfs. I'm using VNC to connect with both rpi's. Please help me figure out what can be wrong ? there is any chance that is issue with VNC ?

When trying to install nfs-kernel-server on Wheezy on the RPI2 I've got an error:

The following packages have unmet dependencies:

nfs-kernel-server : Depends: libtirpc1 but it is not going to be installed

Depends: nfs-common (= 1:1.2.6-4) but it is not going to be installed

E: Unable to correct problems, you have held broken packages.

Hi Dmitry

I have no problem such as yours on my RPI 2.

I did the following:

sudo apt-get update

sudo apt-get upgrade

sudo apt-get dist-upgrade

sudo apt-get install nfs-common nfs-kernel-server

Maybe you can try the above.

Best Regards.

I did all above but still have got:

The following packages have unmet dependencies:

libtirpc1 : Breaks: nfs-kernel-server (< 1:1.2.8-7) but 1:1.2.6-4 is to be installed

nfs-common : Breaks: nfs-kernel-server (< 1:1.2.8-6~) but 1:1.2.6-4 is to be installed

nfs-kernel-server : Depends: nfs-common (= 1:1.2.6-4) but 1:1.2.8-9 is to be installed

E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.

enter this:

sudo apt-get -f install

I don't get any of the text output after sudo service nfs-kernel-server start. The cursor blinks for half a second and then I get the prompt back. No [ ok ] etc. Have I done something wrong?

Good info. Using Raspian Jessie, December 29, 2015. Found the changes to /etc/default/nfs-kernel-server had to be manually entered to make the nfs server work on start up. Neither NEED_STATD nor NEED_IDMAPD were in the new configuration file. nfs-kernel-server would start from the command line but would not start on boot up with out those changes. Thanks.

This issue reported here as well:

https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=110091&p=822196