Mounting an NFS (Network File System) drive via /etc/fstab in Ubuntu allows the drive to be automatically mounted at boot time. Below are the steps to achieve this:
1. Install NFS Client (if not already installed)
Ensure the NFS client is installed on your Ubuntu system:
sudo apt update
sudo apt install nfs-common
2. Edit /etc/fstab
Open the /etc/fstab file in a text editor with root privileges:
sudo nano /etc/fstab
3. Add NFS Entry to /etc/fstab
Add a new line at the end of the file with the following format:
<NFS-Server>:<NFS-Export> <Mount-Point> nfs <Options> 0 0
<NFS-Server>: The IP address or hostname of the NFS server.
<NFS-Export>: The directory being exported from the NFS server.
<Mount-Point>: The local directory where the NFS share will be mounted.
<Options>: Mount options (e.g., defaults,noatime,nolock,soft).
Example:
192.168.1.100:/shared /mnt/nfs nfs defaults,noatime,nolock,soft 0 0
4. Create the Mount Point (if it doesn't exist)
Ensure the mount point directory exists:
sudo mkdir -p /mnt/nfs
5. Test the Configuration
Test the fstab entry to ensure it works without rebooting:
sudo mount -a
Check if the NFS share is mounted:
df -h
Or list the contents of the mounted directory:
ls /mnt/nfs
6. Common Mount Options
Here are some common NFS mount options you might use:
defaults: Use default options.
noatime: Do not update inode access times on the server (reduces network traffic).
nolock: Disable file locking (useful if locking is not needed).
soft: Fail silently if the server does not respond (as opposed to hard, which keeps retrying).
ro: Mount read-only.
rw: Mount read-write.
sync: Ensures data is written to the server before completing the write operation.
async: Allows asynchronous writes (faster but riskier).
7. Reboot or Remount
After editing /etc/fstab, you can either reboot the system or manually remount all filesystems to apply the changes:
sudo mount -a
8. Troubleshooting
- Permission Issues: Ensure the NFS export has the correct permissions on the server and that the client has access.
- Firewall: Ensure the firewall on the NFS server allows NFS traffic (typically UDP ports 2049, 111, and TCP ports 2049, 111, 20048).
- Showmount: Use
showmount -e <NFS-Server> to verify the exported directories on the NFS server.
Example /etc/fstab Entry:
192.168.1.100:/shared /mnt/nfs nfs defaults,noatime,nolock,soft 0 0
This setup ensures the NFS share is automatically mounted at boot and is accessible at /mnt/nfs.