Set Up NFS Server for File Sharing Between VPS Linux
NFS (Network File System) allows multiple VPS instances to share files over a network just like accessing a local directory. It is commonly used in web clusters requiring shared storage for sessions, uploaded files, or media that all nodes need to access.
Install NFS on Server
# On NFS server
sudo apt update
sudo apt install nfs-kernel-server -y
# Create directory to share
sudo mkdir -p /exports/shared
sudo chown nobody:nogroup /exports/shared
sudo chmod 755 /exports/shared
Configure Exports
sudo nano /etc/exports
# Syntax: /path client(options)
# Share to single IP
/exports/shared 192.168.1.100(rw,sync,no_subtree_check)
# Share to entire subnet /24
/exports/shared 192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash)
# Share read-only to all clients (not recommended in production)
/exports/readonly *(ro,sync,no_subtree_check)
Important options:
rw— read-write,ro— read-onlysync— write to disk before replying (more secure than async)no_subtree_check— disable subtree check to reduce overheadno_root_squash— allow root on client to become root on server (be careful with security)
# Apply new exports
sudo exportfs -ra
# View active exports
sudo exportfs -v
Install NFS on Client
# On NFS client
sudo apt install nfs-common -y
# Mount temporarily
sudo mkdir -p /mnt/nfs-shared
sudo mount NFS_SERVER_IP:/exports/shared /mnt/nfs-shared
# Verify mount succeeded
df -h | grep nfs
ls /mnt/nfs-shared
Permanent mount (add to /etc/fstab):
NFS_SERVER_IP:/exports/shared /mnt/nfs-shared nfs defaults,_netdev,rw 0 0
_netdev option tells the system that this filesystem requires network connectivity and will only mount after the network is up. This prevents boot hangs when the NFS server is unavailable.
Configure Firewall for Security
# Allow only known clients
sudo ufw allow from 192.168.1.0/24 to any port nfs
sudo ufw allow from 192.168.1.0/24 to any port 111 # portmapper
# Check firewall rules
sudo ufw status | grep -E "nfs|111|2049"
If you need to connect across the public internet, use a VPN instead of exposing port 2049 publicly. NFS does not encrypt traffic by default.
For high-quality VPS that support private networking between instances, NFS clustering becomes much more practical. asiagb.com offers VPS services in Thailand and Singapore with private networking within the datacenter, ensuring NFS traffic never hits the public internet. Before deploying, verify DNS resolution and port connectivity using dnsxray.com, a free domain health and network checker.
Verify NFS is Working Correctly
# On server, check service status
systemctl status nfs-kernel-server
# View exported shares
showmount -e localhost
# On client, verify mount
mount | grep nfs
df -h | grep nfs
# Test writing a file from client
touch /mnt/nfs-shared/testfile.txt
ls -la /mnt/nfs-shared/
dd if=/dev/zero of=/mnt/nfs-shared/test bs=1M count=100 oflag=direct to check actual throughput. You should achieve at least 50-100 MB/s on a private network.
Common Issues & Troubleshooting
Error: "mount.nfs: access denied by server while mounting"
The client IP does not match your exports configuration or exports has not been reloaded:
# Check the actual client IP as seen by the server
# On server
ss -tn | grep 2049
# Check exports
sudo exportfs -v
# Reload exports
sudo exportfs -ra
sudo systemctl reload nfs-kernel-server
NFS mount is very slow or hangs for long periods
This may be due to MTU mismatch or network latency. Try mounting with tuning options:
# Mount with performance tuning options
sudo mount -o rw,sync,rsize=131072,wsize=131072,timeo=14,intr \
NFS_SERVER_IP:/exports/shared /mnt/nfs-shared
Error: "RPC: Port mapper failure - Unable to receive: errno 111"
The firewall on the server is blocking port 111 (portmapper) or the rpcbind service is not running:
# Check rpcbind service
systemctl status rpcbind
sudo systemctl start rpcbind
sudo systemctl enable rpcbind
# Allow port 111 from client
sudo ufw allow from CLIENT_IP to any port 111
Files created on client are owned by nobody:nogroup
The UID/GID of the user on the client does not match the server. You need to either make UIDs match on both machines or use all_squash with anonuid/anongid:
# In /etc/exports, set anonuid/anongid to match server user
/exports/shared 192.168.1.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
Frequently Asked Questions
What is the difference between NFS and Samba? Which should I use?
NFS is the Unix/Linux native standard — it is faster with less overhead, making it ideal for Linux-to-Linux connections. Samba uses the SMB protocol and is better for sharing with Windows systems. If all your machines are Linux, NFS is the better choice.
What is the difference between NFSv3 and NFSv4?
NFSv4 supports stateful connections, ACLs, and mounts through a single port 2049 (simpler firewall configuration). It also has better performance over WAN. NFSv3 still works well with legacy systems and has slightly less overhead on LAN. Modern Ubuntu defaults to NFSv4.
Is NFS secure enough for production?
NFS has no built-in encryption and should only be used on private networks within a datacenter. If you need to send traffic across the public internet, first wrap it with a VPN (WireGuard/OpenVPN), then run NFS over that encrypted tunnel.