How To Set Up and Configure NFS on Ubuntu 16.04

UbuntuOperating SystemSoftware & Coding

In this article, we will learn how to install NFS on Ubuntu 16.04Network File System (NFS) protocol and a filesystem which allows you to access the shared folders from the remote system or server and also allows you to mount as a remote directory on the servers. This allows you to share the storage space between different clients in different locations. NFS has been always the easiest way to access the remote storages over a network.

To accomplish this demo, we need two systems which Ubuntu installed and user with sudo permissions with a private network.

Installing the Packages on Server

We will install the ‘nfs-kernel’, which will be allowed us to share the directories on the server to share the files and folders. Below is the command to install the nfs package.

$ sudo apt update
$ sudo apt install nfs-kernel-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
keyutils libnfsidmap2 libpython-stdlib libpython2.7-minimal
libpython2.7-stdlib libtirpc1 nfs-common python python-minimal python2.7
python2.7-minimal rpcbind
Suggested packages:
watchdog python-doc python-tk python2.7-doc binutils binfmt-support
The following NEW packages will be installed:
keyutils libnfsidmap2 libpython-stdlib libpython2.7-minimal
libpython2.7-stdlib libtirpc1 nfs-common nfs-kernel-server python
python-minimal python2.7 python2.7-minimal rpcbind
0 upgraded, 13 newly installed, 0 to remove and 98 not upgraded.
Need to get 4,375 kB of archives.
After this operation, 18.5 MB of additional disk s
Selecting previously unselected package libnfsidmap2:amd64.
(Reading database ... 56995 files and directories currently installed.)
..
…
…
currently installed.)
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu4) ...
Setting up libnfsidmap2:amd64 (0.25-5) ...
Setting up libpython2.7-stdlib:amd64 (2.7.12-1~16.04) ...
Setting up python2.7 (2.7.12-1~16.04) ...
Setting up libpython-stdlib:amd64 (2.7.11-1) ...
Setting up python (2.7.11-1) ...
Setting up libtirpc1:amd64 (0.2.5-1) ...
Setting up keyutils (1.5.9-8ubuntu1) ...
Setting up rpcbind (0.2.3-0.2) ...
Setting up nfs-common (1:1.2.8-9ubuntu12) ...
Creating config file /etc/idmapd.conf with new version
Progress: [ 98%] [##########################################################]
Adding system user `statd' (UID 110) ...
Adding new user `statd' (UID 110) with group `nogroup' ...
Not creating home directory `/var/lib/nfs'.
nfs-utils.service is a disabled or a static unit, not starting it.
Setting up nfs-kernel-server (1:1.2.8-9ubuntu12) ...
Creating config file /etc/exports with new version
Creating config file /etc/default/nfs-kernel-server with new version
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for ureadahead (0.100.0-19) ...########################.]
Processing triggers for systemd (229-4ubuntu4) ...

Installing the Packages on Client Side

We have to install the nfs packages on the client in general nfs-common ie., the package which provides the access to the NFS share folders from the server.

$ sudo apt update
$ sudo apt install nfs-common
apt install nfs-common
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
watchdog
The following NEW packages will be installed:
nfs-common
0 upgraded, 1 newly installed, 0 to remove and 98 not upgraded.
Need to get 185 kB of archives.
After this operation, 734 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 nfs-common amd64 1:1 .2.8-9ubuntu12 [185 kB]
Fetched 185 kB in 1s (126 kB/s)
Selecting previously unselected package nfs-common.
(Reading database ... 57864 files and directories currently installed.)
Preparing to unpack .../nfs-common_1%3a1.2.8-9ubuntu12_amd64.deb ...
Unpacking nfs-common (1:1.2.8-9ubuntu12) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu4) ...
Setting up nfs-common (1:1.2.8-9ubuntu12) ...

Enabling and Creating the Share Directories on the Server

Here for demo purpose, we are going to share two folders to differentiate the configuration, setting, First one is with superuser permission and other with trusted users on the client system.

Exporting a General Mount

In this example, we will create a general NFS mount that with the default configuration which is difficult for a user without any permissions on the client machine they can access and this can be used to create a shared space to store the project files in the folders.

Creating a Shared Folder for General Purpose

$ sudo mkdir /usr/nfs/common –p

Change the folder permission, so that anybody can write in the folder

$ sudo chown nobody:nogroup /usr/nfs/common

And now try to access the folder from a client with the below command

Before we mount any shared folder on the client, we needed to create a mount point on the client machine

$ sudo mkdir /mnt/nfs/common
$ sudo mount 192.168.1.25:/usr/nfs/common /mnt/nfs/common

This will mount the NFS share on the server 192.168.1.25 on the client machine with /mnt/nfs/common is mounted at /mnt/nfs/common on the client machine and it can be accessed as a local folder.

Creating a Shared folder with Home Directory

We will share user home directories stored on the server to access from the client, the access needed to conveniently manage the users.

Configuring the NFS Settings on the Server

As we are seeing 2 types of NFS Share, let”s see how to configure the setting to match our requirements.

Open /etc/exports file with an editor

$ sudo vi /etc/exports
# /etc/exports: the access control list for filesystems which may be exported
# to NFS clients. See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)

Add below lines to the configuration file –

/usr/nfs/common 192.168.1.100(rw,sync,no_subtree_check)
/home 192.168.1.100(rw,sync,no_root_squash,no_subtree_check)

Below is the explanation for each option we used in the above commands which we used.

Rw -> This will allow client computers to read and write to the share.

Sync -> This will allow the data to be written in the NFS before it applies to queries and It also increase consistent environment and will be stable.

Nosubtreecheck -> This will prevent subtree checking, where if we enable this option, it will cause many problems if the client has opened the file.

Norootsquash -> This will makes the NFS translation request from the root user for the client into a not –privileged users of the server, where it will also prevent the root account on the client from using the file system of the server as root.

$ sudo systemctl restart nfs-kernel-server

Mounting the Directories on the Client

Before, we mount the share folders on the client, we needed to create a mount point and we will link the share folder from the NFS server to the local folders (mount points).

$ mkdir /mnt/common
$ mkdir /mnt/home
$ sudo mount 192.168.1.100:/usr/nfs/common /mnt/common
$ sudo mount 192.168.1.100:/home /mnt/home

After we run the commands we will not verify that the NFS share folders are mounted correctly or not

$ df –h
Filesystem Size Used Avail Use% Mounted on
udev 538M 0 538M 0% /dev
tmpfs 249M 628K 249M 2% /run
/dev/vda1 100G 10G 90G 10% /
tmpfs 445M 0 445M 0% /dev/shm
tmpfs 10.0M 0 10.0M 0% /run/lock
tmpfs 245M 0 245M 0% /sys/fs/cgroup
tmpfs 249M 0 249M 0% /run/user/0
192.168.1.100:/home 124G 11.28G 118.8G 9% /mnt/home
192.168.1.100:/usr/nfs/common 124G 11.28G 118.8G 9% /mnt/common

As we can see that they both share are mounted and we can see them at the bottom, as they are mounted from the same server so we can see the same disk usage.

Mounting the NFS Share at the Boot Time

We can mount the NFS share at the time of boot so that if we needed to connect the NFS share folders, we can directly access the folders at the mount points

Open the /etc/fstab file and add the below lines.

$ sudo vi /etc/fstab

Add the below line at the bottom of the files

. . .
192.168.1.100:/usr/nfs/common /mnt/general nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0
192.168.1.100:/home /mnt/home nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0

Unmounting the NFS Share Folders

As if we do not want to use the folders, we can unmount the NFS share folders using the below commands

$ sudo umount /mnt/common
$ sudo umount /mnt/home

In the above article setup and configure an NFS share on Ubuntu 16.04 with two different NFS mounts, one share is open anybody can read or write to the folder and other with restricted for users.

raja
Published on 23-Jan-2020 07:31:54
Advertisements