Skip to content

Instantly share code, notes, and snippets.

@asad-albadi
Created February 23, 2024 16:19
Show Gist options
  • Save asad-albadi/10573707f06d488146c25b0fccf4418a to your computer and use it in GitHub Desktop.
Save asad-albadi/10573707f06d488146c25b0fccf4418a to your computer and use it in GitHub Desktop.

NFS Share Auto-Mount Guide on Boot

This guide details the steps to ensure an NFS share mounts automatically at boot on a Linux system, incorporating troubleshooting tips and a custom systemd service for reliability.

Prerequisites

  • NFS client utilities installed on your system.
  • Access to edit system configuration files with root permissions.

Step 1: Install NFS Client Utilities

Ensure the NFS client utilities are installed:

sudo apt update
sudo apt install nfs-common

Step 2: Configure /etc/fstab

Edit the /etc/fstab file to include your NFS share with the appropriate options:

  1. Open /etc/fstab with a text editor, such as nano:

    sudo nano /etc/fstab
  2. Add the NFS share entry:

    10.10.12.85:/mnt/storage/docker-pv /mnt/nfs nfs _netdev,auto,nofail,vers=3 0 0
    
    • _netdev ensures the mount waits for the network.
    • auto enables automatic mounting at boot.
    • nofail prevents boot failure if the mount fails.
    • vers=3 specifies NFS version 3.

Step 3: Ensure Network Availability

Make sure the system waits for the network to be fully available before attempting the NFS mount:

sudo systemctl enable NetworkManager-wait-online.service
sudo systemctl start NetworkManager-wait-online.service

Step 4: Create a Custom Systemd Service

If necessary, create a custom systemd service for more reliable NFS mounting:

  1. Create the Service File:

    Open a new service file in /etc/systemd/system/:

    sudo nano /etc/systemd/system/nfs-mount.service
  2. Service File Content:

    Input the following configuration, using your provided setup for the retry logic:

    [Unit]
    Description=Mount NFS Share
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    Type=oneshot
    ExecStart=/bin/sh -c 'while ! mount /mnt/nfs; do sleep 5; done'
    ExecStop=/bin/umount /mnt/nfs
    RemainAfterExit=yes
    
    [Install]
    WantedBy=multi-user.target
  3. Enable and Start the Service:

    Reload systemd, enable, and start the service:

    sudo systemctl daemon-reload
    sudo systemctl enable nfs-mount.service
    sudo systemctl start nfs-mount.service

Step 5: Reboot and Verify

After setting up, reboot your system to test the automatic mount:

sudo reboot

Verify the NFS share is mounted successfully:

df -h

or

mount | grep nfs

This guide incorporates the necessary steps and a custom systemd service to ensure your NFS share mounts automatically at boot, addressing common issues related to network dependency and permissions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment