Network File System (NFS) allows seamless file sharing between Linux systems over a network. Whether you’re managing servers or collaborating on projects, NFS eliminates the hassle of manual file transfers. This guide covers server/client setup for NFSv4 (the modern standard) using systemd
.
Prerequisites
- Two Linux machines:
- Server: Hosts the shared directory
- Client: Accesses the shared directory
- Terminal access with
sudo
privileges on both - Static IPs for both (e.g., Server:
192.168.0.100
, Client:192.168.0.101
) - Firewall access for NFS ports (TCP/UDP
2049
)
Step 1: Server Setup
Install NFS Packages
sudo apt update && sudo apt install nfs-kernel-server -y # Debian/Ubuntu
sudo dnf install nfs-utils -y # RHEL/CentOS/Fedora
Create & Share a Directory
sudo mkdir -p /mnt/nfs_share
sudo chown nobody:nogroup /mnt/nfs_share # Relax permissions for testing
sudo chmod 777 /mnt/nfs_share # Optional: Adjust per security needs
Configure Exports
Edit /etc/exports
:
sudo nano /etc/exports
Add this line to share with a specific client (replace 192.168.0.101
):
/mnt/nfs_share 192.168.0.101(rw,sync,no_subtree_check)
rw
: Read/write accesssync
: Write changes immediatelyno_subtree_check
: Better performance
Apply changes:
sudo exportfs -ra # Reload exports
sudo systemctl restart nfs-server
Open Firewall
sudo ufw allow from 192.168.0.101 to any port nfs # Ubuntu
sudo firewall-cmd --permanent --add-service=nfs && sudo firewall-cmd --reload # RHEL
Step 2: Client Setup
Install NFS Client
sudo apt install nfs-common -y # Debian/Ubuntu
sudo dnf install nfs-utils -y # RHEL/CentOS
Mount the NFS Share
sudo mkdir -p /mnt/client_share
sudo mount 192.168.0.100:/mnt/nfs_share /mnt/client_share # Replace with server IP
Auto-Mount at Boot
Edit /etc/fstab
:
sudo nano /etc/fstab
Add:
192.168.0.100:/mnt/nfs_share /mnt/client_share nfs4 defaults,timeo=300,retrans=5 0 0
Apply:
sudo mount -a
Step 3: Verify Functionality
- On the server:
echo "Hello from server!" > /mnt/nfs_share/test.txt
- On the client:
cat /mnt/client_share/test.txt # Should show the message
- Create a file on the client and check the server.
Troubleshooting
- “Access Denied”:
- Verify client IP in
/etc/exports
- Check
chown
/chmod
on the server directory
- Verify client IP in
- Mount Hangs:
- Confirm NFS service is running:
sudo systemctl status nfs-server
- Check firewall rules:
sudo ufw status
orsudo firewall-cmd --list-all
- Confirm NFS service is running:
- Logs:
- Server:
tail -f /var/log/syslog
- Client:
dmesg | grep nfs
- Server:
Security Notes
- Restrict Exports: Never use
*
in/etc/exports
(e.g.,192.168.0.0/24
is safer than wildcards). - User Permissions: For strict access control:
- Use
sudo chown user:group /mnt/nfs_share
- Match UID/GIDs across server/client
- Use
- Kerberos: Enable
sec=krb5
in exports for enterprise setups.
Conclusion
NFS transforms cross-system file management into a seamless experience. Once configured, shared directories behave like local storage—ideal for clusters, media servers, or team projects. Test in a safe environment first, then explore advanced tuning like NFSv4.1 (parallel access) or automated mounts with autofs
.
> Pro Tip: Replace IPs with hostnames in /etc/exports
/fstab
if using DNS. Always back up config files before editing!