Β· Kalpa Madhushan Β· devops Β· 3 min read

Mastering NFS in Linux: Setup, Mounting, Management & Troubleshooting

Learn how to configure Network File System (NFS) in Linux environments, including server setup, client mounting, persistent configurations, and common troubleshooting techniques.

Learn how to configure Network File System (NFS) in Linux environments, including server setup, client mounting, persistent configurations, and common troubleshooting techniques.

πŸ”— Mastering NFS in Linux: Setup, Mounting, Management & Troubleshooting

NFS (Network File System) is a powerful way to share files between Linux systems over a network. This guide will walk you through:

  • How NFS works
  • Server-side configuration
  • Client-side mounting (including multiple clients)
  • Making mounts persistent
  • Useful commands and tools
  • Troubleshooting tips

🧠 What is NFS?

NFS allows a Linux server to share directories with other Linux machines (clients) over the network. Clients can mount these directories and access them like they are part of their own filesystem.


πŸ› οΈ Part 1: NFS Server Setup

Let’s assume your NFS server has the IP 192.168.1.100.

βœ… 1. Install NFS Server

sudo apt update
sudo apt install nfs-kernel-server

βœ… 2. Create a Shared Directory

sudo mkdir -p /srv/nfs/shared
sudo chown nobody:nogroup /srv/nfs/shared

βœ… 3. Configure /etc/exports

Open the exports file:

sudo nano /etc/exports

Add this line to share with all clients in subnet 192.168.1.0/24:

/srv/nfs/shared 192.168.1.0/24(rw,sync,no_subtree_check)

βœ… 4. Apply Export Changes

sudo exportfs -ra

βœ… 5. Start and Enable NFS Service

sudo systemctl restart nfs-kernel-server
sudo systemctl enable nfs-kernel-server

πŸ–₯️ Part 2: Mounting NFS on Clients (e.g., 192.168.1.101, 192.168.1.102)

βœ… 1. Install NFS Client

sudo apt install nfs-common

βœ… 2. Create Mount Point

sudo mkdir -p /mnt/shared

βœ… 3. Mount the NFS Share

sudo mount 192.168.1.100:/srv/nfs/shared /mnt/shared

Now, you can access /mnt/shared as if it’s a local folder.


πŸ” Mounting on Multiple Clients Simultaneously

Run the same installation + mount steps on each client machine (192.168.1.101, 192.168.1.102, etc.), or use a script like this:

#!/bin/bash
MOUNTPOINT="/mnt/shared"
SERVER="192.168.1.100:/srv/nfs/shared"

mkdir -p "$MOUNTPOINT"
mount "$SERVER" "$MOUNTPOINT"

You can deploy this with SSH, Ansible, or by login script.


πŸ“Œ Making NFS Mount Persistent (Fstab Method)

Add this line to /etc/fstab on the client machine:

192.168.1.100:/srv/nfs/shared  /mnt/shared  nfs  defaults  0  0

Then test:

sudo mount -a

🧾 Useful NFS Configuration Files

FilePurpose
/etc/exportsDefine NFS shares on server
/etc/fstabDefine persistent mounts on client
/var/lib/nfs/Stores internal NFS state
/etc/hosts.allow / .denyOptional host-based access control

πŸ› οΈ Essential Commands for NFS

CommandDescription
exportfs -raRe-export all shares
showmount -e 192.168.1.100View available exports from a server
mount -t nfs ...Manually mount an NFS share
umount /mnt/sharedUnmount an NFS share
`df -hgrep nfs`Show mounted NFS shares
findmnt -t nfsShow NFS mounts in tree format
systemctl status nfs-kernel-serverCheck NFS server status
sudo exportfs -vShow active exports and settings

🧯 NFS Troubleshooting Tips

ProblemSolution
❌ Permission deniedCheck /etc/exports and use correct rw options
❌ Stale file handle errorsUnmount and remount, or restart NFS on client
❌ Mount hangs or failsCheck firewall, NFS server status, nfs-common installed
❌ Not mounted at bootUse correct syntax in /etc/fstab, and use IP, not hostname
❌ No export shown on showmountRestart NFS server and recheck /etc/exports

βš™οΈ Optional: Autofs (On-Demand Mounting)

If you want shares to mount only when accessed, install autofs:

sudo apt install autofs

Then configure /etc/auto.master:

/mnt/nfs /etc/auto.nfs

Then create /etc/auto.nfs:

shared -rw,soft,intr 192.168.1.100:/srv/nfs/shared

Restart autofs:

sudo systemctl restart autofs

Now, accessing /mnt/nfs/shared will trigger automatic mount.


βœ… Summary

  • Use /etc/exports to define NFS shares.
  • Clients can mount shares manually or via /etc/fstab.
  • Tools like showmount, findmnt, and df help monitor NFS.
  • Automate and scale across multiple clients via scripts or Ansible.
  • Ensure correct subnet (192.168.1.0/24) permissions are in place.

🧩 Example Network Layout

HostnameRoleIP Address
nfs-serverNFS Server192.168.1.100
client01NFS Client192.168.1.101
client02NFS Client192.168.1.102

All clients mount:

192.168.1.100:/srv/nfs/shared β†’ /mnt/shared
Back to Blog

Related Posts

View All Posts Β»