Saturday, September 16, 2023

Setting Up an NFS Server on Red Hat 8 / CentOS 8 and Mounting on a Client

Introduction


Network File System (NFS) is a popular protocol that allows you to share files and directories between Unix-like systems over a network. In this guide, we will walk you through the process of creating an NFS server on Red Hat 8 or CentOS 8 and then mounting that NFS share on a client machine. NFS is an efficient and reliable way to share files and directories among systems in a network.


Network File System (NFS) is a distributed file system protocol originally developed by Sun Microsystems (Sun) in 1984,It allows you to mount your local file systems over a network and remote hosts to interact with them as they are mounted locally on the same system. NFS, like many other protocols, builds on the Open Network Computing Remote Procedure Call (ONC RPC) system. 


    Feature & Benefits of NFS Server 

    • NFS server allows to mount locally from  remote server files.
    • Fast and reliable services 
    •  NFS can be configure as centralized storage .
    • Users get their data irrespective of physical location.
    • File sync real time . 
    • Can be secured with Firewalls.

    NFS Configuration files 
    /etc/exports  : This is main configuration file which contain information of all shared Directory .

    /etc/fstab : This is not NFS configuration but we use this file to mount share permanently. 

    Before you begin, make sure you have the following:


    1. A Red Hat 8 or CentOS 8 server machine.

    2. A client machine running a compatible operating system (e.g., Red Hat, CentOS, or any other Linux distribution).

    3. Root or sudo access to both the server and client machines.

    Server IP - 192.168.1.50

    Client IP - 192.168.1.100


    Step 1: Install NFS Server

    On the server machine (Red Hat 8 or CentOS 8), you need to install the NFS server software. You can do this using the following command:

    Note: You must use root login to perform this task else you can use sudo before commands


    #dnf install nfs-utils

    This command will install the necessary NFS packages.


    Step 2: Configure the NFS Export

    Next, you need to define which directories you want to share over NFS. Edit the NFS exports file by opening it in a text editor:

    #vi /etc/exports 


    In the exports file, add a line for each directory you want to share. The line should follow this format:

    /path/to/shared/directory client_ip(options)


    For example, if you want to share the directory `/data` with a client at IP address `192.168.1.100` with read-write access, add the following line:

    /data 192.168.1.100(rw,sync,no_root_squash,no_all_squash)

    Save and close the file.


    Step 3: Export the NFS Share

    After configuring the NFS exports, you need to export the shares and restart the NFS server:


    #sudo exportfs -a

    #sudo systemctl restart nfs-server


    Step 4: Configure Firewall Rules

    To allow NFS traffic through the firewall, you need to open the necessary ports:

    #firewall-cmd --permanent --zone=public --add-service=nfs

    #firewall-cmd --permanent --zone=public --add-service=mountd

    #firewall-cmd --permanent --zone=public --add-service=rpc-bind

    #firewall-cmd --reload 



    Step 5: Install NFS Client Utilities (On the Client Machine)

    On the client machine, you need to install the NFS client utilities if they are not already installed:


    #dnf install nfs-utils


    Step 6: Mount the NFS Share (On the Client Machine)

    Now, you can mount the NFS share from the server to the client. Create a directory on the client to serve as the mount point:

    #mkdir /mnt/nfs-share


    Then, mount the NFS share:  

     You can mount the NFS share temporarily or add an entry to `/etc/fstab` for automatic mounting at boot. Here are both options:

    To see available NFS share on server 

    #showmount -e 192.168.1.50


       Temporary Mount:

         To temporarily mount the NFS share, use the following command. Replace `server_ip` with the IP address or hostname of your NFS server and `/data` with the shared directory path on the NFS server:


    #mount -t nfs server_ip:/path/to/shared/directory /mnt/nfs-share

    Replace `server_ip` with the IP address of your NFS server and `/path/to/shared/directory` with the directory you want to mount from the server.

    like: 

    #mount -t 192.168.1.50:/data  /mnt/nfs-share



    Permanent Mount (via /etc/fstab):


         To configure the NFS share to be mounted automatically at boot, you can add an entry to `/etc/fstab`. Edit the file using a text editor:


    #vi /etc/fstab

         

         Add the following line to the end of the file, replacing `server_ip:/data` with the NFS server and directory path, and `/mnt/nfs-share` with your desired mount point:


       

    192.168.1.50:/data /mnt/nfs-share nfs defaults 0 0

            

         Save the file and exit the text editor.


    Step 7:  Mount the NFS Share and Verify

       If you added the entry to `/etc/fstab`, you can mount all entries in `/etc/fstab` with the following command:

    #mount -a

       

    You can verify that the NFS share is mounted on the client by listing the contents of the mount point:


    ls /mnt/nfs-share


    If you see the files and directories from the NFS share, congratulations! You've successfully set up an NFS server on Red Hat 8 or CentOS 8 and mounted it on a client machine.


     Unmount the NFS Share** (if needed):

       To unmount the NFS share when you no longer need access to it, you can use the `umount` command:


    #umount /mnt/nfs-share


    Tip: Make sure you remove entry from /etc/fstab to unmount it permanently 


    8. **Access the NFS Share**:


       You can now access the NFS share as if it were a local directory. For example, you can use `cd /mnt/nfs-share` to navigate to the NFS share and manipulate files.


    #ls /mnt/nfs-share


    That's it! You've successfully set up an NFS client on your Red Hat 8 or CentOS 8 system to access an NFS share from an NFS server.


    Conclusion


    Setting up an NFS server on Red Hat 8 or CentOS 8 and mounting it on a client machine is a straightforward process. NFS provides a convenient way to share files and directories across a network, making it a valuable tool for networked Linux systems.

    No comments:

    Post a Comment

    Installing Jenkins on RHEL 8 is a straightforward process. Here's a step-by-step guide

    Introduction  Jenkins is an open-source automation server widely used for continuous integration (CI) and continuous delivery (CD) pipelines...