How to Setup Shareable Drive with NFS in Linux - Complete Guide

Learn how to configure Network File System (NFS) on Linux to create shareable drives across your network. Perfect for home servers and file sharing.

How to Setup Shareable Drive with NFS in Linux - Complete Guide

Network File System (NFS) is one of the most efficient ways to share files across Linux machines on your network. I run it on my N100 mini PC that hosts Jellyfin and handles backups for everything on my network. It lets me access media files, backups, and documents from any Linux machine.

This guide covers the basics through to advanced mounting options.

What is NFS and Why Use It?

Understanding NFS

Network File System (NFS) is a protocol that lets you access files over a network the same way you access local storage. It works well in Linux environments.

Key Benefits of NFS

  • Native Linux Support: Built into all major distributions
  • High Performance: Optimized for Unix-like systems with minimal overhead
  • Scalability: Handles multiple concurrent connections
  • Flexibility: Supports various mount options and security configurations
  • Cost-Effective: Open source with no licensing fees

NFS vs Other Protocols

ProtocolBest ForPerformanceSecurityComplexity
NFSLinux-only environmentsHighModerateLow
SMB/CIFSMixed Windows/LinuxModerateHighModerate
FTPFile transfersLowVariableLow
SSH/SFTPSecure transfersModerateHighHigh

Prerequisites and Planning

Before starting, make sure you have:

  • Two or more Linux machines (server and clients)
  • Root or sudo access on all machines
  • Network connectivity between devices
  • Static IP addresses (this saves headaches later)
  • Firewall configuration knowledge

Network Architecture Overview

NFS Network

Step 1: Setting Up the NFS Server

Install NFS Server Package

Update your system and install the NFS kernel server:

sudo apt update && sudo apt install nfs-kernel-server -y

For CentOS/RHEL systems:

sudo yum install nfs-utils -y
# or for newer versions
sudo dnf install nfs-utils -y

Create the Shared Directory

Set up a directory structure for your NFS shares. I organize by purpose:

# Create main NFS directory
sudo mkdir -p /srv/nfs

# Create specific share directories
sudo mkdir -p /srv/nfs/media
sudo mkdir -p /srv/nfs/backups
sudo mkdir -p /srv/nfs/documents

Directory Location

Using /srv/nfs follows Linux filesystem standards. Don’t use /media or /mnt for NFS exports since these are reserved for temporary mounts.

Configure Permissions

Set ownership and permissions:

# Set ownership to nobody:nogroup for security
sudo chown -R nobody:nogroup /srv/nfs

# Set permissions (755 for directories, 644 for files)
sudo chmod -R 755 /srv/nfs

Configure NFS Exports

The /etc/exports file defines which directories are shared and with what permissions:

sudo nano /etc/exports

Add your export configurations:

# Basic configuration for local network
/srv/nfs/media    192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash)
/srv/nfs/backups  192.168.1.0/24(rw,sync,no_subtree_check,root_squash)
/srv/nfs/documents 192.168.1.101(rw,sync,no_subtree_check) 192.168.1.102(ro,sync,no_subtree_check)

Export Options Explained

OptionDescriptionUse Case
rwRead-write accessFull access for trusted clients
roRead-only accessSharing read-only content
syncSynchronous writesData integrity (recommended)
asyncAsynchronous writesBetter performance, less safe
no_subtree_checkDisable subtree checkingImproved reliability
root_squashMap root to anonymous userSecurity (default)
no_root_squashAllow root accessAdministrative access
all_squashMap all users to anonymousMaximum security

Apply Export Configuration

After configuring exports, apply the changes:

# Export the file systems
sudo exportfs -a

# Restart NFS services
sudo systemctl restart nfs-kernel-server
sudo systemctl enable nfs-kernel-server

Configure Firewall

Allow NFS traffic through the firewall:

# For UFW (Ubuntu)
sudo ufw allow from 192.168.1.0/24 to any port nfs

# For firewalld (CentOS/RHEL)
sudo firewall-cmd --permanent --add-service=nfs
sudo firewall-cmd --reload

Step 2: Setting Up NFS Clients

Install NFS Client Package

On client machines, install the NFS utilities:

sudo apt update && sudo apt install nfs-common -y

Create Mount Points

Create directories where NFS shares will be mounted:

# Create mount points
sudo mkdir -p /mnt/server-media
sudo mkdir -p /mnt/server-backups
sudo mkdir -p /mnt/server-docs

Test Manual Mounting

Before setting up automatic mounting, test the connection:

# Mount the media share
sudo mount -t nfs 192.168.1.100:/srv/nfs/media /mnt/server-media

# Verify the mount
df -h | grep nfs
ls -la /mnt/server-media

Verification

If the NFS mount appears in df -h output and you can access files in the mounted directory, the basic setup works.

Configure Permanent Mounting

Edit /etc/fstab for automatic mounting on boot:

sudo nano /etc/fstab

Add entries for your NFS shares:

# NFS mounts
192.168.1.100:/srv/nfs/media    /mnt/server-media    nfs    defaults,_netdev    0    0
192.168.1.100:/srv/nfs/backups  /mnt/server-backups  nfs    defaults,_netdev    0    0
192.168.1.100:/srv/nfs/documents /mnt/server-docs    nfs    defaults,_netdev,ro 0    0

Mount Options for fstab

