To mount NFS shares we need to install nfs-common:
sudo apt-get install nfs-common
To save us from retyping this after every reboot we add the following line to /etc/fstab:
<nfs-server-IP>:/ /mnt nfs auto 0 0
If after mounting, the entry in /proc/mounts appears as :// (with two slashes), then you might need to specify two slashes in /etc/fstab, or else umount might complain that it cannot find the mount.
The auto option mounts on startup. However this will not work if your client uses a wifi connection managed at the user level (after login), because the network will not be available at boot time. In Ubuntu 12.04 LTS and later, wifi connections are managed at the system level by default, so auto-mounting of NFS shares at boot time should work fine
Source: https://help.ubuntu.com/community/SettingUpNFSHowTo
With WIFI it's better to use Autofs:
We start by installing AutoFS:
sudo apt install autofs
We edit /etc/auto.master:
sudo nano /etc/auto.master
Content:
#
# Sample auto.master file
# This is a 'master' automounter map and it has the following format:
# mount-point [map-type[,format]:]map [options]
# For details of the format look at auto.master(5).
#
#/misc /etc/auto.misc
#
# NOTE: mounts done from a hosts map will be mounted with the
# "nosuid" and "nodev" options unless the "suid" and "dev"
# options are explicitly given.
#
#/net -hosts
#
# Include /etc/auto.master.d/*.autofs
# The included files must conform to the format of this file.
#
#+dir:/etc/auto.master.d
#
# Include central master map if it can be found using
# nsswitch sources.
#
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#
#+auto.master
/media/nfs /etc/auto.nfs --ghost
Comment out (#) "+auto.master" and "+dir:/etc/auto.master.d" and add the lines at the bottom. I spent 2hours trying to get this to work and somehow it does not work without adding the --ghost option. If someone knows why please comment. Now /media/nfs is the dir that will contain your NFS shares (you dont have to create that, autofs does that for you) and /etc/auto.nfs is the configuration file for your shares. We will make that now:
sudo nano /etc/auto.nfs
Insert shares:
Backup 10.0.1.100:/Backup
Multimedia 10.0.1.100:/Multimedia
Now Backup the the dir 10.0.1.100:/Backup will take in /media/nfs.
Restart autofs:
sudo systemctl restart autofs
That's it, enjoy your shares.
Well, finally I've solved the problem.
In this case, I had two problems:
By default, remote-fs.target is NOT depending on network-online.target . This is an known ¿bug? reported by the comunity. So, this si what makes that sometimes work and sometimes not (depends if ethernet has link or not when nfs is mounted).
Second problem, in my case, I mount rbinds after mount the NFS. The source of the rbinds is a folder of the NFS mountpoint (so, it's clear that is has to be mounted later). When systemd generates the ".mount" unit files for every mountpoint of the fstab, all rbinds were depending of local-fs.target. Of course, this is a problem in this case because it could mount the rbinds (local-fs.target) before the nfs mountpoint (remote-fs.target).
So, to solve this situation what I made is:
First, I edited the file /lib/systemd/system/remote-fs-pre.target and added:
Wants=network-online.target
After=network-online.target
Then, in /etc/fstab I indicated to the NFS mountpoint the next options:
X.X.X.X:/nfs_share /mnt/nas nfs _netdev,noauto,x-systemd.automount,nolock,soft,intr,nfsvers=3,tcp,bg,rw 0 0
And finally, we indicate to systemd that rbinds are network device (so, systemd will put them on remote-fs.target) and also I indicated that /mnt/nas has to be mounted strictly.
/mnt/nas/config/php /etc/php none noauto,_netdev,x-systemd.automount,x-systemd.requires=/mnt/nas,rbind,defaults 0 0
With this change, I fix the order of launching the mounts propertly in systemd
Best Answer
Setting
nfsvers=4
would cause the mount to fail if your NFS service is version 3. You need to setnfsvers=3
, or not set it at all (fromman nfs
):