0 - Introduction
Have you ever wanted to share a folder from, for example, your desktop pc to your laptop while you are at home? This article will teach you how to share a folder using NFS. If your client is a Windows machine (in the example, your laptop), we recommend using SAMBA (SMB) instead.
We also recommend updating both the client and server before starting the tutorial with these two commands:
sudo apt update
sudo apt upgrade
1 - Setup server
Let’s start by installing the nfs server in the machine that will be hosting the folder:
sudo apt install nfs-kernel-server
Now we need to create one, or many folders that are going to be shared, but those folders should not be owned by root, for safety reasons, so we change the ownership to ‘nobody’ of ‘nogroup’, here are some examples:
# The rest of the guide will use only this folder
sudo mkdir /var/nfs/general -p
sudo chown nobody:nogroup /var/nfs/general
sudo chmod 777 /var/nfs/general
sudo mkdir /var/nfs/photos -p
sudo chown nobody:nogroup /var/nfs/photos
sudo chmod 777 /var/nfs/photos
sudo mkdir /var/nfs/videos -p
sudo chown nobody:nogroup /var/nfs/videos
sudo chmod 777 /var/nfs/videos
Now that we have the folders that will be exported, we need to export them, and we do that by editing a file, in the following command you can switch nano with any text editor you like as long it is able to edit files in a protected folder (with sudo):
sudo nano /etc/exports
In this file you will have to write the path to your folder, which hostnames can access it (‘*’ for all) and the permissions/options:
/var/nfs/general *(rw,sync,no_subtree_check)
You might be confused with what are those words in parentheses, so here is what some of these options mean:
- ‘rw’ – The client has read and write access
- ‘sync’ – The server will write changes to disk before replying. This usually results in a more stable experience, since the reply you get from the server reflects the state of the disk, but it also reduces the speed while transfering many files.
- ‘no_subtree_check’ – This option removes a check that the server does for every request, of whether the file is still available or not, this however can cause a lot of problems if a user has a file open and another user renames it.
- ‘no_root_squash’ – This disables a security measure that prevents a root account on a client to access the host as root.
After editing the ‘exports’ file, go ahead and restart the nfs service to apply changes:
sudo systemctl restart nfs-kernel-server
If you want to make your folder available from outside your home network, you might need to mess with your firewall, but that is outside the scope of this guide.
2 - Setup client
On the client we will start by installing the nfs tools by doing the command:
sudo apt install nfs-common
After installing the ‘nfs-common’ package you will need to ‘mount’ the remote folder to your machine, starting by creating a folder where the remote folder will be mounted and only then ‘mounting’ it, don’t forget to replace ‘host_ip’ with your server’s ip:
sudo mkdir -p /mnt/nfsgeneral
sudo mount host_ip:/var/nfs/general /mnt/nfsgeneral
And that’s it, you now are ready to use the remote folder! If you need to unmount the folder after, you can do:
sudo umount -f -l /mnt/nfsgeneral
sudo rm -r /mnt/nfsgeneral
Thanks for reading! Stay tuned for more tech insights and tutorials. Until next time, and keep exploring the world of tech!