OptionDescriptionBenefit
_netdevNetwork deviceWaits for network before mounting
softSoft mountReturns error if server unavailable
hardHard mountRetries indefinitely (default)
intrInterruptibleAllows process interruption
rsize=8192Read buffer sizeOptimizes read performance
wsize=8192Write buffer sizeOptimizes write performance

Test the fstab configuration:

# Test mounting all fstab entries
sudo mount -a

# Verify mounts
mount | grep nfs

Step 3: Advanced Configuration Options

Using AutoFS for On-Demand Mounting

AutoFS handles automatic mounting and unmounting, which helps when the NFS server isn’t always available.

Install AutoFS:

sudo apt install autofs -y

Configure the master map:

sudo nano /etc/auto.master

Add this line:

/mnt/auto /etc/auto.nfs --timeout=60 --ghost

Create the AutoFS map file:

sudo nano /etc/auto.nfs

Configure your auto-mounts:

media     -fstype=nfs4,rw,soft,intr  192.168.1.100:/srv/nfs/media
backups   -fstype=nfs4,rw,soft,intr  192.168.1.100:/srv/nfs/backups
documents -fstype=nfs4,ro,soft,intr  192.168.1.100:/srv/nfs/documents

Start and enable AutoFS:

sudo systemctl restart autofs
sudo systemctl enable autofs

Performance Optimization

For better performance with large files or heavy traffic:

# Add to /etc/fstab for optimized performance
192.168.1.100:/srv/nfs/media /mnt/server-media nfs rsize=32768,wsize=32768,hard,intr,_netdev 0 0

Security Enhancements

Using NFSv4 with Kerberos

For better security in production environments:

# On server: Install Kerberos
sudo apt install krb5-kdc krb5-admin-server -y

# Configure NFSv4 security
echo "Domain = your-domain.com" | sudo tee -a /etc/idmapd.conf

Restricting Access by IP Range

Modify /etc/exports for tighter security:

# More restrictive access
/srv/nfs/media 192.168.1.100/32(rw,sync,no_subtree_check) 192.168.1.101/32(ro,sync,no_subtree_check)

Step 4: Monitoring and Maintenance

Checking NFS Status

Monitor your NFS setup:

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

# View active exports
sudo exportfs -v

# Show NFS statistics
nfsstat -s  # Server stats
nfsstat -c  # Client stats

# Monitor active connections
sudo ss -tuln | grep :2049

Log Analysis

NFS logs are typically found in:

# Check NFS logs
sudo journalctl -u nfs-kernel-server
sudo tail -f /var/log/syslog | grep nfs

Performance Monitoring

Create a simple monitoring script:

#!/bin/bash
# nfs-monitor.sh

echo "=== NFS Performance Monitor ==="
echo "Date: $(date)"
echo

echo "Active NFS Mounts:"
df -h | grep nfs
echo

echo "NFS Server Statistics:"
nfsstat -s | head -10
echo

echo "Network Connections:"
sudo ss -tuln | grep :2049

Troubleshooting Common Issues

Connection Problems

Common Error: Connection Refused

This usually means firewall issues or NFS services aren’t running.

Solution checklist:

  • Verify NFS services are running: sudo systemctl status nfs-kernel-server
  • Check firewall rules: ports 2049, 111, and related ports should be open
  • Test network connectivity: ping between server and client
  • Verify exports: sudo exportfs -v on server

Permission Issues

# Fix ownership issues
sudo chown -R nobody:nogroup /srv/nfs

# Check export permissions
sudo exportfs -v | grep your-share

Performance Issues

For slow NFS performance:

  1. Adjust buffer sizes:

    # In /etc/fstab
    rsize=32768,wsize=32768
  2. Use async for non-critical data:

    # In /etc/exports
    /srv/nfs/temp *(rw,async,no_subtree_check)
  3. Monitor network utilization:

    iftop -i eth0

Integration with Home Server Setup

NFS works well with other home server technologies. Here’s how I use it:

Media Server Integration

For Jellyfin and other media servers:

# Media directory structure
/srv/nfs/media/
├── movies/
├── tv-shows/
├── music/
└── photos/

This structure lets the N100 mini PC serve media files efficiently across the network.

Backup Strategy

Combined with the LVM setup from my previous article:

# Backup scripts can now target NFS shares
rsync -av /home/user/documents/ /mnt/server-backups/user-docs/

Container Integration

When running Docker containers:

# docker-compose.yml
version: '3'
services:
  jellyfin:
    image: jellyfin/jellyfin
    volumes:
      - /mnt/server-media:/media:ro
      - ./config:/config

Best Practices and Security Considerations

Security Best Practices

  • Use NFSv4 when possible for better security
  • Set firewall rules to restrict access to trusted networks
  • Keep both server and clients updated
  • Check access logs for suspicious activity
  • Use root_squash by default unless you need otherwise

Backup Considerations

Backup Strategy

NFS shares should be part of your backup strategy. Back up the shared data on the server side to prevent data loss.

Network Considerations

  • Use wired connections when possible for better performance
  • Set up network monitoring as discussed in server monitoring
  • Consider network segmentation for security

Conclusion

NFS on Linux gives you solid file sharing across your network. I run it on my N100 mini PC alongside Jellyfin, and it handles everything I throw at it.

Start with a basic setup. Get that working first, then add AutoFS and performance tuning as you need them.

For more on home servers and Linux, see my articles on best mini PCs for home servers and Docker containers for home servers.

Start Building Your NFS Setup