To set up NFS to export /mnt/backup, follow these steps. First, I need to know your Linux distribution (e.g., Ubuntu, CentOS, Fedora, Arch). I will assume Ubuntu LTS for the instructions. If you use a different distribution, please let me know.
I will use 192.168.1.0/24 as an example for your private local network range. Adjust this to match your actual network.
On the NFS Server
-
Install NFS Server Software This command updates your package lists and installs the NFS kernel server.
sudo apt update sudo apt install -y nfs-kernel-serverExplanation:
apt updaterefreshes available package lists;apt install -yinstallsnfs-kernel-serverwithout prompting. -
Create the Directory to Export If
/mnt/backupdoesn’t exist, create it. Set permissions suitable for NFS.sudo mkdir -p /mnt/backup sudo chown nobody:nogroup /mnt/backup sudo chmod 777 /mnt/backupExplanation:
mkdir -pcreates the directory;chown nobody:nogroupsets ownership, often used for NFS exports to simplify initial permissions;chmod 777grants read/write/execute to all, adjust this later for tighter security. -
Configure NFS Exports Back up the existing
/etc/exportsfile, then open it for editing and add the export entry.sudo cp /etc/exports /etc/exports.bak sudo nano /etc/exportsAdd the following line to the end of the file, replacing
192.168.1.0/24with your network:/mnt/backup 192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash)Explanation of options:
192.168.1.0/24: Allows access from any host in this network range. Use a specific IP address (e.g.,192.168.1.100) to restrict access to a single client.rw: Grants read and write access to clients.sync: Forces NFS to write changes to disk before replying, ensuring data consistency (can impact performance).no_subtree_check: Disables subtree checking, which can improve reliability in some cases.no_root_squash: WARNING: This option allows the root user on the client to have root privileges on the NFS server for the exported directory. Use with extreme caution and only on trusted private networks. For better security, omit this option or useroot_squash(the default).
Save and close the file (Ctrl+X, Y, Enter in Nano).
-
Export the Shares and Restart NFS Service Apply the new export configuration and ensure the NFS server is running and enabled.
sudo exportfs -a sudo systemctl restart nfs-kernel-server sudo systemctl enable nfs-kernel-serverExplanation:
exportfs -aexports all directories listed in/etc/exports;systemctl restartrestarts the service;systemctl enableensures it starts on boot.Verification: Check the status of the NFS service.
systemctl status nfs-kernel-serverExpected output: Should show
Active: active (running). -
Configure Firewall (UFW) Allow NFS traffic from your private network.
sudo ufw allow from 192.168.1.0/24 to any port nfs sudo ufw reloadExplanation:
ufw allowadds a rule to permit NFS traffic from the specified network;ufw reloadapplies the changes.Verification: Check UFW status.
sudo ufw statusExpected output: Should show a rule similar to
NFS ALLOW 192.168.1.0/24.
On the NFS Client
-
Install NFS Client Software On the client machine, install the necessary NFS utilities.
sudo apt update sudo apt install -y nfs-commonExplanation:
nfs-commonprovides tools likeshowmountand the NFS client libraries. -
Verify Available NFS Shares Check which shares are exported by the NFS server. Replace
<NFS_SERVER_IP>with your server’s IP address.showmount -e <NFS_SERVER_IP>Expected output: Should list
/mnt/backupas an available export. -
Create Mount Point and Mount the NFS Share Create a local directory on the client where the NFS share will be mounted, then mount it.
sudo mkdir -p /mnt/nfs_backup sudo mount <NFS_SERVER_IP>:/mnt/backup /mnt/nfs_backupExplanation:
mkdir -pcreates the mount point;mountconnects the remote NFS share to the local directory.Verification: Check if the share is mounted.
df -h /mnt/nfs_backupExpected output: Should show the NFS share mounted with available space.
Test write access:
sudo touch /mnt/nfs_backup/test_file.txt ls /mnt/nfs_backupExpected output:
test_file.txtshould appear. If there are permission issues, check/etc/exportsoptions and directory permissions on the server. -
Automount on Boot (Optional) To make the NFS share mount automatically every time the client boots, add an entry to
/etc/fstab.sudo nano /etc/fstabAdd the following line to the end of the file:
<NFS_SERVER_IP>:/mnt/backup /mnt/nfs_backup nfs defaults,timeo=900,retrans=5,_netdev 0 0Explanation:
defaultsuses standard mount options;timeoandretransare timeout/retry settings;_netdevtells the system to wait for network availability before attempting to mount.Save and close the file. Test the fstab entry without rebooting:
sudo umount /mnt/nfs_backup sudo mount -aVerification:
df -h /mnt/nfs_backupshould show the share mounted again.