Overview of NFS #
Network File System (NFS) is a distributed file system protocol that allows you to share directories and files with other Linux clients over a network. It’s often used for centralized storage where files need to be accessed across different systems.
Prerequisites #
- Two Linux systems:
- NFS Server: The server that hosts the shared directory.
- NFS Client: The system that will access the shared directory.
- Root or sudo access on both the server and client systems.
- Firewall configuration (if needed) to allow NFS traffic.
Step 1: Install NFS on the Server #
- Update the package index:
sudo apt update # On Debian/Ubuntu
sudo yum update # On CentOS/RHEL
2. Install the NFS server package:
sudo apt install nfs-kernel-server # On Debian/Ubuntu
sudo yum install nfs-utils # On CentOS/RHEL
3. Enable and start the NFS service:
sudo systemctl enable nfs-server
sudo systemctl start nfs-server
Step 2: Create a Shared Directory #
Choose a directory on the server to share. Here, we’ll use /srv/nfs_share
.
- Create the directory:
sudo mkdir -p /srv/nfs_share
2. Set permissions on the directory (adjust permissions as needed):
sudo chown -R nobody:nogroup /srv/nfs_share # Debian/Ubuntu
sudo chown -R nfsnobody:nfsnobody /srv/nfs_share # CentOS/RHEL
3. Add files to the directory (optional, for testing):
echo "NFS Test File" | sudo tee /srv/nfs_share/test.txt
Step 3: Configure NFS Exports #
- Open the exports file:
sudo nano /etc/exports
2. Add the export entry for your directory and client IP. The syntax is:
/path/to/share client_IP(options)
For example, to share /srv/nfs_share
with a client at IP 192.168.1.100
with read-write access:
/srv/nfs_share 192.168.1.100(rw,sync,no_subtree_check)
Explanation of Options:
rw
: Allows read and write access.sync
: Writes changes to disk before confirming.no_subtree_check
: Prevents subtree checking for better performance.
3. Save and close the file.
4. Apply the changes by exporting the shared directories:
sudo exportfs -a
5. Verify the NFS share
sudo exportfs -v
Step 4: Configure the Firewall (if applicable) #
If you have a firewall enabled, allow NFS service traffic.
- Open NFS ports:
sudo ufw allow from 192.168.1.0/24 to any port nfs # On Debian/Ubuntu with UFW
sudo firewall-cmd --permanent --add-service=nfs # On CentOS/RHEL with Firewalld
sudo firewall-cmd --reload
Step 5: Mount the NFS Share on the Client #
Now that the server is configured, set up the client to access the shared directory.
- Install NFS client packages:
sudo apt install nfs-common # On Debian/Ubuntu
sudo yum install nfs-utils # On CentOS/RHEL
2. Create a mount point on the client:
sudo mkdir -p /mnt/nfs_share
3. Mount the NFS share to the client’s mount point:
sudo mount -t nfs 192.168.1.10:/srv/nfs_share /mnt/nfs_share
- Replace
192.168.1.10
with the IP of your NFS server.
4. Verify the mount:
df -h | grep nfs_share
5. Access the shared directory:
ls /mnt/nfs_share
cat /mnt/nfs_share/test.txt
Step 6: Auto-mount the NFS Share at Boot (Optional) #
To automatically mount the NFS share at boot, add it to the /etc/fstab
file on the client.
- Open the fstab file:
sudo vi /etc/fstab
2. Add the following entry at the end of the file:
192.168.1.10:/srv/nfs_share /mnt/nfs_share nfs defaults 0 0
3. Save and close the file.
4. Test the fstab entry:
sudo mount -a
Step 7: Manage and Troubleshoot NFS #
1. To unmount the NFS share: #
sudo umount /mnt/nfs_share
2. To restart the NFS service on the server
sudo systemctl restart nfs-server
3. To check NFS logs for troubleshooting:
sudo tail -f /var/log/syslog # On Debian/Ubuntu
sudo tail -f /var/log/messages # On CentOS/RHEL
Final word #
You’ve now set up an NFS server to share directories with Linux clients over a network. This setup allows easy access to shared files, making it ideal for environments that need centralized file storage.