How to Configure NFS Server on Ubuntu 18.04 Server

1
4221

Last updated on January 13th, 2019 at 02:57 pm

In this tutorial, we will discuss how to configure NFS server on Ubuntu 18.04 server.

NFS operates on a server-client architecture and therefore to demonstrate how to configure NFS server on Ubuntu 18.04 server to share directories and files over the network and Ubuntu 18.04 desktop as our NFS client, follow through this guide.

Network File system (NFS) is a commonly used file-based storage system that allows remote systems to access files over a computer network and interact with them as if they were locally mounted. This enables system Administrators to group resources onto centralized servers on a network for easy sharing.

The details of our nfs server and client are:

NFS server: nfs01.example.com, 192.168.43.154
NFS client
: usera.example.com, 192.168.43.214

Configure NFS Server on Ubuntu 18.04 Server

Install NFS server packages on the NFS server host

apt-get install nfs-kernel-server -y

Edit the /etc/idmapd.conf file and uncomment line 6 and set it to the correct domain name.

vim /etc/idmapd.conf
...
Domain = example.com
...

Configure the NFS Exports

Exports are file systems or directories on an NFS server that are shared to or accessible to NFS clients. So we are going to create a general directory and a private directory where the files we would like to share publicly or privately respectively over the network will be stored.

mkdir /opt/{general,private}

Edit the /etc/exports file and set up the above directories for sharing. To set up a directory for sharing, specify the directory to be shared, IP addresses or domain names (if you have DNS server) of the systems to share with along the options associated with shared directory in the format:

nfshare   nfsclient_IP or nfsclients_net or_domainname(sharingoptions)

vim /etc/exports

In our case, we will share the general directory with anyone and the private directory with specific clients.

...
# NFS share directory
/opt/general  *(ro,sync,root_squash,subtree_check)
/opt/private 192.168.43.214(rw,sync,no_root_squash,no_subtree_check)

The mount options used above are;
ro mounts the directory on the client with read only permissions.
rw mounts the shared directory on the client with read write permissions
sync ensures that any changes made to the shared directory is synchronized between the server and the client.
root_squash maps the remote root user privileges into a non-privileged user on the NFS server
no_root_squash allows remote user to access the share with full privileges of the root user on the NFS server.
subtree_check ensures that in case a directory instead of a block device is exported, the nfs server must check the existence of files in the shared directory for every request made.
no_subtree_check specifies that the nfs server should not verify the availability of the files in the export for every request.

For a deeper insight into the export mount options, check man 5 exports

Once done with editing, save the file and run the following command to export the shared directories.

exportfs -arvf
exporting 192.168.43.214:/opt/private
exporting *:/opt/general

See the man page for exportfs to more information on options used above, man exportfs

If all is well, restart NFS server so as to make the share available to NFS clients.

systemctl restart nfs-kernel-server

If firewall is running on NFS server, allow access to the nfs share from the client.

ufw allow from 192.168.43.214 to any port nfs

Reload the firewall to apply the changes:

ufw reload

Configure NFS Client

Install NFS client packages

apt-get install nfs-common -y

Edit the /etc/idmapd.conf file and uncomment line 6 and set it to the correct domain name.

vim /etc/idmapd.conf
...
Domain = example.com
...

Create a directory to mount the remote share

To access the remote shared directories on the client, we need to mount those directories on the NFS client.

mkdir -p /nfs-shares/{general,private}

Run the following command to verify the export list on NFS server.

showmount -e 192.168.43.154
Export list for 192.168.43.154:
/opt/general *
/opt/private 192.168.43.214

Mount the exports on NFS client as shown below

mount -t nfs 192.168.43.154:/opt/general /nfs-shares/general
mount -t nfs 192.168.43.154:/opt/private /nfs-shares/private/

Verify the mounting with the following command;

df -hT
Filesystem                                Type           Size    Used Avail Use%  Mounted on
udev                                      devtmpfs       697M       0  697M   0%  /dev
tmpfs                                     tmpfs          146M    1.6M  144M   2%  /run
/dev/mapper/ubuntu--vg-root ext4                         8.9G    4.1G  4.3G  50%  /
[...]
192.168.43.154:/opt/general               nfs4           8.9G    1.7G  6.7G  21%  /nfs-shares/general
192.168.43.154:/opt/private               nfs4           8.9G    1.7G  6.7G  21%  /nfs-shares/private

As you can see, both of the shares have been mounted.

To finalize on this, let us create some files on the NFS server and verify that the same becomes available to the client.

touch /opt/general/public-files.txt
touch /opt/private/private-files.txt

On the client:

ls /nfs-shares/general/
public-files.txt
ls /nfs-shares/private/
private-files.txt

Up to there,  you have learnt how to configure NFS Server on Ubuntu 18.04 Server. You can now be able to access the exports from Ubuntu 18.04 desktop. In our next article, we will discuss how to automount an NFS export.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here