In this article I will describe how to connect to an NFS share from your Raspberry Pi using an NFS client.
NFS stands for Network File System, a protocol used for distributed file systems.
To connect to an NFS server, we need to install and configure an NFS client on the Raspberry Pi.
NFS is commonly used to connect to a NAS, as it is a very well-supported protocol.
You can also configure your Raspberry Pi to act as an NFS server if you need to share files from it.
As you will see, the steps to set up the NFS client are fairly simple and take just a few moments.
Installing the NFS client on the Raspberry Pi
Before we can connect to an NFS share, we first need to install and configure the NFS client on the Raspberry Pi.
Fortunately, all the packages needed to connect to an NFS server are available in the base Raspberry Pi OS repository.
- Before installing the NFS client, run the usual update commands:
sudo apt update && sudo apt full-upgrade -y
- Now that the system is up to date, we can install the NFS client on the Raspberry Pi with the following command:
sudo apt install nfs-common
This package contains everything we need to connect to an NFS server from our device.
Connecting to an NFS server using the nfs-common client
Now we can connect to an NFS share from the Raspberry Pi. Bear in mind that you need to know the IP address or hostname of the NFS server you want to connect to.
To connect to the NFS server, we will use the mount command on the Raspberry Pi.
Mounting the entire directory tree
The basic command to mount all the directories exported by the server is shown below:
sudo mount -t nfs -o proto=tcp,port=2049 [IP_del_tuo_server_NFS]:/ /mnt
When entering this command, replace [IP_del_tuo_server_NFS] with the IP address of your NFS server.
This command specifies the type as nfs using the -t argument. It also specifies the tcp protocol and port 2049, which is the port NFS typically runs on. Next comes the IP address or hostname of the NFS server we want to connect to. By using :/ immediately after the IP address, we specify that we want to mount the top-level directory exported by the server. Finally, we need to specify where we want the NFS share to be mounted. In this example we are using the “/mnt” directory
Mounting a specific directory
In some cases, we may only need to mount a specific directory exported by the NFS server. This requires just a few small changes to the previous command.
sudo mount -t nfs -o proto=tcp,port=2049 [IP_del_tuo_server_NFS]:/[cartella_da_montare] /mnt
As in the previous example, be sure to replace “[IP_del_tuo_server_NFS]” with the IP address of your NFS server and “” with the name of the directory. As you can see from the command above, all you need to specify is the name of the exported directory after “[cartella_da_montare]:/“. The rest of the command is the same as in the previous example, specifying the IP, mount type and mount location.
Mounting an NFS share at boot
Having to run the mount command every time you want to connect to your NFS share from the Raspberry Pi will quickly become tedious.
To get around this, we can edit the fstab file.
This file allows the Raspberry Pi to mount our network share automatically at boot.
- Let’s start by editing the
fstabfile.
We can edit this file with the nano text editor by running the following command.
sudo nano /etc/fstab
- At the bottom of the file, add the following line.
[IP_del_tuo_server_NFS]:/[cartella_da_montare] /mnt,username=[username],password=[password] nfs auto 0 0
When entering this line, be sure to replace “” with the IP address of your NFS server, and “[IP_del_tuo_server_NFS][username]” and “[password]” with the credentials authorised to access the network share.
You also need to replace “” with the name of the network directory you want to mount. If you want to mount the entire directory tree, remove “[cartella_da_montare]” entirely.[cartella_da_montare]
Finally, if you want to change where the network directory is mounted, replace “/mnt” with the new location.
- Now save the file by pressing
CTRL+X, thenY(orS, depending on your system language), followed by theENTERkey to confirm. - All that’s left now is to check that it works by restarting the Raspberry Pi.
You can restart the device with the following command:
sudo reboot now
You should now have installed and configured an NFS client on your Raspberry Pi.
You should also have an idea of how to connect to an NFS server with ease. If you ran into any difficulties with this guide, feel free to leave a comment below.
As always, enjoy!
