Class 45 of 55 • Linux Networking & Servers

Linux NFS (Network File System) Server Setup & Client Mounting Tutorial

A complete step-by-step masterclass on configuring an enterprise NFS kernel server, mastering /etc/exports access parameters, and mounting remote filesystems persistently via /etc/fstab.

The Network File System (NFS) is a distributed file system protocol originally developed by Sun Microsystems in 1984. It allows client computers on a network to mount remote directory shares exported by a central server and interact with files over the network as if they resided on the client's local physical disk drives.

In enterprise Linux deployments, cloud clusters, and Kubernetes architectures, NFS serves as the standard shared storage backbone for web server farms (Nginx/Apache sharing uploaded media), centralized user home directories, database backup dumps, and persistent volume claims (PVCs).

1. NFS Architecture & Protocol Versions

NFS operates via the client-server model over the standard TCP/IP transport layer:

  • NFS Server: Exports local disk directories and enforces granular read/write access controls.
  • NFS Client: Mounts exported directories into its local Virtual File System (VFS) namespace using standard POSIX system calls (open(), read(), write()).

NFSv3 vs Modern NFSv4

Feature Legacy NFSv3 Modern NFSv4
Connection State Stateless (Server holds no client lock records). Stateful (Tracks open file states and client leases).
Network Ports Multiple dynamic ports (Requires rpcbind, mountd, statd). Difficult for firewalls. Single well-known TCP port 2049. Highly firewall friendly.
Security & Auth Relies purely on client IP and standard numeric UIDs/GIDs (spoofable). Native RPCSEC_GSS Kerberos authentication, ACLs, and string-based usernames.
Namespace Export Exports isolated directory paths independently. Unified pseudo-filesystem hierarchy anchored at a root export.

2. Step-by-Step: Setting Up the NFS Server

Step 2.1: Install Server Packages

# On Ubuntu / Debian systems:
sudo apt update
sudo apt install nfs-kernel-server -y

# On RHEL / Rocky Linux / AlmaLinux / Fedora:
sudo dnf install nfs-utils -y

Step 2.2: Create and Secure the Shared Directory

# Create directory to share
sudo mkdir -p /srv/nfs/shared_data

# Set ownership and permissions (give full read/write access to test client)
sudo chown nobody:nogroup /srv/nfs/shared_data   # Ubuntu/Debian
# sudo chown nfsnobody:nfsnobody /srv/nfs/shared_data # RHEL/CentOS
sudo chmod 777 /srv/nfs/shared_data

Step 2.3: Configure Exports in /etc/exports

The /etc/exports file controls which directories are shared and with whom. Open the file in an editor:

sudo nano /etc/exports

Add the following export rule:

# Format: <Directory> <Authorized-Host/Subnet>(<Options>)
/srv/nfs/shared_data  192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash)

Deep Dive into Export Parameters:

  • rw: Grants Read-Write access (use ro for read-only).
  • sync: Forces the NFS server to commit write operations to physical disk storage before acknowledging the client request. Guarantees data integrity during server power loss. (The faster alternative async acknowledges immediately before disk flushing, risking data corruption).
  • no_subtree_check: Prevents the server from verifying if a file requested by the client still resides in the exported subdirectory tree. Significantly improves performance and prevents errors when files are renamed by active processes.
  • root_squash (Default): Maps client requests executed by root (UID 0) to unprivileged user nobody, preventing client roots from compromising server system files.
  • no_root_squash: Allows client root users to create and edit files as true root on the server share (mandatory for diskless booting and root backup systems).

Step 2.4: Export the Shares & Start Services

# Apply /etc/exports changes without restarting daemon
sudo exportfs -ra

# Verify active exported directories
sudo exportfs -v

# Enable and start NFS server service
sudo systemctl enable --now nfs-kernel-server   # Ubuntu/Debian
# sudo systemctl enable --now nfs-server        # RHEL/Rocky

Step 2.5: Configure Firewall Rules

# Ubuntu UFW: Allow traffic from local private subnet
sudo ufw allow from 192.168.1.0/24 to any port nfs
sudo ufw status

