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.
sudo apt update -y && sudo apt install nfs-kernel-server -yOnce 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.
sudo mkdir -p /media/nfs_shareNow we need to give the ownership of this directory to the nobody user and group.
sudo chown nobody:nogroup /media/nfs_shareWe 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.
sudo chmod 777 /media/nfs_shareThis 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.
sudo vi /etc/exportsAdd the following line to the file.
/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.
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.
/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).
sudo exportfs -aFinally we will restart the NFS server to make sure that the changes are applied.
sudo systemctl restart nfs-kernel-serverAdditionally enable the NFS server to start automatically on boot.
sudo systemctl enable nfs-kernel-serverThatās it, we are done on the NFS server side, now letās move on to the NFS client.
Death is hard enough. Accessing accounts shouldn't be.
When someone dies, you don't get even one extra second to access the documents and information they meant to share it with you. Trying to fix this problem with Eternal Vault.
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.
sudo apt update -y && sudo apt install nfs-common -yOnce 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.
sudo mkdir -p /mnt/sukunaNow we can mount our NFS shareable drive using the mount command.
sudo mount -t nfs 192.168.0.100:/media/nfs_share /mnt/sukunaReplace 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.
mount | grep nfs_shareIf 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.
cd /mnt/sukuna && ls -lGo 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.
sudo vi /etc/fstabAdd the following line to the file.
192.168.0.100:/nfs_share /mnt/sukuna nfs defaults 0 0Save 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.
sudo mount -aNow 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.
sudo apt update -y && sudo apt install autofs -yOnce the installation is complete, we need to edit the /etc/auto.master file.
sudo vi /etc/auto.masterAdd the following line to the file.
/mnt /etc/auto.sukuna --ghost --timeout=60Save and close the file.
Now we need to create the /etc/auto.sukuna file.
sudo vi /etc/auto.sukunaAdd the following line to the file.
sukuna -fstype=nfs4,rw 192.168.0.100:/media/nfs_shareOnce again, save and close the file.
Now we need to restart the AutoFS service.
sudo systemctl restart autofsThatā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.
cd /mnt/sukuna && ls -lCheck with df -h command to see if the drive is mounted or not.
df -hAnd 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 š.