1 Mounting Server Folder - NFS Storage Setup

A step-by-step guide to mounting remote folders via NFS โ€” because sometimes your server needs to borrow storage from its neighbor.

Table of Contents


๐Ÿ“– Overview

This guide explains how to store files on Server A from Server B using mounted folders via NFS (Network File System). When you mount Server A's shared folder on Server B, any files written to the mounted folder are directly stored on Server A's physical storage, not on Server B.


โš™๏ธ How It Works

When Server B mounts Server A's directory, the mount point on Server B acts as a gateway. All file operations (read, write, delete) performed on the mount point are translated into network requests and executed on Server A's actual storage.

Important

Server B only uses minimal local resources for caching and network operations, but the actual data resides on Server A.


๐Ÿชœ Step 1: Configure Server A (Storage Server)

Install NFS Server

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

Create the Shared Folder

# Create the directory that will hold your files
sudo mkdir -p /var/shared_folder

# Set permissions (adjust based on your security needs)
sudo chown nobody:nogroup /var/shared_folder
sudo chmod 777 /var/shared_folder

Configure NFS Exports

Edit the exports file:

sudo nano /etc/exports

Add this line (replace 192.168.1.100 with Server B's IP):

/var/shared_folder 192.168.1.100(rw,sync,no_subtree_check,no_root_squash)

Key Options Explained:

Apply the Configuration

# Export the shares
sudo exportfs -arv

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

# Verify exports
sudo exportfs -v

Configure Firewall

# Allow Server B to access NFS
sudo ufw allow from 192.168.1.100 to any port nfs

๐Ÿชœ Step 2: Configure Server B (Client Using Remote Storage)

Install NFS Client

# Ubuntu/Debian
sudo apt update
sudo apt install nfs-common -y

Create a Mount Point

# This is just an empty directory โ€” a gateway to Server A's storage
sudo mkdir -p /mnt/remote_storage

Mount Server A's Folder

Basic mount:

# Replace 192.168.1.50 with Server A's IP address
sudo mount -t nfs 192.168.1.50:/var/shared_folder /mnt/remote_storage

Optimized mount with better performance:

sudo mount -t nfs -o vers=4,rw,hard,proto=tcp 192.168.1.50:/var/shared_folder /mnt/remote_storage

Verify the Mount

df -h | grep remote_storage
# or
mount | grep nfs

Expected output:

192.168.1.50:/var/shared_folder  100G   10G   90G  10% /mnt/remote_storage

๐Ÿชœ Step 3: Test File Storage

On Server B

Create a test file:

# Write to the mounted folder on Server B
echo "Test data from Server B" > /mnt/remote_storage/test.txt
ls -lh /mnt/remote_storage/

On Server A

Verify the file exists locally:

# Check Server A's actual folder
ls -lh /var/shared_folder/
cat /var/shared_folder/test.txt

You should see the same file โ€” it's physically stored on Server A, not Server B.


๐Ÿชœ Step 4: Permanent Mount (Auto-mount on Boot)

Edit fstab

sudo nano /etc/fstab

Add Mount Entry

Basic configuration:

192.168.1.50:/var/shared_folder /mnt/remote_storage nfs defaults,_netdev,vers=4 0 0

Recommended configuration with more options:

192.168.1.50:/var/shared_folder /mnt/remote_storage nfs rw,hard,intr,proto=tcp,vers=4,_netdev,timeo=600,retrans=2 0 0

Important Mount Options:

Test the fstab Entry

# Test without rebooting
sudo umount /mnt/remote_storage
sudo mount -a

# Verify
df -h | grep remote_storage

Verify After Reboot

sudo reboot

After reboot, check:

df -h | grep remote_storage

๐Ÿ” Storage Usage Verification

On Server B

# Check overall disk usage
df -h

# The /mnt/remote_storage should show Server A's storage size
# NOT Server B's local disk

On Server A

# Check the actual physical storage
du -sh /var/shared_folder
Note

When you write large files to /mnt/remote_storage on Server B, Server A's storage will fill up, not Server B's.


๐Ÿ“ Use Case Examples

Node.js Application

// On Server B - application writes to mounted folder
const fs = require('fs');
const uploadPath = '/mnt/remote_storage/uploads';

// Files are stored on Server A's disk
fs.writeFileSync(`${uploadPath}/user_upload.pdf`, fileData);

PostgreSQL Data Directory

# On Server B, configure PostgreSQL to use Server A's storage
# Edit postgresql.conf
data_directory = '/mnt/remote_storage/postgresql/data'

Docker Integration

# docker-compose.yml on Server B
version: '3.8'
services:
  app:
    image: myapp
    volumes:
      # Container writes to Server A's storage
      - /mnt/remote_storage/app_data:/app/data

โšก Performance Considerations

Network Speed Matters:

Latency:

Caching:

Enable client-side caching for better read performance:

sudo mount -t nfs -o vers=4,rw,hard,proto=tcp,ac,acregmin=60,acregmax=600 192.168.1.50:/var/shared_folder /mnt/remote_storage

๐Ÿ”’ Security Best Practices

Firewall Rules

Restrict NFS access only to Server B's IP:

# On Server A
sudo ufw allow from 192.168.1.100 to any port 2049
sudo ufw deny from any to any port 2049

Network Security

Use a private network (VPN or private VLAN) between servers if possible, especially if they're on public networks.

Proper Permissions

# On Server A - more restrictive permissions
sudo chown youruser:yourgroup /var/shared_folder
sudo chmod 755 /var/shared_folder

Regular Backups

Since all data is on Server A, implement a backup strategy for that server.


๐Ÿ’ก Alternative: SSHFS for Encrypted Storage

If your servers are on untrusted networks or you need encryption, use SSHFS instead.

Install and Mount SSHFS

# Install SSHFS
sudo apt install sshfs -y

# Mount Server A's folder with encryption
sudo sshfs -o allow_other,default_permissions,compression=yes,reconnect user@192.168.1.50:/var/shared_folder /mnt/remote_storage

Permanent SSHFS Mount

Add to /etc/fstab:

user@192.168.1.50:/var/shared_folder /mnt/remote_storage fuse.sshfs noauto,x-systemd.automount,_netdev,reconnect,identityfile=/home/user/.ssh/id_rsa,allow_other,default_permissions 0 0

๐Ÿ“‹ Summary

How It Works

  1. Server A exports /var/shared_folder via NFS
  2. Server B mounts it to /mnt/remote_storage
  3. Any application on Server B writing to /mnt/remote_storage โ†’ files physically stored on Server A
  4. Server B's local storage remains free
  5. Multiple servers can mount the same folder if needed

This is a common pattern for centralized storage, allowing you to scale compute (Server B) separately from storage (Server A).


๐Ÿ“‹ Quick Reference Commands

# Check mounted NFS shares
mount | grep nfs

# Check storage usage
df -h | grep remote_storage

# Unmount share
sudo umount /mnt/remote_storage

# Re-mount all fstab entries
sudo mount -a

# Check NFS exports on server
sudo exportfs -v

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