Open In App

How to Share Files Between Linux Computers Using NFS

Last Updated : 03 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 utilitis
Installing NFS utilitis

Here, '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
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 shares
Discovering available NFS shares

4. 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 Folder
Creating a Shared Folder

5. 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/exports
Setting Up NFS Exports - making changes into /etc/exports

Add 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 Exports
Setting Up NFS Exports

6. 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 Directory
Exporting the Shared Directory

7. 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 directory
Setting Permissions to the shared directory

Replace <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 Firewall
Permitting NFS Through Firewall

Connecting 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 systems
Installing NFS in client systems

2. 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/fstab
making mount permanent with sudo nano /etc/fstab

Add 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 Permanent
Making the Mount Permanent

To verify the mount:

sudo mount | grep -i nfs
Verifying the mount
Verifying the mount

Conclusion

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.


Next Article
Article Tags :

Similar Reads