Simplifying File Sharing: A Guide to Setting Up NFS Server and Client on Linux
Streamline File Transfers: Adopt NFS for Load Distribution and Consistent Uptime
Network File System (NFS) is a powerful tool that allows users to share files and directories across a network seamlessly. This blog post outlines the step-by-step process for setting up an NFS server and client on Linux, enabling efficient file sharing between systems.
Overview of NFS Setup
Setting up NFS involves two main components: the NFS server, which hosts the shared directories, and the NFS client, which accesses these directories. The following sections detail the commands necessary for configuration on both sides.
Server Side Configuration
Install NFS Server:
To begin, install the NFS kernel server package using:
sudo apt install nfs-kernel-server
Create Shared Directory:
Create a directory that will be shared:
sudo mkdir -p /root/public
Set Permissions:
Change ownership and permissions to allow access:sudo chown nobody:nogroup /root/public sudo chmod 777 /root/public
Configure Exports:
Edit the/etc/exports
file to define access permissions for clients. ReplaceIP_of_Server_B
with the actual IP address of the client:/root/public IP_of_Server_B(rw,no_root_squash,no_subtree_check)
Export the Shares:
Update the export table:sudo exportfs -a
Adjust Firewall Settings:
Allow NFS traffic through the firewall:ufw allow from Client_IP to any port nfs
Restart NFS Service:
Finally, restart the NFS server to apply changes:sudo systemctl restart nfs-kernel-server
Client Side Configuration
Install NFS Common Package:
On the client machine, install the necessary package:sudo apt install nfs-common
Create Mount Point:
Create a directory where the shared files will be mounted:sudo mkdir -p /mnt/public_share
Mount the Shared Directory:
Mount the NFS share from the server, replacingIP_of_Server_A
with the server's actual IP address:sudo mount -t nfs IP_of_Server_A:/root/public /mnt/public_share
Verify Mounting:
Check if the mount was successful:df -h | grep public_share
Persisting Mounts:
To ensure that the mount persists after a reboot, add it to/etc/fstab
:echo "IP_of_Server_A:/root/public /mnt/public_share nfs defaults 0 0" | sudo tee -a /etc/fstab
Conclusion
By following these steps, you can easily set up an NFS server and client on Linux, facilitating efficient file sharing across your network. This setup is ideal for environments where collaborative file access is necessary, enhancing productivity and resource management. With NFS, you can leverage your network's capabilities to streamline workflows and improve data accessibility across multiple systems.