NFS Server on Ubuntu 18.04

First, as root, install nfs-kernel-server:

[email protected]:~# apt install nfs-kernel-server

By default NFS version 2 is disabled. This is probably fine but if you do need to enable it, edit /etc/default/nfs-kernel-server and add:

RPCNFSDOPTS="--nfs-version 2,3,4 --debug --syslog"

And then restart the service:

[email protected]:~# service nfs-kernel-server restart

Confirm with:

[email protected]:~# cat /proc/fs/nfsd/versions
+2 +3 +4 +4.1 +4.2

Here we are going to create export directories in a global NFS root directory /srv/nfs4 and bind mount them to the actual directory:

[email protected]:~# mkdir -vp /srv/nfs4/{nfs-vcas-shared-storage,nfs-iso}
mkdir: created directory '/srv/nfs4'
mkdir: created directory '/srv/nfs4/nfs-vcas-shared-storage'
mkdir: created directory '/srv/nfs4/nfs-iso'

Bind mount the data directory to the global NFS root:

[email protected]:~# mount --bind /mnt/shared-storage/ /srv/nfs4/nfs-vcas-shared-storage/
[email protected]:~# mount --bind /mnt/nfs-iso/ /srv/nfs4/nfs-iso/

To make these bind mounts persist over a reboot, edit the /etc/fstab file:

[email protected]:~# vim /etc/fstab

And add an entry like the following:

/mnt/shared-storage /srv/nfs4/nfs-vcas-shared-storage	none	bind	0 0
/mnt/nfs-iso/ /srv/nfs4/nfs-iso/			none	bind	0 0

To check this worked, first un-mount if required:

[email protected]:~# umount -v /srv/nfs4/nfs-vcas-shared-storage /srv/nfs4/nfs-iso 
umount: /srv/nfs4/nfs-vcas-shared-storage unmounted
umount: /srv/nfs4/nfs-iso unmounted

Running the below command will read /etc/fstab and try to mount all entries within it:

[email protected]:~# mount -a

If that worked the NFS shares should now be mounted again:

[email protected]:~# lsblk -f /dev/vd{b,c}
NAME   FSTYPE LABEL          UUID                                 MOUNTPOINT
vdb                                                               
└─vdb1 ext4   shared-storage 2e7a0866-1164-44a8-9b6e-e2c4315e4e96 /srv/nfs4/nfs-vcas-shared-storage
vdc                                                               
└─vdc1 ext4   nfs-iso        8f0992ec-f3e8-4fa2-8ea9-a094a7dba1dd /srv/nfs4/nfs-iso

Before exporting the filesystem, update the owner:

[email protected]:~# chown -v nobody:nogroup /srv/nfs4/nfs-vcas-shared-storage/
[email protected]:~# chown -v nobody:nogroup /srv/nfs4/nfs-iso/

And permissions:

[email protected]:~# chmod 777 /srv/nfs4/nfs-vcas-shared-storage/
r[email protected]:~# chmod 777 /srv/nfs4/nfs-iso/

Now export the filesystem.

[email protected]:~# vim /etc/exports

My exports file looks like the following:

# /etc/exports: the access control list for filesystems which may be exported
#		to NFS clients.  See exports(5).
/srv/nfs4  192.168.0.0/16(rw,sync,no_subtree_check,crossmnt,fsid=0) 
/srv/nfs4/nfs-vcas-shared-storage  192.168.11.0/24(rw,sync,no_subtree_check) 
/srv/nfs4/nfs-iso  192.168.11.0/24(rw,sync,no_subtree_check)

Export the filesystem with exportfs and restart the nfs-kernel-server.service.

[email protected]:~# exportfs -ra
[email protected]:~#

To view the current active exports including all the default options:

[email protected]:~# exportfs -v
/srv/nfs4     	192.168.0.0/16(rw,wdelay,crossmnt,root_squash,no_subtree_check,fsid=0,sec=sys,rw,secure,root_squash,no_all_squash)
/srv/nfs4/nfs-vcas-shared-storage
		192.168.11.0/24(rw,wdelay,root_squash,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
/srv/nfs4/nfs-iso
		192.168.11.0/24(rw,wdelay,root_squash,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)

Client Test

Lastly we need to test that the client can see, connect and make changes to the NFS shares. You can view the available shares with:

[[email protected] ~]$ showmount -e nfs2.pikedom
Export list for nfs2.pikedom.com:
/srv/nfs4/nfs-iso                 192.168.11.0/24
/srv/nfs4/nfs-vcas-shared-storage 192.168.11.0/24
/srv/nfs4                         192.168.0.0/16

Make the directory where you want to mount the share, /nfs/isos in my case:

[[email protected] ~]$ ls -la /nfs/isos/
total 8
drwxr-xr-x 2 root root 4096 Feb 10 10:59 .
drwxr-xr-x 5 root root 4096 Feb 10 10:59 ..

Mount the share with:

[[email protected] ~]$ sudo mount nfs2.pikedom.com:/srv/nfs4/nfs-iso /nfs/isos/
[sudo] password for andy: 
[[email protected] ~]$ ls -la /nfs/isos/
total 24
drwxrwxrwx 3 nobody nobody  4096 Feb  5 17:42 .
drwxr-xr-x 5 root   root    4096 Feb 10 10:59 ..
drwx------ 2 nobody nobody 16384 Feb  5 17:42 lost+found

Check you can write a file to the share:

[[email protected] ~]$ touch /nfs/isos/test.txt
[[email protected] ~]$ ls -la /nfs/isos/test.txt
-rw-r--r-- 1 andy andy 0 Feb 10  2020 /nfs/isos/test.txt

Check you can open, edit and save the file:

[[email protected] ~]$ vim /nfs/isos/test.txt
[[email protected] ~]$ cat /nfs/isos/test.txt
Test edit.

Lastly make sure you can remove the file:

[[email protected] ~]$ rm -v /nfs/isos/test.txt
removed '/nfs/isos/test.txt'

Mount on Boot

You may want to make sure the share is mounted on each boot-up. To achieve this, edit /etc/fstab:

[[email protected] ~]$ sudo vim /etc/fstab

And enter something similar to the below at the bottom of the file:

nfs2.pikedom.com:/srv/nfs4/nfs-vcas-shared-storage  /nfs/shared-storage nfs defaults,user,exec  0 0
nfs2.pikedom.com:/srv/nfs4/nfs-iso                  /nfs/isos   nfs defaults,user,exec  0 0

Lastly make sure the entry worked by running the below command which will try to mount everything listed in /etc/fstab:

[[email protected] ~]$ sudo mount -a

You can also check its mounted by using the df command:

[[email protected] ~]$ sudo df -h
Filesystem                                          Size  Used Avail Use% Mounted on
dev                                                  16G     0   16G   0% /dev
run                                                  16G  1.8M   16G   1% /run
/dev/dm-0                                           423G  120G  282G  30% /
...
nfs2.pikedom.com:/srv/nfs4/nfs-vcas-shared-storage  295G   64M  280G   1% /nfs/shared-storage
nfs2.pikedom.com:/srv/nfs4/nfs-iso                  251G   60M  239G   1% /nfs/isos

Be the first to comment

Leave a Reply