NFS (Network File System) is a distributed file system that allows clients to access files and directories on remote servers over a network. NFS is commonly used in environments with many systems, where it is important to share resources between different servers. In this article, we will discuss how to set up an NFS share on Debian.

Prerequisites

Before we start setting up the NFS share, we need to ensure that the following prerequisites are met:

  • Two or more Debian systems (one as a server and one or more as clients).
  • The server and client systems should have a static IP address.
  • Ensure that the server and client systems can communicate with each other using the ping command.
  • Install the NFS package on the server system.

Step 1: Install the NFS Package

First, we need to install the NFS package on the server system. Open the terminal and run the following command:

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

This command installs the necessary packages required to set up an NFS share.

Step 2: Create a Directory for NFS Share

Next, we need to create a directory that we want to share with the client systems. We can create this directory anywhere on the server system, but it is recommended to create it in the root directory for easier management.

For example, we will create a directory named nfs_share in the root directory. Run the following command to create this directory:

sudo mkdir /nfs_share 

Step 3: Set Permissions for NFS Share

After creating the directory, we need to set the proper permissions for the directory. We will set the ownership of the directory to the nobody user and nogroup group, which is the default user and group for NFS shares. We will also set the read-write-execute permissions for the directory.

Run the following command to set the permissions:

sudo chown nobody:nogroup /nfs_share 
sudo chmod 777 /nfs_share 

Step 4: Configure the NFS Share

Next, we need to configure the NFS share in the /etc/exports file. This file contains the configuration settings for the NFS shares on the server system.

Open the /etc/exports file using the following command:

sudo nano /etc/exports 

Add the following line to the end of the file:

/nfs_share 192.168.1.10(rw,sync,no_subtree_check)

Replace “192.168.1.5” with the IP address of the client system that you want to access the NFS share.

This configuration line allows the client system to read and write to the NFS share, synchronizes the data between the client and server systems, and verifies the NFS share for errors.

Step 5: Export the NFS Share

After configuring the NFS share, we need to export the share to make it accessible to the client system. Run the following command to export the NFS share:

sudo exportfs -a 

This command exports the NFS share that we have configured in the “/etc/exports” file.

Step 6: Enable and Start the NFS Service

Finally, we need to enable and start the NFS service on the server system using the following commands:

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

These commands enable and start the NFS service on the server system.

Step 7: Mount the NFS Share on the Client Machine

Once we have completed the configuration on the server system, we can access the NFS share from the client system.

  • Create a directory where you want to mount the shared directory. For example, we can create a directory called nfs_share in the /mnt directory.
    sudo mkdir /mnt/nfs_share 
    
  • Mount the shared directory using the following command:
    sudo mount -t nfs 192.168.1.100:/nfs_share /mnt/nfs_share 
    

    This command will mount the nfs_share directory on the server with the IP address 192.168.1.100 to the nfs_share directory that you created in the /mnt directory on the client machine.

  • Verify that the shared directory is mounted successfully using the mount command:
    mount | grep nfs_share 
    

    You should see an output similar to this:

    192.168.1.100:/nfs_share on /mnt/nfs_share type nfs (rw,relatime,vers=3,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=192.168.1.100,mountvers=3,mountport=20048,mountproto=tcp,local_lock=none,addr=192.168.1.100)
    

    This output confirms that the shared directory is mounted successfully on the client machine.

Step 8: Configure the NFS Share to Auto-Mount at Boot Time

To ensure that the NFS share is always available on the client machine, you can configure the system to automatically mount the share at boot time. Follow these steps:

  • Edit the /etc/fstab file:
    sudo nano /etc/fstab 
    
  • Add the following line at the end of the file:

    192.168.1.100:/nfs_share /mnt/nfs_share nfs defaults 0 0

    This line specifies the mount point, file system type (nfs), mount options (defaults), dump (0), and file system check (0).

  • Save and close the file.

To test the configuration, reboot the client machine and verify that the NFS share is mounted automatically using the mount command.

Conclusion

In this tutorial, you learned how to set up an NFS share on Debian. You can now easily share files between multiple Linux machines using NFS. Remember to ensure that your firewall rules are correctly configured to allow NFS traffic, and that you set the appropriate permissions on the shared directories to ensure the security of your system.