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

Setting up a Network File System (NFS) is one of the most efficient ways to share files across Linux machines in your network. Whether you’re running a home server setup or managing multiple Linux systems, NFS provides a robust solution for centralized file storage and sharing.

In my home lab setup, I use an N100 Intel mini PC that hosts Jellyfin and handles file backups for all devices in my network. NFS has been instrumental in creating a seamless file-sharing environment that allows me to access media files, backups, and documents from any Linux machine on my network.

This comprehensive guide will walk you through setting up NFS on Linux, from basic configuration to advanced mounting options.

What is NFS and Why Use It?

Understanding NFS

Network File System (NFS) is a distributed file system protocol that enables users to access files over a computer network similar to how local storage is accessed. It’s particularly well-suited for Linux environments.

Key Benefits of NFS

  • Native Linux Support: Built-in support across all major Linux distributions
  • High Performance: Optimized for Unix-like systems with minimal overhead
  • Scalability: Handles multiple concurrent connections efficiently
  • Flexibility: Supports various mount options and security configurations
  • Cost-Effective: Open-source solution 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 diving into the configuration, ensure you have:

  • Two or more Linux machines (server and client(s))
  • Root or sudo access on all machines
  • Network connectivity between all devices
  • Static IP addresses (recommended for stability)
  • Firewall configuration knowledge

Network Architecture Overview

NFS Network

Step 1: Setting Up the NFS Server

Install NFS Server Package

Begin by updating your system and installing 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

Establish a directory structure for your NFS shares. I recommend organizing shares 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 hierarchy standards. Avoid using /media or /mnt for NFS exports as these are typically reserved for temporary mounts.

Configure Permissions

Set appropriate ownership and permissions for the shared directories:

# 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

Establish 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 configuring 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 you can see the NFS mount in the df -h output and access files in the mounted directory, the basic setup is working correctly.

Configure Permanent Mounting

Edit the /etc/fstab file 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 provides automatic mounting and unmounting of NFS shares, which is particularly useful 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, especially with large files or high-traffic scenarios:

# 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 enhanced 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 with these commands:

# 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 indicates firewall issues or NFS services not running properly.

Solution checklist:

  • Verify NFS services are running: sudo systemctl status nfs-kernel-server
  • Check firewall rules: Ensure ports 2049, 111, and others are 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

As mentioned in my home server articles, NFS pairs excellently with other home server technologies. Here’s how I integrate NFS in my setup:

Media Server Integration

For Jellyfin and other media servers:

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

This structure allows my N100 mini PC to serve media files efficiently to all devices on 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
  • Implement proper firewall rules restricting access to trusted networks
  • Regular security updates on both server and clients
  • Monitor access logs for suspicious activity
  • Use root_squash by default unless specifically needed

Backup Considerations

Backup Strategy

Remember that NFS shares should be included in your backup strategy. The shared data should be backed up on the server side to prevent data loss.

Network Considerations

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

Conclusion

Setting up NFS in Linux provides a robust, high-performance solution for file sharing across your network. Whether you’re building a home server setup like my N100 Intel mini PC configuration or managing a small office network, NFS offers the flexibility and performance needed for modern file sharing requirements.

The key to a successful NFS implementation lies in proper planning, security configuration, and regular maintenance. Start with a basic setup and gradually implement advanced features like AutoFS and performance optimizations as your needs evolve.

For more home server and Linux tutorials, check out my other articles on best mini PCs for home servers and Docker containers for home servers.

Start Building Your NFS Setup

Related Posts