How to share directory over local network using NFS

Export directory over local network using NFS to easily access data on central server.

I will export /srv/exports/www to two different servers (read write and read only mode) using www-data user/group (uid 33, gid 33) as it will contain only HTML files.

Server

Install NFS kernel server and support programs.

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

Create directory that will be shared to the remote server.

$ sudo mkdir -p /srv/exports/www

Change owner and group.

$ sudo chown www-data:www-data /srv/exports/www

Specify exported directory and define user/group ID.

$ cat << EOF | sudo tee -a /etc/exports
/srv/exports/www 192.168.1.108(rw,sync,anonuid=33,anongid=33,no_subtree_check) 192.168.1.130(ro,no_subtree_check)
EOF

Export defined directories.

$ sudo exportfs -a -v
exporting 192.168.1.108:/srv/exports/www
exporting 192.168.1.130:/srv/exports/www

Display current export list including export options to verify configuration.

$ sudo exportfs -v
/srv/exports/www
		192.168.1.108(rw,wdelay,root_squash,no_subtree_check,anonuid=33,anongid=33,sec=sys,rw,secure,root_squash,no_all_squash)
/srv/exports/www
		192.168.1.130(ro,wdelay,root_squash,no_subtree_check,sec=sys,ro,secure,root_squash,no_all_squash)

Client

Install NFS support programs.

$ sudo apt install nfs-common

List the NFS server's export list.

$ sudo showmount -e 192.168.1.131
Export list for 192.168.1.131:
/srv/exports/www 192.168.1.130,192.168.1.108

Create directory.

$ sudo mkdir /var/www
$ ls -l /var/
total 40
drwxr-xr-x  2 root root  4096 Oct 22 15:05 backups
drwxr-xr-x  7 root root  4096 Jul 24  2017 cache
drwxr-xr-x 25 root root  4096 Jan 23 14:38 lib
drwxrwsr-x  2 root staff 4096 Jul 13  2017 local
lrwxrwxrwx  1 root root     9 Jul 24  2017 lock -> /run/lock
drwxr-xr-x  4 root root  4096 Jan 23 14:33 log
drwxrwsr-x  2 root mail  4096 Jul 24  2017 mail
drwxr-xr-x  2 root root  4096 Jul 24  2017 opt
lrwxrwxrwx  1 root root     4 Jul 24  2017 run -> /run
drwxr-xr-x  4 root root  4096 Jul 24  2017 spool
drwxrwxrwt  3 root root  4096 Jan 23 14:35 tmp
drwxr-xr-x  2 root root  4096 Jan 23 15:03 www

Mount NFS export.

192.168.1.108$ sudo mount -t nfs -o rw 192.168.1.131:/srv/exports/www /var/www

Notice that directory permissions are modified.

$ ls -l /var/
total 40
drwxr-xr-x  2 root     root     4096 Oct 22 15:05 backups
drwxr-xr-x  7 root     root     4096 Jul 24  2017 cache
drwxr-xr-x 25 root     root     4096 Jan 23 14:38 lib
drwxrwsr-x  2 root     staff    4096 Jul 13  2017 local
lrwxrwxrwx  1 root     root        9 Jul 24  2017 lock -> /run/lock
drwxr-xr-x  4 root     root     4096 Jan 23 14:33 log
drwxrwsr-x  2 root     mail     4096 Jul 24  2017 mail
drwxr-xr-x  2 root     root     4096 Jul 24  2017 opt
lrwxrwxrwx  1 root     root        4 Jul 24  2017 run -> /run
drwxr-xr-x  4 root     root     4096 Jul 24  2017 spool
drwxrwxrwt  3 root     root     4096 Jan 23 14:35 tmp
drwxr-xr-x  3 www-data www-data 4096 Jan 23 15:04 www

Display mounted NFS exports.

$ mount -t nfs4
192.168.1.131:/srv/exports/www on /var/www type nfs4 (rw,relatime,vers=4.2,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.108,local_lock=none,addr=192.168.1.131)

Ensure that it will mount at boot.

$ cat << EOF | sudo tee -a /etc/fstab
192.168.1.131:/srv/exports/www /var/www nfs rw 0 0
EOF

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system. He is also open for new opportunities and challenges.