This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has
sudoaccess and that you precede each of the privileged commands with
Network File System (NFS) is a distributed file system protocol. The NFS allows the user to access the files present in the network.
This tutorial will help you to install and configure NFS Server on Ubuntu system and use it on a NFS client system.
Install NFS Server
Before we install the NFS kernal server in our machine we need to update our local repository. For this we can use the following command.
To install and configure the NFS server we are using the following command
apt-get install nfs-kernel-server portmap
We need to create a export directory now, For this we create a directory in the machine and make that folder accessible by the client machines. The folder named share is created in the mnt directory of our machine.
sudo mkdir /mnt/share
You should remove the restrictive permissions as we want our client machines to access this folder.This can be done by the following command.
chown nobody:nogroup /mnt/share
Now ,to make our clients machines to access the folder we need to create the following configuration in the export file. We can access the exports file and edt by the following command.
We use the following settings as we want to share the share folder created by us.
/etc/exports /mnt/share 192.168.1.110(rw,sync,no_subtree_check)
Here /mnt/share can only be accessed from IP 192.168.1.110.
To apply the configuration we must run the following command.
To view the exported directory you can use the following command .
It will give you the output as follows
Output: /mnt/share 192.168.1.110(rw,wdelay,no_root_squash,no_subtree_check)
We need to restart the server to make the changes to take place by the following command
systemctl restart nfs-kernel-server
Open Firewall For Clients
After all the configuration we must open the port for the client so that the client can communicate with the server. Here we assume the client ip to be 192.168.102 and we open the port for them
ufw allow from 192.168.102 to any port nfs
We can check the status of the open ports by following command
Output Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere 2049 ALLOW 192.168.1.110 OpenSSH (v6) ALLOW Anywhere (v6)
Next we need to configure the client machine to get the share folder from the client .
Before instaling NFS client we should update the local repository of your machine. You can use the following command
We need to install the following packages on NFS client system, which is required to mount the remote directory using NFS protocol.
apt-get install nfs-common portmap
Now we should create the mount directories for mounting server nfs exported directories. This can be done by the following command
After creating mount directory, mount remote NFS exported directory using following command.
mount 192.168.1.100:/opt/share /mnt/share
We can check the mounted directory by the following command
sudo df -h
Output: Filesystem Size Used Avail Use% Mounted on /dev/sda1 20G 2.8G 16G 16% / udev 371M 4.0K 371M 1% /dev 192.168.1.100:/opt/share 20G 20G 16G 0% /mnt/share
Test The Connection
We can now create a sample file from the NFS server mount directory and try to access that file from the client mount directory.