Linux

How to Set Up NFS Share on Debian

Tags:
Share on:

Table of Contents

NSF (Network File System) is a protocol used for sharing files over a network. In Linux, NFS allows a system to share directories and files with others over a network.

Installing NFS Package

First, 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 will install the necessary packages required to set up an NFS share.

Create Directory for NFS Share

Next, 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 

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 

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)

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.

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.

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.

Mount 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.

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.