Guide Intermediate 12 min read

NFS Setup: Network File Sharing on Linux

Complete guide to NFS server setup on Linux. Share directories across your network, configure exports, mount shares, and set up NFS root for embedded systems.

OceanSoft Solutions
nfslinuxnetworkingembeddedsysadmin
nfs@server:~

What is NFS?

NFS (Network File System) allows you to share directories over a network. Clients can mount remote directories as if they were local filesystems. Common use cases:

  • Development — Share code between host and target systems
  • Embedded systems — Boot from NFS root during development
  • Cluster computing — Share home directories across nodes
  • Backup systems — Centralized storage access

Server Installation

Ubuntu/Debian

# Install NFS server
sudo apt update
sudo apt install nfs-kernel-server

# Check service status
sudo systemctl status nfs-kernel-server

RHEL/CentOS/Fedora

# Install NFS utilities
sudo dnf install nfs-utils

# Enable and start services
sudo systemctl enable --now nfs-server rpcbind

Configuring Exports

The /etc/exports file defines which directories to share and with what permissions.

# Edit exports file
sudo nano /etc/exports

Basic Export Syntax

# Format: /path/to/share    client(options)

# Share with a single host
/home/shared    192.168.1.100(rw,sync,no_subtree_check)

# Share with entire subnet
/home/shared    192.168.1.0/24(rw,sync,no_subtree_check)

# Share with any client
/home/public    *(ro,sync,no_subtree_check)

# Multiple clients with different permissions
/srv/data       192.168.1.100(rw) 192.168.1.101(ro)

Common Export Options

Option Description
rw Read-write access
ro Read-only access
sync Write changes immediately (safer)
async Buffer writes (faster, less safe)
no_subtree_check Disable subtree checking (recommended)
no_root_squash Allow root access from clients
root_squash Map root to nobody (default, more secure)
all_squash Map all users to anonymous
anonuid=1000 Anonymous user UID
anongid=1000 Anonymous group GID

Apply Export Changes

# Apply new exports
sudo exportfs -ra

# Show current exports
sudo exportfs -v

# Show exports with verbose info
sudo exportfs -s

Client Setup

Install NFS Client

# Ubuntu/Debian
sudo apt install nfs-common

# RHEL/CentOS
sudo dnf install nfs-utils

Mount NFS Share

# Create mount point
sudo mkdir -p /mnt/nfs-share

# Mount the share
sudo mount -t nfs 192.168.1.50:/home/shared /mnt/nfs-share

# Mount with specific options
sudo mount -t nfs -o rw,soft,timeo=50 192.168.1.50:/home/shared /mnt/nfs-share

# Verify mount
df -h | grep nfs
mount | grep nfs

Persistent Mount (fstab)

# Add to /etc/fstab for automatic mount at boot
192.168.1.50:/home/shared  /mnt/nfs-share  nfs  defaults,_netdev  0  0

# Mount all entries in fstab
sudo mount -a

NFS Root for Embedded Development

During embedded Linux development, booting from NFS root allows quick iteration without reflashing.

Server Configuration

# Export root filesystem
# /etc/exports
/srv/nfs/rootfs    192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash)

# Apply exports
sudo exportfs -ra

Prepare Root Filesystem

# Extract your root filesystem to NFS directory
sudo mkdir -p /srv/nfs/rootfs
sudo tar -xf rootfs.tar.gz -C /srv/nfs/rootfs

# Ensure proper permissions
sudo chown -R root:root /srv/nfs/rootfs

U-Boot Configuration

# Set boot arguments for NFS root
setenv bootargs console=ttyS0,115200 root=/dev/nfs nfsroot=192.168.1.50:/srv/nfs/rootfs,nfsvers=3,tcp rw ip=dhcp

# Or with static IP
setenv bootargs console=ttyS0,115200 root=/dev/nfs nfsroot=192.168.1.50:/srv/nfs/rootfs,nfsvers=3,tcp rw ip=192.168.1.100::192.168.1.1:255.255.255.0::eth0:off

The ip parameter format: ip=client_ip::gateway:netmask::interface:off

Kernel Configuration

Ensure your kernel has these options enabled:

CONFIG_ROOT_NFS=y
CONFIG_NFS_V3=y
CONFIG_NFS_V4=y
CONFIG_IP_PNP=y
CONFIG_IP_PNP_DHCP=y

NFSv4 vs NFSv3

Feature NFSv3 NFSv4
Single port No (multiple) Yes (2049)
Firewall friendly No Yes
Security Basic Kerberos, ACLs
Performance Good Better
# Force specific NFS version when mounting
sudo mount -t nfs -o vers=3 server:/share /mnt/nfs
sudo mount -t nfs -o vers=4 server:/share /mnt/nfs

# Check which version is being used
nfsstat -m

Firewall Configuration

NFSv4 (Simple - Single Port)

# UFW
sudo ufw allow 2049/tcp

# firewalld
sudo firewall-cmd --permanent --add-service=nfs
sudo firewall-cmd --reload

NFSv3 (Multiple Ports)

# Required ports for NFSv3
sudo ufw allow 111/tcp   # rpcbind
sudo ufw allow 111/udp
sudo ufw allow 2049/tcp  # nfs
sudo ufw allow 2049/udp
sudo ufw allow 32765:32768/tcp  # mountd, lockd, etc.
sudo ufw allow 32765:32768/udp

Troubleshooting

# Check NFS server status
sudo systemctl status nfs-kernel-server

# View exported shares
sudo exportfs -v

# Check RPC services
rpcinfo -p localhost

# Test mounting from server itself
sudo mount -t nfs localhost:/home/shared /mnt/test

# Check client-side mount issues
dmesg | grep -i nfs

# Show NFS statistics
nfsstat

# Debug mount with verbose
sudo mount -t nfs -o vers=3,soft,timeo=5 -v server:/share /mnt/nfs

Common Issues

Issue Solution
Permission denied Check exports, use no_root_squash for root access
Mount hangs Check firewall, use soft mount option
Stale file handle Unmount and remount the share
Connection refused Verify NFS server is running, check IP in exports
Version mismatch Specify version with vers=3 or vers=4

Security Best Practices

  • Use root_squash to prevent root access from clients
  • Limit exports to specific IP addresses or subnets
  • Use NFSv4 with Kerberos for authentication
  • Keep exports to a minimum (least privilege)
  • Use sync option to prevent data loss
# Secure export example
/srv/data    192.168.1.0/24(rw,sync,no_subtree_check,root_squash,sec=krb5p)

Resources

Need help with network infrastructure? Contact us for consulting.