How to Mount NFS Shares on Linux

July 8, 2024 Linux

Network File System (NFS) is a distributed file system protocol that enables you to share remote directories over a network. With NFS, you can mount remote directories on your system and interact with the remote files as if they were local. This powerful feature enhances file sharing and collaboration in networked environments.

In this comprehensive tutorial, we’ll walk you through the process of mounting NFS shares on Linux systems, both manually and automatically. We’ll cover everything from installing the necessary packages to troubleshooting common issues.

Installing NFS Client Packages

Before you can mount an NFS share, you need to install the NFS client package on your Linux system. The package name varies depending on your Linux distribution.

For Ubuntu and Debian-based systems:

sudo apt update
sudo apt install nfs-common

For CentOS and Fedora-based systems:

sudo yum install nfs-utils

Ensure you have the appropriate permissions to install packages on your system.

Manually Mounting NFS File Systems

Mounting a remote NFS share is similar to mounting regular file systems. The general syntax for the mount command is:

mount [OPTION...] NFS_SERVER:EXPORTED_DIRECTORY MOUNT_POINT

Follow these steps to manually mount a remote NFS share:

  1. Create a directory to serve as the mount point:
   sudo mkdir /var/nfs_data

This directory will be where the NFS share is attached in your local file system.

  1. Mount the NFS share using the following command:
   sudo mount -t nfs 192.168.1.100:/shared_data /var/nfs_data

Replace 192.168.1.100 with the IP address of your NFS server, /shared_data with the exported directory on the server, and /var/nfs_data with your local mount point.

  1. If successful, the command will not produce any output. You can verify the mount by using either the mount command or df -h.
  2. For additional mount options, use the -o flag. For example:
   sudo mount -t nfs -o rw,sync 192.168.1.100:/shared_data /var/nfs_data

This mounts the share with read-write permissions and synchronous updates.

Note that manually mounted NFS shares do not persist after a system reboot.

Automatically Mounting NFS File Systems with /etc/fstab

For a more permanent solution, you can configure your system to automatically mount NFS shares at boot time using the /etc/fstab file.

Follow these steps to set up automatic mounting:

  1. Create the mount point if it doesn’t already exist:
   sudo mkdir /var/nfs_data
  1. Open the /etc/fstab file with your preferred text editor:
   sudo nano /etc/fstab
  1. Add the following line to the file:
   192.168.1.100:/shared_data /var/nfs_data nfs defaults 0 0

Adjust the server IP, exported directory, and mount point as needed.

  1. Save the file and exit the text editor.
  2. To test the new fstab entry without rebooting, run:
   sudo mount -a

This command mounts all filesystems listed in /etc/fstab.

  1. Verify the mount using df -h or the mount command.

With this configuration, the NFS share will be automatically mounted each time your system boots.

Unmounting NFS File Systems

To detach a mounted NFS share, use the umount command followed by either the mount point or the remote share:

sudo umount /var/nfs_data

or

sudo umount 192.168.1.100:/shared_data

If the NFS mount has an entry in the /etc/fstab file and you want to permanently remove it, edit the file and delete the corresponding line.

Troubleshooting NFS Mounts

If you encounter issues while mounting NFS shares, try these troubleshooting steps:

  1. Ensure the NFS server is running and the share is exported correctly.
  2. Check network connectivity between your client and the NFS server.
  3. Verify that any firewalls are configured to allow NFS traffic (typically on ports 111 and 2049).
  4. Use the showmount -e SERVER_IP command to list available exports on the server.
  5. Check system logs (/var/log/syslog or /var/log/messages) for any error messages related to NFS.

Remember, NFS security is primarily based on IP addresses and hostnames, so ensure your client’s IP is allowed to access the NFS share on the server.

By following this guide, you should now be able to successfully mount NFS shares on your Linux system, both manually and automatically. NFS provides a powerful way to share files across a network, enhancing collaboration and resource utilization in Linux environments.