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
- โ๏ธ How It Works
- ๐ช Step 1: Configure Server A (Storage Server)
- ๐ช Step 2: Configure Server B (Client Using Remote Storage)
- ๐ช Step 3: Test File Storage
- ๐ช Step 4: Permanent Mount (Auto-mount on Boot)
- ๐ Storage Usage Verification
- ๐ Use Case Examples
- โก Performance Considerations
- ๐ Security Best Practices
- ๐ก Alternative: SSHFS for Encrypted Storage
- ๐ Summary
- ๐ Quick Reference Commands
๐ 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.
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:
rwโ Allows Server B to read and write filessyncโ Ensures data is written to disk immediately before respondingno_subtree_checkโ Improves reliabilityno_root_squashโ Allows root on Server B to have root permissions on Server A (use cautiously)
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:
_netdevโ Wait for network before mounting (crucial)hardโ Retry indefinitely if Server A becomes unavailableintrโ Allow interruption of NFS operationstimeo=600โ Timeout value in deciseconds (60 seconds)retrans=2โ Number of retries before giving up
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
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:
- Use gigabit or faster connections between servers for optimal performance
Latency:
- Operations will be slower than local storage
- Expect 5-10ms latency for each operation vs <1ms for local disks
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
- Server A exports
/var/shared_foldervia NFS - Server B mounts it to
/mnt/remote_storage - Any application on Server B writing to
/mnt/remote_storageโ files physically stored on Server A - Server B's local storage remains free
- 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