Horje
How to Share Files Between Linux Computers Using NFS

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.

Files Between Linux Computers Using NFS – FAQs

What is NFS in Linux?

NFS (Network File System) is a protocol in Linux that allows a user on a client computer to access files on a network just as the files were locally stored.

How to install NFS on my Linux server?

To install NFS on the Linux server, we can use the Linux system package manager to install the ‘nfs-kernel-server’ package.

Can we restrict access to NFS shares?

Yes, we can specify which clients can access NFS shares by configuring the /etc/exports file and setting permissions accordingly. Here you can add the client machine IPs and also specify the read-write permissions.

Is NFS secure for file sharing?

NFS is secure when it is configured correctly with proper firewall settings (ufw, firewalld) and permissions (chmod).




Reffered: https://www.geeksforgeeks.org


Linux Unix

Related
How To Free Up Space and Speed up Kali Linux? How To Free Up Space and Speed up Kali Linux?
How to use John the Ripper in Kali Linux How to use John the Ripper in Kali Linux
How to use Flatpak? Step-By-Step Guide How to use Flatpak? Step-By-Step Guide
Using Square Brackets in Bash Using Square Brackets in Bash
IPv6 Iptables Rules IPv6 Iptables Rules

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
18