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.
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.
Now we need to give the ownership of this directory to the nobody
user and group.
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.
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.
Add the following line to the file.
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.
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).
Finally we will restart the NFS server to make sure that the changes are applied.
Additionally enable the NFS server to start automatically on boot.
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.
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.
Now we can mount our NFS shareable drive using the mount
command.
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.
If you see the output then it means that the drive was mounted successfully.
Now you can easily access the shareable drive from the client machine.
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.
Add the following line to the file.
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.
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.
Once the installation is complete, we need to edit the /etc/auto.master
file.
Add the following line to the file.
Save and close the file.
Now we need to create the /etc/auto.sukuna
file.
Add the following line to the file.
Once again, save and close the file.
Now we need to restart the AutoFS service.
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.
Check with df -h
command to see if the drive is mounted or not.
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 đź‘‹.