NFS Server on Ubuntu 18.04

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

root@nfs2:~# 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:

root@nfs2:~# service nfs-kernel-server restart

Confirm with:

root@nfs2:~# 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:

root@nfs2:~# 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:

root@nfs2:~# mount --bind /mnt/shared-storage/ /srv/nfs4/nfs-vcas-shared-storage/
root@nfs2:~# mount --bind /mnt/nfs-iso/ /srv/nfs4/nfs-iso/

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

root@nfs2:~# 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:

root@nfs2:~# 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:

root@nfs2:~# mount -a

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

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

Before exporting the filesystem, update the owner:

root@nfs2:~# chown -v nobody:nogroup /srv/nfs4/nfs-vcas-shared-storage/
root@nfs2:~# chown -v nobody:nogroup /srv/nfs4/nfs-iso/

And permissions:

root@nfs2:~# chmod 777 /srv/nfs4/nfs-vcas-shared-storage/
root@nfs2:~# chmod 777 /srv/nfs4/nfs-iso/

Now export the filesystem.

root@nfs2:~# 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).

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

root@nfs2:~# exportfs -ra

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

root@nfs2:~# exportfs -v

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:

[andy@home-pc ~]$ showmount -e nfs2.pikedom
Export list for

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

[andy@home-pc ~]$ 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:

[andy@home-pc ~]$ sudo mount /nfs/isos/
[sudo] password for andy: 
[andy@workpc ~]$ 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:

[andy@home-pc ~]$ touch /nfs/isos/test.txt
[andy@home-pc ~]$ 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:

[andy@home-pc ~]$ vim /nfs/isos/test.txt
[andy@home-pc ~]$ cat /nfs/isos/test.txt
Test edit.

Lastly make sure you can remove the file:

[andy@home-pc ~]$ 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:

[andy@home-pc ~]$ sudo vim /etc/fstab

And enter something similar to the below at the bottom of the file:  /nfs/shared-storage nfs defaults,user,exec  0 0                  /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:

[andy@home-pc ~]$ sudo mount -a

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

[andy@home-pc ~]$ 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% /
...  295G   64M  280G   1% /nfs/shared-storage                  251G   60M  239G   1% /nfs/isos

Be the first to comment

Leave a Reply