Why can't I automount NFS directory using systemd units?

I’ve created a shared folder like the following:

server ❯ sudo cat /etc/fstab
/run/media/user/X /srv/nfs/X none bind 0 0
server ❯ sudo mount -a
client ❯ sudo mkdir -p /data/nfs/X
client ❯ sudo cat /etc/systemd/system/data-nfs-X.mount
[Unit]
Description=Shared partition X
After=network.target

[Mount]
What=192.168.1.122:/nfs/X
Where=/data/nfs/X
Type=nfs
Options=_netdev,auto

[Install]
WantedBy=multi-user.target
client ❯ sudo systemctl enable data-nfs-X.mount
client ❯ sudo cat /etc/systemd/system/data-nfs-X.automount
[Unit]
Description=Automount shared partition X
ConditionPathExists=/data/nfs/X

[Automount]
Where=/data/nfs/X
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target
client ❯ sudo systemctl enable --now data-nfs-X.automount
client ❯ ls /data/nfs/X
ls: no se puede abrir el directorio '/data/nfs/X': No existe el dispositivo
client ❯ sudo mount -t nfs 192.168.1.122:/X /data/nfs/X

Why can I mount the folder manually but I can’t do it with systemd units?

Hi @Individual-Heart-719,

I’ve never done anything with NFS, always had to use SAMBA, but I know you should:

  1. Not have anything mounted in /run, as /run is volatile and not for permanent mounts. Rather use /mnt, that’s what it’s there for.
  2. Don’t have any entries for the drive both in /etc/fstab and a systemd unit for it. Choose 1 option only. I recommend systemd units. Mine works like an absolute charm.
  3. Rather use the IP address for the NFS share.

See:

and

Hope it helps!

Do not do this. It will not always work as expected. Do not use /run/ for anything expect USB thumb-drive you mount with via a GUI program.

Does not look like the same.

Do not enable it, if you want to use automount. Disable it, just enable the automount.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.