NFS Installation in Linux

Network File System (NFS) allows a system to share directories and files with others over a network. Below is a step-by-step guide to install and configure NFS on a Linux system.

Ubuntu Package name: nfs-common
Redhat Package name: nfs-utils
Demon Name: nfsd / nfs-server
Configuration File: /etc/exports
Service Name: nfs-server
Port Number: 2049

Prerequisites form client end

Ensure you have root or sudo privileges on the system where you want to install NFS.

Make sure your system is updated:


        sudo apt update && sudo apt upgrade
    

Install the necessary packages:


        sudo apt install nfs-common
    

Ensure that the system has a static IP address assigned.

Check the current IP address:


        ip addr show
    

Make sure the firewall allows NFS traffic:

NFS Installation in Ubuntu

Step 1: Install NFS Server

Run the following command to install the NFS server package:


        sudo apt update && sudo apt install nfs-kernel-server
    

Step 2: Create a Shared Directory

Create a directory that you want to share over the network:


        sudo mkdir -p /mnt/shared
    

Set the appropriate permissions:


        sudo chown nobody:nogroup /mnt/shared
    

Step 3: Configure Exports

Edit the /etc/exports file to define the shared directory and its permissions:


        sudo nano /etc/exports
    

Add the following line:


        /mnt/shared 192.168.1.0/24(rw,sync,no_subtree_check)
    

Replace 192.168.1.0/24 with your network's IP range.

Type of permission (rw)"Read-write", (ro)"Read-only"

Step 4: Apply Changes

Run the following command to apply the changes:


        sudo exportfs -a
    

Restart the NFS server:


        sudo systemctl restart nfs-kernel-server
    

Step 5: Configure Firewall

Allow NFS through the firewall:


        sudo ufw allow from 192.168.1.0/24 to any port nfs
    

Step 6: Mount NFS Share on Client

On the client machine, install the NFS client package:


        sudo apt install nfs-common
    

Mount the shared directory:


        sudo mount 192.168.1.100:/mnt/shared /mnt
    

Replace 192.168.1.100 with the server's IP address.

Conclusion

You have successfully installed and configured NFS on your Linux system. The shared directory is now accessible from the client machine.

NFS Installation in Redhat

Step 1: Install NFS Server in Redhat

Run the following command to install the NFS server package:


        sudo yum install nfs-utils
    

Enable and start the NFS service:


        sudo systemctl enable nfs-server
        sudo systemctl start nfs-server
    

Check the status of the NFS service:


        sudo systemctl status nfs-server
    

Step 2: Create a Shared Directory

Create a directory that you want to share over the network:


        sudo mkdir -p /mnt/shared
    

Set the appropriate permissions:


        sudo chown nobody:nogroup /mnt/shared
    

Step 3: Configure Exports

Edit the /etc/exports file to define the shared directory and its permissions:


        sudo vim /etc/exports
    

Add the following line:


        /mnt/shared 192.168.1.0/24(rw,sync)
    

Replace 192.168.1.0/24 with your network's IP range.

Replace *(rw) Other method to give all connected machine to share

Type of permission (rw)"Read-write", (ro)"Read-only"

Step 4: Apply Changes

Run the following command to apply the changes:


        sudo exportfs -a
    

Restart the NFS server:


        sudo systemctl restart nfs-server
    

Step 5: Configure Firewall

Allow NFS through the firewall:


        sudo firewall-cmd --permanent --add-service=nfs
        sudo firewall-cmd --permanent --add-service=rpc-bind
        sudo firewall-cmd --permanent --add-service=mountd
    

Reload the firewall:


        sudo firewall-cmd --reload
    

Step 6: Mount NFS Share on Client

On the client machine, install the NFS client package:


        sudo yum install nfs-utils
    

Mount the shared directory:

NFS Client Side Installation

Step 1: Install NFS Client

Run the following command to install the NFS client package:


        sudo apt install nfs-common
    

For Redhat, use:


        sudo yum install nfs-utils
    

Enable and start the NFS service:


        sudo systemctl enable nfs-common
    

        sudo systemctl start nfs-common
    

For Redhat, use:


        sudo systemctl enable nfs-server
    

        sudo systemctl start nfs-server
    

Check the status of the NFS service:


        sudo systemctl status nfs-common
    

For Redhat, use:


        sudo systemctl status nfs-server
    

Check the NFS share available on the server:

Step 2: Create a Mount Point

Create a directory where you want to mount the NFS share:


        sudo mkdir -p /mnt/nfs_share
    

Step 3: Mount the NFS Share

Mount the NFS share using the following command:


        -> To check the NFS share available on the server:
        showmount -e <server_ip_address>      #  Replace with the server's IP address

        -> Mount the NFS share:
        sudo mount -t nfs <server_ip_address>:/mnt/shared /mnt/nfs_share

        -> If there is different version of NFS, use:
        sudo mount -t nfs -o nfsvers=4 <server_ip_address>:/mnt/shared /mnt/nfs_share

    

Replace <server_ip_address> with the server's IP address.

To check the mounted NFS share:


        df -h
    

NFS Permanently add in Fstab

To make the NFS share mount automatically at boot, add the following line to the /etc/fstab file:


        <server_ip_address>:/mnt/shared /mnt/nfs_share nfs defaults 0 0
    

Replace <server_ip_address> with the server's IP address.

Save and exit the file.