While the traditional method of mounting Network File System (NFS) shares involves editing the /etc/fstab file, this approach can cause boot delays or failures if the network is unavailable during startup. A more robust, modern approach on Ubuntu servers is to use systemd mount units. Systemd allows for delayed, on-demand mounting, ensuring the operating system boots smoothly regardless of network storage availability.
Why Use systemd Over fstab?
Systemd offers granular control over mount dependencies. By using an automount unit, the NFS share is only mounted when a user or application actually attempts to access the directory. This eliminates the dreaded “hanging at boot” issue caused by unreachable network drives.
Step 1: Install NFS Client Packages
First, ensure your Ubuntu system has the necessary NFS client tools installed:
sudo apt update
sudo apt install nfs-common
Step 2: Create the Mount Unit
Systemd requires mount unit files to be named exactly after the mount point path, replacing slashes with dashes. For example, to mount the share at /mnt/nfs_data, the file must be named mnt-nfs_data.mount.
- Create the mount point directory:
sudo mkdir -p /mnt/nfs_data - Create the systemd mount unit file:
sudo nano /etc/systemd/system/mnt-nfs_data.mount - Add the following configuration, replacing the IP and share path with your NFS server’s details:
[Unit] Description=Mount NFS Share After=network-online.target Wants=network-online.target [Mount] What=192.168.1.100:/exported/share Where=/mnt/nfs_data Type=nfs Options=rw,soft,timeo=30,retrans=3 [Install] WantedBy=multi-user.target
Step 3: Create the Automount Unit
To enable on-demand mounting, create an accompanying automount unit.
- Create the automount file:
sudo nano /etc/systemd/system/mnt-nfs_data.automount - Add the following configuration:
[Unit] Description=Automount NFS Share [Automount] Where=/mnt/nfs_data TimeoutIdleSec=300 [Install] WantedBy=multi-user.target
Step 4: Enable and Start the Service
Finally, reload systemd to recognize the new files, then enable and start the automount service (do not enable the .mount service directly):
sudo systemctl daemon-reload
sudo systemctl enable mnt-nfs_data.automount
sudo systemctl start mnt-nfs_data.automount
Now, simply navigate to /mnt/nfs_data. Systemd will detect the access attempt and instantly mount the NFS share in the background, providing a seamless and highly resilient storage solution.