Installation

(root@server)_$: apt-get install nfs-common nfs-kernel-server
(user@client)_$: apt-get install nfs-common rpcbind

Upon starting the server, it will complain about not finding the /run/rpcbind/rpcbind.xdr and /run/rpcbind/portmap.xdr files. It is a bug in Ubuntu that does not affect to the server’s performance.

Server configuration

We will assume the server IP address is 192.168.1.191 for the rest of the examples.

(root@server)_$: mkdir -p /srv/share
(root@server)_$: chmod 0777 /srv/share
/etc/hosts.allow:
-----------------
...
portmap: 192.168.1.*
(root@server)_$: service portmap restart
/etc/exports:
-------------
...
# /srv/share
/srv       192.168.0.0/16(rw,root_squash,subtree_check)
/srv/share 192.168.0.0/16(rw,root_squash,subtree_check)

Check the /etc/idmapd.conf file. If everything is ok you will not have to change anything, but check it just in case.

/etc/idmapd.conf:
-----------------
[General]

Verbosity = 0
Pipefs-Directory = /run/rpc_pipefs
# set your own domain here, if id differs from FQDN minus hostname
# Domain = localdomain

[Mapping]

Nobody-User = nobody
Nobody-Group = nogroup
(root@server)_$: service nfs-kernel-server restart

Client configuration

Check the /etc/idmapd.conf file in the client too.

/etc/idmapd.conf:
-----------------
[General]

Verbosity = 0
Pipefs-Directory = /run/rpc_pipefs
# set your own domain here, if id differs from FQDN minus hostname
# Domain = localdomain

[Mapping]

Nobody-User = nobody
Nobody-Group = nogroup

Ubuntu 14 - Bug 1270445

To solve this bug you need to change the following:

/etc/default/nfs-common:
------------------------
...
NEED_GSSD="yes"
_$: service rpcbind restart
_$: rpc.gssd

If that does not work, reboot the server.

Test

Check the NFS server is running

(root@server)_$: rpcinfo -p | grep nfs
    100003    2   tcp   2049  nfs
    100003    3   tcp   2049  nfs
    100003    4   tcp   2049  nfs
    100003    2   udp   2049  nfs
    100003    3   udp   2049  nfs
    100003    4   udp   2049  nfs

Mount the directory and create a file on the client

(user@client)_$: sudo mkdir -p /tmp/share
(user@client)_$: sudo mount -t nfs -o rw,nosuid 192.168.1.191:/srv/share /tmp/share
(user@client)_$: cd /tmp/share/
(user@client)_$: touch a
(user@client)_$: echo "asd" > a

See if the changes went to the server

(root@server)_$: ll /srv/share/
total 4
-rw-r--r-- 1 wintenfox wintenfox 4 Dec 28 09:43 a
(root@server)_$: more a
asd

And unmount the directory in the client

(user@client)_$: cd /tmp/
(user@client)_$: sudo umount shared

Create a script to mount the directory

All of these changes are done in the client host.

/etc/init.d/mount-nfs-files:
----------------------------
#! /bin/sh
#
# skeleton      example file to build /etc/init.d/ scripts.
#               This file should be used to construct scripts for /etc/init.d.
#
#               Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#               Modified for Debian
#               by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#               Further changes by Javier Fernandez-Sanguino <jfs@debian.org>
#
# Version:      @(#)skeleton  1.9  26-Feb-2001  miquels@cistron.nl
#
### BEGIN INIT INFO
# Provides:          files shared directory
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Mount/umount files shared folder.
# Description:       Mount/umount files shared folder.
### END INIT INFO

NAME="mount-nfs-files"
REMOTE="192.168.1.191"
REMOTE_DIR="/srv/share/files"
LOCAL_DIR="/home/<user>/shared/files"

case "$1" in
    start)
        mount -t nfs -o rw,nosuid ${REMOTE}:${REMOTE_DIR} ${LOCAL_DIR}
        ;;
    stop)
        umount ${LOCAL_DIR}
        ;;
    restart)
        umount ${LOCAL_DIR}
        mount -t nfs -o rw,nosuid ${REMOTE}:${REMOTE_DIR} ${LOCAL_DIR}
        ;;
    *)
        echo "Usage: $NAME {start|stop|restart}"
        exit 1
        ;;
esac
exit 0

Create the init scripts:

_$: chown root:root /etc/init.d/mount-nfs-files
_$: chmod 0755      /etc/init.d/mount-nfs-files
_$: update-rc.d mount-nfs-files defaults 99 00
_$: service mount-nfs-files start

If you ever decide to delete the script:

_$: update-rc.d -f mount-nfs-files remove
_$: rm /etc/init.d/mount-nfs-files

Troubleshooting

1) The command sudo mount -t nfs ... does not finish immediately in the client.

Check your iptables configuration and add the 2049 TCP and UDP port.

...
-A INPUT -p udp -m state --state NEW -m udp --dport 2049 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 2049 -j ACCEPT
...

2) all_squash does not work.

If the directory we want to share is inside the sharing directory, we need to bind it.

Example: We want to share /srv/share/test The sharing directory is /srv/share

(root@server)_$: mv /srv/share/test /srv/share/.test
(root@server)_$: mkdir /srv/share/test
(root@server)_$: mount --bind .test/ test
/etc/exports:
-------------
/srv             192.168.0.0/16(rw,no_root_squash,subtree_check)
/srv/share       192.168.0.0/16(rw,no_root_squash,subtree_check)
/srv/share/test  192.168.0.0/16(rw,all_squash,anonuid=65534,anongid=65534,subtree_check)

You can add this to fstab so that it is not affected by reboots:

/etc/fstab:
-----------
...
/srv/share/.test /srv/share/test none rw,bind 0 0

3) We can not unmount the directory because the NFS server is irresponsive (shutdown, no network connection, etc.).

(user@client)_$: umount -f /tmp/share

4) Directory appears as busy when unmounting.

(user@client)_$: umount -f -l /tmp/share

5) The changes that we made in .test are not reflected in test.

This can happen if we move the directory to somewhere else or we restore a backup overwriting the old directory. Creating and deleting files in those directories should not need these commands to see changes reflected.

(root@server)_$: mount --bind .test/ test/
(root@server)_$: mount -o remount,ro,bind .test/ test/

Configuration example

/etc/exports:
-------------
# /etc/exports: the access control list for filesystems which may be exported
#               to NFS clients.  See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes       hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4        gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes  gss/krb5i(rw,sync,no_subtree_check)
#

/srv             192.168.0.0/16(rw,no_root_squash,subtree_check)
/srv/share       192.168.0.0/16(rw,no_root_squash,subtree_check)
/srv/share/files 192.168.0.0/16(rw,all_squash,subtree_check)
/etc/fstab:
-----------
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
...
/srv/share/.files /srv/share/files none rw,bind 0 0