How to Share Files Between Linux Computers Using NFS
Last Updated :
03 Apr, 2025
The Network File System, or NFS protocol, is widely used in Linux for sharing files and directories between Linux systems on the same network. Here, the shared directories are created on a file server running the NFS server component. The files are added to these folders and then shared with other Linux computers after the users are granted permission to access the folder. An NFS file share is mounted on the client machine, thus making it available similar to the folders made locally. Therefore, to set up a Network File System (NFS), both the NFS server and client need to be configured.
Creating the Server
1. Installing NFS Utilities
The NFS utilities are to be installed in both server and client machines. The packages for the NFS utility can be installed using the packet manager for the specific Linux distribution.
For Debian/Ubuntu
sudo apt update
sudo apt install nfs-kernel-server
Here, 'apt update' ensures your package list is up-to-date before installation, and
'nfs-kernel-server' is the package that provides the NFS server components.
For RedHat/CentOS
sudo yum install nfs-utils
Installing NFS utilitisHere, 'yum install' retrieves and installs the necessary packages, and
'nfs-utils' is the package that includes NFS server utilities.
2. Starting the NFS server
For Debian/Ubuntu
sudo systemctl start nfs-kernel-server.service
For RedHat/CentOS
sudo systemctl enable --now nfs-server
Starting the NFS server- 'systemctl enable --now' starts and enables the NFS server to automatically start on boot.
- 'nfs-server' is the systemd service unit for NFS on RedHat/CentOS.
3. Discovering available NFS shares
We can also discover all the available NFS shares using the command below.
showmount -e server_IP
Discovering available NFS shares4. Creating a Shared Folder
After installing the required packages, we need to create the folder/directory to be shared on the server machine.
sudo mkdir -p /mnt/shared
ls -ld /mnt/shared
Creating a Shared Folder5. Setting Up NFS Exports
Next, we will define the directory to be shared and the clients that are to be allowed to access it. This is done by adding the client IPs in /etc/exports file as shown below.
sudo nano /etc/exports
Setting Up NFS Exports - making changes into /etc/exportsAdd the following line to share the directory with a single client:
/mnt/shared client_IP(rw,sync,no_subtree_check)
Replace client_IP with the actual IP of the client machine. Save and close the document.
Setting Up NFS Exports6. Exporting the Shared Directory
In the next step, we need to configure the export file by exporting the shared directories to make them available to the client machines.
sudo exportfs -a
Exporting the Shared Directory7. Setting Permissions to the shared directory
Now that we are done exporting the shared directory, we have to define permissions for its access.
sudo chown -R <user>:<groupname> /mnt/shared
sudo chmod -R 775 /mnt/shared
ls -l /mnt
Setting Permissions to the shared directoryReplace <user> with username and <groupname> with the group that is to be granted access. chmod -R 775 sets permissions to allow read, write, and execute for the owner and group, and read and execute for others.
8. Permitting NFS Through Firewall
If the firewall is running, we have to enable the NFS traffic.
Debian/Ubuntu
sudo ufw allow from client_IP to any port nfs
CentOS/RedHat
sudo firewall-cmd --permanent --add-service=nfs
sudo firewall-cmd --reload
Permitting NFS Through FirewallConnecting the Client's Computers
1. Installing NFS in client systems
For the client machines to access the shared files using NFS we need to install the NFS client on all Linux systems in the network.
For Debian/Ubuntu
sudo apt install nfs-common
For RedHat/CentOS
sudo dnf install nfs-utils
Installing NFS in client systems2. Mounting the Shared Directory on the Client Machine
On the client machine, we need to create the mounting point and run the mounting command to access the shared directory as shown below.
sudo mkdir -p /mnt/nfs_shared
sudo mount server_IP:/mnt/shared /mnt/nfs_shared
3. Making the Mount Permanent
To make the mount permanent and ensure that the shared directory mounts at boot, add the following line to the /etc/fstab file on the client machine:
sudo nano /etc/fstab
making mount permanent with sudo nano /etc/fstabAdd the line as below:
mserver_IP:/mnt/shared /mnt/nfs_shared nfs defaults 0 0
Replace server_IP with the IP of the NFS server. Save and close the document.
In the above line,
- 'server_IP:/mnt/shared' specifies the NFS server's IP and shared directory path.
- '/mnt/nfs_shared' is the local mount point on the client machine.
- 'nfs' indicates the file system type (NFS).
- 'defaults' uses default mount options.
- '0 0' specifies options for file system checks.
Making the Mount PermanentTo verify the mount:
sudo mount | grep -i nfs
Verifying the mountConclusion
NFS provides an efficient file-sharing solution to all Linux users. In fact, it is very advantageous when disk space is limited. Following the steps above one can easily configure and share files using NFS in Linux.
Similar Reads
How to create a Shared Folder between two Local User in Linux? This article shows how to set-up a shared folder between two local users in Linux. The shared directory/folder will be accessible to both the users, they will be able to read/write each other's file.Let us create shared directory /home/shareFolder for user Bob and Alice and add them to a common grou
2 min read
How to Securely Copy Files in Linux | scp Command Secure file transfer is a crucial part of Linux systems administration. Whether moving sensitive files between local machines or transferring data between servers, or you need to move backup files to a remote server, fetch logs from a hosted machine, or sync directories across multiple systems, scp
10 min read
How to Share File in IPFS Blockchain? IPFS stands for Interplanetary File System. It is a distributed peer-to-peer network and a protocol for organizing, storing and sharing files and data. It works on the blockchain principle as IPFS itself is a decentralized file system on a distributed network. So, before moving ahead with the file t
10 min read
How to Copy a File in Node.js? Node.js, with its robust file system (fs) module, offers several methods to copy files. Whether you're building a command-line tool, a web server, or a desktop application, understanding how to copy files is essential. This article will explore various ways to copy a file in Node.js, catering to bot
2 min read
How To Share File From Host Machine(Windows) To Guest Machine(Linux) We need to have Ubuntu installed in our Virtual Box for the purpose of this experiment. The host machine is Windows 10 in the following experiment. Transfer File From Host Machine(Windows) To Guest Machine(Linux) 1. Method 1: Installing SSH on Ubuntu Terminal and allowing Firewall blockage Open Term
4 min read