Setup Shareable Drive with NFS in Linux

📆 · ⏳ 7 min read · · 👀

Introduction

So one of the goals that I have planned for myself this year in my journey of exploring homelabbing and self hosting is to setup a NAS (Network Attached Storage) server.

I have been using a Raspberry Pi’s and an old laptop as my servers for a while now and I have been using it to host a few services, more details about this can be found on my self hosting journey in 2023 blog.

So while I wait to get the right hardware, I thought I will start exploring the software side of the things and learn how to setup a NAS server. For this I used my old laptop as the temporary NAS server and my Raspberry Pi as the client machine.

This article is a step by step guide on what worked for me to setup a shareable drive with NFS in Linux. I hope this helps you in your journey too.

What is NFS?

Firstly, we need to understand what is NFS and why do we need it, I will keep it brief I promise.

NFS stands for Network File System, it is a protocol that allows us to share files and folders over the network. Like NFS there are other protocols like SMB, AFP, etc. that allow us to share files and folders over the network.

There are lots of articles/videos which cover NFS in detail so you can follow up on those if you want to learn more about it. For example on wikipedia ↗️.

Why I chose NFS?

Well in my setup, I only have Linux machines, so I thought NFS would be a good choice for me. But if you have a mixed setup of Windows and Linux machines, then you might want to consider using SMB.

I have written a detailed article on how to setup a shareable drive with SMB in Linux, so you can check that out if you want to learn more about it.

Let’s get started

So first as a prerequisite, we need to have at least two machines, one which will act as the NFS server and the other which will act as the NFS client.

First let’s setup the NFS server.

NFS Server

We need the nfs-kernel-server package to setup the NFS server, so let’s install it.

Terminal window
sudo apt update -y && sudo apt install nfs-kernel-server -y

Once the installation is complete, we need to create a directory which we want to share with the client machine. For example I created a directory called nfs_share in /media directory.

Terminal window
sudo mkdir -p /media/nfs_share

Now we need to give the ownership of this directory to the nobody user and group.

Terminal window
sudo chown nobody:nogroup /media/nfs_share

We are doing this because we want to make sure that the client machine can access this directory and add files to it.

Now we need to give the read and write permissions to this directory.

Terminal window
sudo chmod 777 /media/nfs_share

This sets up our shareable drive, now we need to configure the NFS server to share this directory with the client machine.

For this we need to edit the /etc/exports file.

Terminal window
sudo vi /etc/exports

Add the following line to the file.

Terminal window
/media/nfs_share 192.168.0.101(rw,sync,no_subtree_check)

Replace 192.168.0.101 with the IP address of your client machine.

💡

Pro Tip!

You can also use the * wildcard to allow all the machines in your network to access the shareable drive.

There might be some security implications of doing this and may not be recommended for everyone, but I am actually using this in my setup.

So the contents of /etc/exports file in my setup looks like this.

Terminal window
/media/nfs_share *(rw,sync,no_subtree_check)

Once you have added the line, save the file and exit.

Now we need to export the configurations that we just added to the /etc/exports file to make it accessible to the client machine(s).

Terminal window
sudo exportfs -a

Finally we will restart the NFS server to make sure that the changes are applied.

Terminal window
sudo systemctl restart nfs-kernel-server

Additionally enable the NFS server to start automatically on boot.

Terminal window
sudo systemctl enable nfs-kernel-server

That’s it, we are done on the NFS server side, now let’s move on to the NFS client.

NFS Client

Now our NFS server is ready to share the directory with the client machine, so let’s setup the client machine.

First we need to install the nfs-common package. This is usually already present in most of the Linux distributions, but if it is not present then you can install it using the following command.

Terminal window
sudo apt update -y && sudo apt install nfs-common -y

Once the installation is complete, we need to create a directory where we want to mount the shareable drive. I will be choosing the /mnt/sukuna directory.

sukuna is the name of the old laptop server so its easier for me to remember but feel free to pick any name you want.

Terminal window
sudo mkdir -p /mnt/sukuna

Now we can mount our NFS shareable drive using the mount command.

Terminal window
sudo mount -t nfs 192.168.0.100:/media/nfs_share /mnt/sukuna

