How to Implement NFS Directory Sharing for CentOS 7 Servers?

Understanding NFS Directory Sharing
Network File System (NFS) remains the backbone of file sharing in Linux environments, particularly crucial for US hosting setups requiring seamless data access across multiple servers. Whether you’re managing a web cluster or implementing a distributed storage solution, mastering NFS configuration on CentOS 7 can significantly enhance your server infrastructure.
Prerequisites and Environment Setup
Before diving into NFS configuration, ensure your environment meets these requirements:
- Two or more CentOS 7 servers (minimum kernel version 3.10.0)
- Root or sudo access on all servers
- Static IP addresses configured
- SELinux configured appropriately
- Firewall rules allowing NFS traffic (ports 111, 2049, and 20048)
Server-Side Configuration
Let’s start with configuring the NFS server. We’ll use practical examples with a server IP of 192.168.1.100:
# Install required packages
sudo yum install nfs-utils nfs-utils-lib
# Create shared directory
sudo mkdir /shared_data
sudo chmod 755 /shared_data
# Configure exports
sudo vi /etc/exports
/shared_data 192.168.1.0/24(rw,sync,no_root_squash)After configuring exports, initialize the NFS services:
# Start and enable NFS services
sudo systemctl start rpcbind nfs-server
sudo systemctl enable rpcbind nfs-server
# Export the shared directories
sudo exportfs -rv
# Verify exports
sudo exportfs -vClient-Side Implementation
With the server configured, let’s set up the client machine (assuming client IP is 192.168.1.101). The process requires precise steps to ensure reliable connectivity:
# Install NFS packages on client
sudo yum install nfs-utils nfs-utils-lib
# Create mount point
sudo mkdir /mnt/shared_data
# Test NFS connectivity
showmount -e 192.168.1.100
# Mount the NFS share
sudo mount -t nfs 192.168.1.100:/shared_data /mnt/shared_data
# Add to fstab for persistent mounting
echo "192.168.1.100:/shared_data /mnt/shared_data nfs defaults 0 0" | sudo tee -a /etc/fstabSecurity Optimization
Securing your NFS setup is crucial in US hosting environments. Implement these advanced security measures to protect your shared data:
# Configure NFSv4 with Kerberos (if applicable)
sudo vi /etc/sysconfig/nfs
RPCNFSDARGS="-V 4.2"
# Set proper SELinux context
sudo semanage fcontext -a -t nfs_t "/shared_data(/.*)?"
sudo restorecon -R /shared_data
# Configure firewall rules
sudo firewall-cmd --permanent --add-service=nfs
sudo firewall-cmd --permanent --add-service=mountd
sudo firewall-cmd --permanent --add-service=rpc-bind
sudo firewall-cmd --reloadPerformance Tuning
Optimize your NFS performance with these proven configurations:
# Adjust NFS thread count
sudo vi /etc/sysconfig/nfs
RPCNFSDCOUNT=16
# Optimize mount options
sudo mount -o rw,sync,hard,intr,rsize=32768,wsize=32768 192.168.1.100:/shared_data /mnt/shared_data
# Configure system limits
sudo vi /etc/sysctl.conf
net.core.wmem_max = 16777216
net.core.rmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216Troubleshooting Guide
When issues arise in your NFS setup, use these diagnostic commands and solutions:
# Check NFS server status
sudo systemctl status nfs-server
# View NFS statistics
nfsstat -s
nfsstat -c
# Monitor NFS connections
watch -n1 "netstat -an | grep :2049"
# Check NFS logs
sudo tail -f /var/log/messagesCommon issues and solutions:
- “Access denied” – Check permissions and export options
- “RPC timeout” – Verify network connectivity and firewall rules
- “Stale file handle” – Remount the NFS share
- “Permission denied” – Verify user/group mappings
Best Practices for Production Environments
Implement these proven strategies in your US hosting environment:
# Regular backup schedule
0 2 * * * rsync -av /shared_data/ /backup/nfs/
# Monitor NFS performance
# Add to /etc/sysconfig/nfs-utils
RPCNFSDCOUNT="8"
RPCMOUNTDOPTS="-p 892"
# Enable NFS reporting
systemctl enable nfs-utils
systemctl start nfs-utilsAdvanced Use Cases
Consider these scenarios for your hosting infrastructure:
- Web Cluster Configuration:
# Mount shared web assets 192.168.1.100:/shared_data/web /var/www/html nfs defaults,_netdev 0 0 - Database Storage:
# Mount database files 192.168.1.100:/shared_data/db /var/lib/mysql nfs rw,sync,hard,intr 0 0
Monitoring and Maintenance
Implement these monitoring solutions:
# Install monitoring tools
sudo yum install nagios-plugins-nfs
# Create monitoring script
#!/bin/bash
if ! mountpoint -q /mnt/shared_data; then
echo "NFS mount check failed"
exit 1
fi
echo "NFS mount check successful"Conclusion
Implementing NFS sharing in your CentOS 7 server environment requires careful planning and execution. This guide covers essential aspects of NFS configuration, from basic setup to advanced optimization techniques. For US hosting environments, particularly in high-performance scenarios, following these best practices ensures reliable file sharing across your server infrastructure. Remember to regularly update your NFS configuration and monitor system performance for optimal results.
