Using autofs
Install autofs
On Debian/Ubuntu based distros
sudo apt install autofs
On Redhat/Centos based distros
dnf install autofs
This will install the autofs packages and any missing dependencies. You may need to install the nfs utilities or even smb if you wish to use autofs to mount CIFS shares although in my experience these are automatically brought in as dependencies.
Configure autofs for NFS
edit /etc/auto.master
and add the following to the bottom of the file
/nfs /etc/auto.nfs
This entry means that when a process enters the /nfs
directory autofs will read the /etc/auto.nfs
file to determine what it should be mounting next.
Edit the /etc/auto.nfs
file
For example, to mount the /export/multimedia
nfs share on the nas the following line is required in the /etc/auto.nfs
file.
multimedia nas:/export/multimedia
Then restart or reload the autofs service
sudo systemctl restart autofs
or
sudo systemctl reload autofs
Now, in your terminal enter the following
cd /nfs
Note, that running ls
shows that you have an empty folder, this is becuase autofs doesn't mount anything until you actually enter the configured locations.
cd multimedia
will now mount the share from the NAS and typing ls
will show the directories on that share and df -h
will show the mounted file system too
e.g. The mounted share is the second last line in the output below
Filesystem Size Used Avail Use% Mounted on
tmpfs 1.6G 2.0M 1.6G 1% /run
/dev/sda3 64G 7.5G 57G 12% /
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 4.0M 0 4.0M 0% /sys/fs/cgroup
/dev/sda5 94G 27G 63G 30% /var/lib/docker
/dev/sda1 1022M 118M 905M 12% /boot/efi
/dev/sda4 32G 261M 32G 1% /transcode
/dev/sda6 251G 2.4G 248G 1% /home
nas:/export/multimedia 20T 12T 8.2T 59% /nfs/multimedia
tmpfs 1.6G 224K 1.6G 1% /run/user/1000
Using systemd automount
This method doesn't require any additional packages as it's built into systemd (the arguments for and against systemd are not for here but do a search to see the polarising views about it). Obviously if you're using a systemd free distro then you'll need the autofs method above...
There are a couple of ways of automounting file systems with systemd, the simple method is described below
For the simple method add the required filesystems in /etc/fstab with the x-systemd.automount option and create the required directories on your file system
Add the required nfs share and mount point to /etc/fstab
nas:/export/multimedia /nfs/multimedia nfs x-systemd.automount 0 0
Create the required directory for the mount point
# mkdir -p /nfs/multimedia
Then either reboot your system or reload systemd to enable the mount
# systemctl daemon-reload && systemctl start nfs-multimedia.automount
Here, the systemd unit is nfs-multimedia, the naming convention is to replace the /
inthe mount point path with a -
in the unit name.
If I had specified /multimedia
as the mount point then the command to start the unit would be systemctl start multimedia.automount
. If I had set the mount point as /nfs/plex/media
then the command would be systemctl start nfs-plex-media.automount
.
You can also add the x-systemd.idle-timeout option for systemd to unmount the file system when idle, and any other mount options the file system requires:
nas:/export/multimedia /nfs/multimedia nfs x-systemd.automount,x-systemd.idle-timeout=2min,rw,sync 0 0