Configure nfs share which is automounted by another device

Published

# Contents

Sometimes it is useful to have a computer/server on the network start a network-share, which is then automounted by one or more other computers/devices. I do this for backup-targets, and sometimes for services which needs some shared data.

# Configure the server

I run all of these servers on Debian machines, at the time of writing, they are running Debian 12.

First you need to install the server we will use to export the share

apt install nfs-kernel-server

After this you need to configure the nfs-server. This is done in the file /etc/exports. Usually the exports I need, are for a disk which is mounted in /mnt. So they are generally configured in /etc/fstab, but it can be any place in the filesystem. You need to add a line like this:

/mnt/disk1 10.0.0.3(rw,sync,subtree_check,insecure,no_root_squash)

Where the IP-address is there for restricting who can connect to the share. This is also not using encrypted transfer, which means that anyone on your network can snoop on this traffic. In my case these shares are in separate VLANs, which somewhat help mitigate the issue. So no "normal" devices, like phones or IOT devices can snoop on it. You are still however using quite unrestricive rules here.

At this point, all you have to do is restart nfs-kernel-server

systemctl restart nfs-kernel-server

# Configure automounting of the NFS-share

If you have machines which always are on, you can probably just configure these in `/etc/fstab` and be done with it. In my case I want it to automatically reconnect without a reboot (or other manual intervention, all you need to do is `mount -a`, or even add that as a cronjob), if the server was offline for some reason. I often use `autofs` for this purpose.
apt install autofs

There are only two files you need to change to configure autofs.

The first is /etc/auto.master where you need to add the line below:

/- /etc/auto.misc

Then you need to add the actual nfs-share to /etc/auto.misc with a line like:

/mnt/disk1 -fstype=nfs4,rw 10.0.0.2:/mnt/disk1
#   │                         │         └ The name of the share, this is the one configured in /etc/exports
#   │                         └ IP of the server which exports the share
#   └ Where to mount the share locally on this machine

Lastly you will probably have to restart autofs for the changes to take effect:

systemctl restart autofs