How to Setup NFS Server in Ubuntu

Written by: Bagus Facsi Aginsa
Published at: 18 Sep 2021


NFS (Network File Share) is a protocol that allow you to share directory with other machine via network. A machine that share the directory to other machine called NFS Server, a machine that use the remote directory is called NFS client. In this tutorial, we will show you how to install an NFS server.

Prerequisite

  1. Ubuntu

Sudo Privileges

Before start, we make sure that we will have no permission issue on the configuration.

sudo su

Install Dependencies

First we need to install dependecy library

apt update && apt install nfs-kernel-server

Create a Directory to Share

This directory will be shared among the NFS client. In linux this is called the export directory. You can create a directory anywhere, for example:

mkdir -p /nfs/share

Remove any restriction in this directory

chown -R nobody:nogroup /nfs/share
chmod -R 777 /nfs/share

Configure NFS Server

A configuration is needed in the NFS server, so the client can detect the directory path that is shareable. To do that, open /etc/exports file

nano /etc/exports

Add this line of code

/nfs/share <expected client ip/subnet>(rw,sync,no_subtree_check)

Change <expected client ip/subnet> to match your expected NFS client IP / Subnet. For example, if we expect an NFS client in subnet 192.168.5.0/24 in the future, then we can write

/nfs/share 192.168.5.0/24(rw,sync,no_subtree_check)

Or if we want to accept connection from anywhere, we can just put a wildcard (*). Note that this is not good practice from security perpective.

/nfs/share *(rw,sync,no_subtree_check)

After configure what directory to be shared, we just need to run this command

exportfs -a

And restart NFS server

systemctl restart nfs-kernel-server

Thats it! Congratulation, now you know how to setup nfs server in ubuntu. If you want to mount your NFS client to the server you just created, please check my other tutorial here: How to Mount Directory to NFS Server