If you store all your files, videos and data on one computer, and you wanted to access the files on another computer, setting up a NFS server is one of the easiest way to share files over the network.
What is NFS?
NFS stands for Network File System and it is a distributed file system protocol that allow a client computer to access files on the host computer over the network.
Here is the guide to setup NFS server and mount NFS share on Ubuntu. Even though this tutorial is for Ubuntu, the instruction is applicable for most Linux distros too.
For this guide, you will need to have access to two (or more) Ubuntu computers and sudo
permission to install software. Both the computers have to be connected to the internet, and preferably on the same local network. One of the computer will be used as the host server, while the other as a client.
Setup NFS Server on Host
Before we start, open a terminal and update the system:
<span style="color: #c20cb9; font-weight: bold;">sudo</span> apt update <span style="color: #000000; font-weight: bold;">&&</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> apt <span style="color: #660033;">-y</span> upgrade
After the update/upgrade, install the NFS server:
<span style="color: #c20cb9; font-weight: bold;">sudo</span> apt <span style="color: #c20cb9; font-weight: bold;">install</span> nfs-kernel-server
Next, we are going to create a folder to be shared over the network. This can be a folder in the user or root directory.
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #000000; font-weight: bold;">/</span>mnt<span style="color: #000000; font-weight: bold;">/</span>NFSHostFolder
Remove the folder permission so the clients can access and make changes to it:
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chown</span> nobody:nogroup <span style="color: #000000; font-weight: bold;">/</span>mnt<span style="color: #000000; font-weight: bold;">/</span>NFSHostFolder<br /><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #000000;">777</span> <span style="color: #000000; font-weight: bold;">/</span>mnt<span style="color: #000000; font-weight: bold;">/</span>NFSHostFolder
For the client to be able to access this NFS server, we need to specify the client’s IP address in the “exports” file.
Open the “exports” file:
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>exports
and add this line to the end of the file
<span style="color: #000000; font-weight: bold;">/</span>mnt<span style="color: #000000; font-weight: bold;">/</span>NFSHostFolder clientIP<span style="color: #7a0874; font-weight: bold;">(</span>rw,<span style="color: #c20cb9; font-weight: bold;">sync</span>,no_subtree_check<span style="color: #7a0874; font-weight: bold;">)</span>
Replace “clientIP” with the actual IP address of the client computer. Press Ctrl + O to save and Ctrl + X to exit. You can add multiple lines if you have multiple clients to connect to.
Refresh the export:
<span style="color: #c20cb9; font-weight: bold;">sudo</span> exportfs <span style="color: #660033;">-a</span><br /><span style="color: #c20cb9; font-weight: bold;">sudo</span> systemctl restart nfs-kernel-server
Add Firewall to allow access to client
If you have a firewall setup for your host computer, now is the time to open the NFS port for access from the client’s IP address:
<span style="color: #c20cb9; font-weight: bold;">sudo</span> ufw allow from clientIP to any port nfs
Change the “clientIP” to the actual client’s IP address.
Mount NFS Shares in Client
In the client Ubuntu computer, first update/upgrade the computer and install the NFS files:
<span style="color: #c20cb9; font-weight: bold;">sudo</span> apt update <span style="color: #000000; font-weight: bold;">&&</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> apt <span style="color: #660033;">-y</span> upgrade<br /><span style="color: #c20cb9; font-weight: bold;">sudo</span> apt <span style="color: #c20cb9; font-weight: bold;">install</span> nfs-common
Next, create a new folder to mount the NFS share. This folder can be in the user or root directory:
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #000000; font-weight: bold;">/</span>mnt<span style="color: #000000; font-weight: bold;">/</span>NFSClientFolder
Lastly, mount the NFS share:
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mount</span> serverIP:<span style="color: #000000; font-weight: bold;">/</span>mnt<span style="color: #000000; font-weight: bold;">/</span>NFSHostFolder <span style="color: #000000; font-weight: bold;">/</span>mnt<span style="color: #000000; font-weight: bold;">/</span>NFSClientFolder
You might also want to add the entries to “/etc/fstab” so it will automount when it boot up.
Open the “/etc/fstab” file:
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>fstab
Add the following line to the end of the file:
serverIP:<span style="color: #000000; font-weight: bold;">/</span>mnt<span style="color: #000000; font-weight: bold;">/</span>NFSHostFolder <span style="color: #000000; font-weight: bold;">/</span>mnt<span style="color: #000000; font-weight: bold;">/</span>NFSClientFolder nfs auto,nofail,noatime,nolock,intr,tcp,<span style="color: #007800;">actimeo</span>=<span style="color: #000000;">1800</span> <span style="color: #000000;">0</span> <span style="color: #000000;">0</span>
Replace “serverIP” with the actual IP address of the host computer. Press Ctrl + O to save and Ctrl + X to exit.
That’s it.
Be the first to comment! Get the discussion going.