# RHEL Firewalld: Allow nfs, mountd, and rpcbind services
sudo firewall-cmd --permanent --add-service=nfs
sudo firewall-cmd --permanent --add-service=mountd
sudo firewall-cmd --permanent --add-service=rpc-bind
sudo firewall-cmd --reload

3. Step-by-Step: Configuring the NFS Client

Step 3.1: Install Client Utilities

# On Ubuntu / Debian client:
sudo apt update && sudo apt install nfs-common -y

# On RHEL / Rocky Linux client:
sudo dnf install nfs-utils -y

Step 3.2: Discover Remote Shares with showmount

# Query available shares on the NFS server (e.g. 192.168.1.100)
showmount -e 192.168.1.100

# Expected output:
# Export list for 192.168.1.100:
# /srv/nfs/shared_data 192.168.1.0/24

Step 3.3: Manual Mounting

# Create local mount directory on client
sudo mkdir -p /mnt/nfs_share

# Mount the remote NFS share
sudo mount -t nfs 192.168.1.100:/srv/nfs/shared_data /mnt/nfs_share

# Verify the mount is active and check disk space
df -hT /mnt/nfs_share

4. Persistent Auto-Mounting via /etc/fstab

Manual mounts using mount vanish when the client machine reboots. To make mounts persistent, append an entry to /etc/fstab on the client:

sudo nano /etc/fstab

Add the following configuration line at the bottom:

192.168.1.100:/srv/nfs/shared_data  /mnt/nfs_share  nfs  defaults,_netdev,nofail,timeo=14,intr  0  0

Key Fstab Mount Flags Explained:

  • _netdev: Critical! Informs systemd to delay mounting until the physical network stack and IP addresses are completely up. Without this, system boot hangs indefinitely trying to reach an unavailable IP.
  • nofail: Prevents boot failure if the NFS server is powered down or unreachable during client boot.
  • timeo=14: Sets RPC timeout to 1.4 seconds before attempting retransmission.
  • intr: Allows users to cancel or kill hung file operations if the server crashes.
# Test fstab configuration without rebooting
sudo mount -a

5. Practical Terminal Lab: Verifying NFS Sync

# 1. On Client: Create a test file inside the NFS mount
echo "Hello from Client machine" > /mnt/nfs_share/client_test.txt

# 2. On Server: Check if the file instantly appeared on disk
cat /srv/nfs/shared_data/client_test.txt
# Output: Hello from Client machine

# 3. On Client: Safely unmount when testing is finished
sudo umount /mnt/nfs_share

6. Student Viva & Exam Practice Q&A

The exportfs -ra command re-reads /etc/exports and re-exports all listed directories. The -r flag re-exports all directories, and -a specifies that all entries in the exports configuration should be synchronized into the kernel's live export table without terminating existing client connections.
Under sync, the NFS server writes incoming data to the physical disk (or non-volatile journal) before sending an acknowledgment back to the client. Under async, the server replies as soon as data enters RAM buffers. If the server loses power or kernel panics while holding data in RAM, uncommitted client data is permanently lost and files become corrupted.
If an NFS server goes offline, standard umount calls freeze waiting for the server. You can unmount forcefully using: sudo umount -f -l /mnt/nfs_share. The -f forces the unmount and -l (lazy unmount) detaches the filesystem immediately from the VFS tree while cleaning up remaining socket references in the background.

7. Frequently Asked Questions (FAQ)

Yes. Windows 10/11 Pro and Enterprise editions include an optional feature called "Client for NFS" that allows mounting NFS shares via the command: mount \\nfs-server-ip\srv\nfs\share Z:. However, for native Windows-to-Linux file sharing with file locking and Active Directory integration, Samba (SMB/CIFS) is the industry standard.
NFSv3 transmits raw numeric UIDs and GIDs over the wire. If user 'alice' is UID 1001 on the client, but UID 1001 on the server belongs to user 'bob', Bob will have complete ownership of Alice's files on the server! In modern environments, this is solved by centralizing identities with LDAP / Active Directory or using NFSv4 with idmapd (ID mapping daemon).
You can check connected clients using ss -tan | grep :2049, or inspect the kernel's active NFS client list by running: cat /proc/fs/nfs/exports or nfsstat -s for comprehensive server I/O statistics.
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!