How To Create NFS Shares and Automount them as systemd service in Ubuntu 22.04

When you click on links to various merchants on this site and make a purchase, this can result in this site earning a commission. Affiliate programs and affiliations include, but are not limited to, the eBay Partner Network. As an Amazon Associate I earn from qualifying purchases. #ad #promotions

Let’s connect some NFS mounts from a truenas share to a group of local folders like /mnt/dataAA – AE to be automatically mounted at system boot.

Mounting Remote NFS Shares with a systemd service in Ubuntu 22.04

Create directories for your mounts.

sudo mkdir -vp /mnt/dataA{A-E}

Install the nfs client and create a directory for your scripts.

sudo apt install nfs-common
sudo mkdir -v scripts
cd scripts
sudo nano mountnfs.sh

Then copy and paste in the below script. This assumes your machine IP is 192.168.1.XX so please update that to your NFS servers IP address.


#!/usr/bin/env bash
mount -t nfs 192.168.1.XX:/mnt/dataAA/dataAA /mnt/dataAA
mount -t nfs 192.168.1.XX:/mnt/dataAB/dataAB /mnt/dataAB
mount -t nfs 192.168.1.XX:/mnt/dataAC/dataAC /mnt/dataAC
mount -t nfs 192.168.1.XX:/mnt/dataAD/dataAD /mnt/dataAD
mount -t nfs 192.168.1.XX:/mnt/dataAE/dataAE /mnt/dataAE

CNTRL + X and Y to save and exit.

Next we will allow the script to be executed.

Then we will go ahead and mount the shares to validate they are mounted.

Now list the directories, you should see your 5 shares mounted now and their sizes reported.

Now unmount the shares now that you have validated your script works as intended since we will be creating a systemd service for this to automatically trigger at boot.

Next create a systemd service to run the script to mount nfs shares.

sudo nano /lib/systemd/system/mountnfs.service

Then paste in the following

[Unit]
Description=Mounting NFS Shares
After=multi-user.target
[Service]
ExecStart=/usr/bin/bash /home/dsp/scripts/mountnfs.sh
Type=simple
[Install]
WantedBy=multi-user.target

CNTRL + X and Y to save and exit.

sudo systemctl daemon-reload
sudo systemctl enable mountnfs.service
sudo systemctl status mountnfs.service

Finally you can reboot to test it fires.