Replace 192.168.0.100 with your NFS server IP address and update paths if you have used different paths.

You can verify if the drive was mounted or not by using the same mount command and grep for the directory name.

Terminal window
mount | grep nfs_share

If you see the output then it means that the drive was mounted successfully.

192.168.0.100:/nfs_share on /mnt/sukuna type nfs (rw,relatime,vers=3,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=192.168.0.100,mountvers=3,mountport=20048,mountproto=tcp,local_lock=none,addr=192.168.0.100)

Now you can easily access the shareable drive from the client machine.

Terminal window
cd /mnt/sukuna && ls -l

Go ahead and create some files and folders in this directory and you will see that the same files and folders are also visible on the NFS server.

Auto Mounting NFS Drive on Boot

While this is great, but if you restart the client machine then you will have to manually mount the drive again. Wouldn’t it be great if the drive is automatically mounted on boot?

Well we can do that by adding an entry to the /etc/fstab file.

Terminal window
sudo vi /etc/fstab

Add the following line to the file.

Terminal window
192.168.0.100:/nfs_share /mnt/sukuna nfs defaults 0 0

Save and close the file. To verify if the entry is correct, you can use the mount -a command to mount all the entries in the /etc/fstab file.

Terminal window
sudo mount -a

Now if you restart the client machine, you will see that the drive is automatically mounted.

Mount using AutoFS

While the above method works great, but there is a small problem with it. If the NFS server is down then the client machine will not boot up.

And for me, I have this exact scenario where I have a Raspberry Pi as my NFS client and an old laptop as my NFS server. I don’t want to keep my laptop running all the time, so I only turn it on when I need to access the shareable drive.

So in my case, if the laptop is down then the Raspberry Pi will not boot up. So I need a way to mount the drive only when I need it.

That’s when I came across AutoFS when I was lurking on the internet. AutoFS is a tool that allows us to automatically mount the drive when we access it and unmount it when the drive is not being used.

So let’s see how we can setup AutoFS.

First we need to install the autofs package.

Terminal window
sudo apt update -y && sudo apt install autofs -y

Once the installation is complete, we need to edit the /etc/auto.master file.

Terminal window
sudo vi /etc/auto.master

Add the following line to the file.

Terminal window
/mnt /etc/auto.sukuna --ghost --timeout=60

Save and close the file.

Now we need to create the /etc/auto.sukuna file.

Terminal window
sudo vi /etc/auto.sukuna

Add the following line to the file.

Terminal window
sukuna -fstype=nfs4,rw 192.168.0.100:/media/nfs_share

Once again, save and close the file.

Now we need to restart the AutoFS service.

Terminal window
sudo systemctl restart autofs

That’s it, we are done. Now if you access the /mnt/sukuna directory (or whatever if your file path), you will see that the drive is automatically mounted.

Terminal window
cd /mnt/sukuna && ls -l

Check with df -h command to see if the drive is mounted or not.

Terminal window
df -h

And wait for the timeout period to pass and you will see that the drive is automatically unmounted again using the df -h command.


I am quite happy with this setup and even though I am using it for a temporary setup, I think I will continue to use it even after I get my NAS server since it works well even if the NFS server is down.

Conclusion

So that’s it, this is how you can setup a shareable drive with NFS in Linux. I hope this article was helpful to you and you were able to setup your own shareable drive.

I hope you found this article useful. If you have any questions or feedback, please feel free to reach out to me on X / Twitter ↗️.

Until next time 👋.

You may also like

  • # linux# homelab# networking

    Setup Shareable Drive with Samba in Linux

    In this article we will setup a shareable drive in Linux with SMB. We will learn how to setup the share directory using Samba on server and how to mount it on client.

  • # linux# homelab

    HTTPS with self-signed certificates for your Homelab services

    In this article we will deep dive into understanding how we can setup HTTPS with self-signed certificates for our Homelab services.This is often required when you are running your own services and you want to access them over HTTPS.

  • # homelab# networking

    AdGuard Home + Tailscale = Erase Ads on the Go

    Fed up with pesky online ads? In this blog, I'll show you how a dynamic duo, AdGuard Home and Tailscale, can give you ad-free browsing anytime, anywhere. It's a technical adventure that's worth every click.