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

by mirza irwan osman in Circuits > Linux

56691 Views, 58 Favorites, 0 Comments

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

F45YZ59I6C2JFC0.png

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

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

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

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

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!

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