How-To: Filesharing Between Linux Machines Using NFS

How-To: Filesharing Between Linux Machines Using NFS

If your experience is anything like mine, you have no issues connecting a Windows client to a Linux fileserver via Samba / CIFS. But you've run into a number of issues with using Samba / CIFS to connect a Linux client to a Linux fileserver. Judging from the number of posts on the Interwebs, you and I are not the only ones. Even after spending much time researching and attempting fixes, we find the problem to be stubborn.

If you are connecting Linux clients to Linux fileservers, why not use the native NFS filesharing solution? That's what this article is about.

Setting up the Linux fileserver

Install NFS server component

Ubuntu / Debian:

sudo apt update
sudo apt install nfs-kernel-server

CentOS / Fedora:

yum -y install nfs-utils
apt-get install nfs-kernel-server

Arch / Manjaro:

sudo pacman -Syu nfs-utils

Create root NFS directory

If your server already has a directory or mountpoint that is to be used for filesharing, skip this section.

A filesharing directory with adequate permissions for basic use can be created like this:

sudo mkdir /mnt/sharedfolder
sudo chown nobody:nogroup /mnt/sharedfolder
sudo chmod 777 /mnt/sharedfolder

Define NFS client access on server

The NFS exports file is normally located at /etc/exports - In order to add access for all computers on a subnet, the subnet will have to be specified. For this example, we'll assume that the network's local address is 192.168.0.x. This can be determined by entering ip a at the command prompt and looking for the network address of the active network adapter (Ethernet or WiFi, generally).

Open /etc/exports as sudo using your favorite text editor and enter the following all on one line:

/mnt/sharedfolder 192.168.0.1/24 (rw,sync,no_subtree_check)

Save and close the file, and enter the following command to enable the changes:

Ubuntu / Debian / CentOS / Fedora:

sudo exportfs -a
sudo systemctl restart nfs-kernel-server

Arch / Manjaro:

sudo exportfs -arv
sudo systemctl restart nfs-server.service

If a firewall is enabled on the server, access will need to be granted using the sudo ufw allow command.

Setting up the Linux client machine

Install NFS client component

Ubuntu / Debian:

sudo apt update
sudo apt install nfs-common

CentOS / Fedora:

yum -y install nfs-utils

Arch / Manjaro:

sudo pacman -Syu nfs-utils

Mounting NFS file share

The most common places to create a mountpoint on a Linux machine are /mnt/sharename or /media/sharename. We'll use the first for this example:

sudo mkdir /mnt/fileserver

Assuming the NFS fileserver is at 192.168.0.10, use the following command to mount it (enter this all on one line):

sudo mount -t nfs 192.168.0.10:/sharedfolder /mnt/fileserver

The NFS mountpoint can be added to the /etc/fstab file so it is mounted on every boot, but this can lead to long boot times and possibly a failed boot if the fileserver is not available at the time of booting. If adding the above example to /etc/fstab, it should look something like this (again, all on one line):

192.168.0.10:/sharedfolder /mnt/fileserver nfs defaults 0 0

Common issues

If your fileserver already has a directory structure you'll be sharing using NFS,  you may run into permissions issues. Generally, these can be fixed with chmod and chown with the recursive option to set the proper permissions for your network.

The only system that we couldn't get to work as an NFS client was Manjaro running on an ODROID-N2, but we can't get Samba / CIFS to work on that system either, so there may be something wrong with the install on that device.

Additional NFS resources regarding Arch / Manjaro and derivitave operating systems can be found here: https://wiki.archlinux.org/title/NFS

Cool Ideas

PinePhone

With your home Linux fileserver set up with NFS, your PinePhone could automatically use rsync when connected to your home network. Your important files would be automatically synchronized between the PinePhone and the fileserver.

This could also be used to send new music, pictures, documents and/or videos from the home server to the PinePhone.

If you know someone else who owns a PinePhone or other Linux phone, you can each run NFS server on your PinePhone and easily send files to each other.

Another option for accessing files (and the X11-based user interface) on your PinePhone is to use DWS Remote Control available at https://www.dwservice.net/

 

Linux IP Cameras

If you wanted to create a Linux IP camera out of a single board computer and inexpensive webcam or other imaging device, NFS and rsync could be used to reliably move images off the internal storage and onto a network video file server. This could also be a great solution for the PineCube Linux Single-Cube Computer.

 

ODROID-Go Advance / Super

NFS and rsync again could be used with any of the Linux-based retrogaming operating systems to keep games in sync with a home NAS.

 

In-Car Infotainment System

For those DIYers who have added Linux-based SBCs to their vehicle, the dashcam footage and driving data could be automatically uploaded to the home NAS system when the car gets within WiFi range of the home network.

 

DIY Robots

When you build your home robot, you'll definitely want a way for it to read and write files to your fileserver.

Conclusion

Using NFS, filesharing across the Linux network can be done relatively simply without the added complications of setting up additional Samba / CIFS users and permissions, and the difficulties of dealing with incompatibilities between different SMB / CIFS versions on the client and server machines.

Previous article Ultimate Guide to Free Games for Batocera

Leave a comment

Comments must be approved before appearing

* Required fields

x