Automounting NFS as user

I have to admit that I am building up a thorough frustration with automated mounts in general on Linux. My really really simple use case (notebook with wifi connection) is proving almost insurmountable to solve in a practical manner (no delay in (re)boot, available without manually having to mount it, rw access, some basic level of security (no chmod +777)).
Can someone give me some proper pointers?

  • In this case Arch wiki for autofs is quite ambiguous and not nearly detailed enough, at least for me.
  • systemd automount will mount it as root user, so no write access
  • static fstab will not mount it properly even with noauto,_netdev

Thanks.

I am using systemd mount units

You need two files for each mount - one mount and one automount

Also be sure to review the naming convention for mount units.

1 Like

This tutorial got me to the same place I already was with systemd automount before.

  • the share is mounted on demand sometimes (if it does not time out with Failed with result 'mount-start-limit-hit'.)
  • I have no write access
  • the reboot hangs

My unit looks like this:

[Unit]
Description = Automount NFS Share
ConditionPathExists=/mnt/mounts4bind/nfs_share

[Automount]
Where=/mnt/mounts4bind/nfs_share
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target

The examples in the referenced topic is battletested - I use them everyday.

The way this works is the automount unit activates the mount unit.

So one unit /etc/systemd/system/mnt-mounts4bind-nfs_share.mount

[Unit]
Description=nfs share

[Mount]
What=$YOUR_SERVER:/$YOUR_SHARE
Where=/mnt/mounts4bind/nfs_share
Type=nfs
Options=_netdev,auto

[Install]
WantedBy=multi-user.target

And another unit etc/systemd/system/mnt-mounts4bind-nfs_share.automount

[Unit]
Description = Automount NFS Share
ConditionPathExists=/mnt/mounts4bind/nfs_share

[Automount]
Where=/mnt/mounts4bind/nfs_share
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target
  1. Comment the line in fstab
  2. The automount will fail if the mount point is mounted
  3. ONLY start and enable the automount (I assume your mountpoint exist)

If your mount does not work then there is other issues at play

sudo systemctl enable --now mnt-mounts4bind-nfs_share.automount

Try moving the mountpoint out of /mnt to a designated folder like

sudo mkdir -p /data/nfs/share

Remember to rename the mount units and modify the mount point in the units.

Remember to set the correct permissions on the mount point

sudo chmod ugo+rwx /data/nfs/share

I feel like I am losing my sanity here, so I printed all the relevant things in a single file.
In short, the REboot still hangs and still only root has write (it is exported as rw, and no_root_squash).

sudo systemctl status mnt-mounts4bind-nfs_share.mount
● mnt-mounts4bind-nfs_share.mount - nfs share 
Loaded: loaded (/etc/systemd/system/mnt-mounts4bind-nfs_share.mount; disabled, vendor preset: disabled) 
Active: active (mounted) since Sun 2021-10-17 19:07:37 CEST; 2s ago 
TriggeredBy: ● mnt-mounts4bind-nfs_share.automount 
Where: /mnt/mounts4bind/nfs_share 
What: broken-lenovo.lan:/mnt/nfs_share 
Tasks: 0 (limit: 18926) 
Memory: 16.0K 
CPU: 11ms 
CGroup: /system.slice/mnt-mounts4bind-nfs_share.mount 
okt 17 19:07:37 myuser-dell systemd[1]: Mounting nfs share... 
okt 17 19:07:37 myuser-dell systemd[1]: Mounted nfs share.

sudo systemctl status mnt-mounts4bind-nfs_share.automount
● mnt-mounts4bind-nfs_share.automount - Automount NFS Share 
Loaded: loaded (/etc/systemd/system/mnt-mounts4bind-nfs_share.automount; enabled; vendor preset: disabled) 
Active: active (running) since Sun 2021-10-17 19:02:34 CEST; 5min ago 
Triggers: ● mnt-mounts4bind-nfs_share.mount 
Where: /mnt/mounts4bind/nfs_share 
okt 17 19:02:54 myuser-dell systemd[1]: mnt-mounts4bind-nfs_share.automount: Got automount request for /mnt/mounts4bind/nfs_share, triggered by 1741 (AUTHENTICATE) okt 17 19:03:12 myuser-dell systemd[1]: mnt-mounts4bind-nfs_share.automount: Got automount request for /mnt/mounts4bind/nfs_share, triggered by 2058 (dolphin) 
okt 17 19:06:32 myuser-dell systemd[1]: mnt-mounts4bind-nfs_share.automount: Got automount request for /mnt/mounts4bind/nfs_share, triggered by 3647 (kioslave5) okt 17 19:06:43 myuser-dell systemd[1]: mnt-mounts4bind-nfs_share.automount: Got automount request for /mnt/mounts4bind/nfs_share, triggered by 3647 (kioslave5) okt 17 19:06:54 myuser-dell systemd[1]: mnt-mounts4bind-nfs_share.automount: Got automount request for /mnt/mounts4bind/nfs_share, triggered by 3647 (kioslave5) 
okt 17 19:07:05 myuser-dell systemd[1]: mnt-mounts4bind-nfs_share.automount: Got automount request for /mnt/mounts4bind/nfs_share, triggered by 3647 (kioslave5) 
okt 17 19:07:15 myuser-dell systemd[1]: mnt-mounts4bind-nfs_share.automount: Got automount request for /mnt/mounts4bind/nfs_share, triggered by 3647 (kioslave5) 
okt 17 19:07:26 myuser-dell systemd[1]: mnt-mounts4bind-nfs_share.automount: Got automount request for /mnt/mounts4bind/nfs_share, triggered by 3647 (kioslave5) 
okt 17 19:07:37 myuser-dell systemd[1]: mnt-mounts4bind-nfs_share.automount: Got automount request for /mnt/mounts4bind/nfs_share, triggered by 3647 (kioslave5) 
okt 17 19:07:48 myuser-dell systemd[1]: mnt-mounts4bind-nfs_share.automount: Got automount request for /mnt/mounts4bind/nfs_share, triggered by 3647 (kioslave5)

cat /etc/systemd/system/mnt-mounts4bind-nfs_share.mount
[Unit] 
Description=nfs share 

[Mount] 
What=broken-lenovo.lan:/mnt/nfs_share 
Where=/mnt/mounts4bind/nfs_share 
Type=nfs Options=_netdev,auto 

[Install] 
WantedBy=multi-user.target

cat /etc/systemd/system/mnt-mounts4bind-nfs_share.automount
[Unit] 
Description = Automount NFS Share 
ConditionPathExists=/mnt/mounts4bind/nfs_share 

[Automount] 
Where=/mnt/mounts4bind/nfs_share 
TimeoutIdleSec=10 

[Install] 
WantedBy=multi-user.target

cat /etc/fstab | grep nfs
# nfs share 
#broken-lenovo.lan:/mnt/nfs_share /mnt/mounts4bind/nfs_share nfs rw,sync,noauto,_netdev,x-systemd.automount,x-systemd.mount-timeout=10,timeo=14,x-systemd.idle-timeout=1min 0 0

Edit: REboot hangs, not boot

There is something weird here.

What=broken-lenovo.lan:/mnt/nfs_share 
Where=/mnt/mounts4bind/nfs_share 
Type=nfs Options=_netdev,auto 

one of your issues could be your dns resolver - can you actually ping the servers hostname?
another thing is this on the client or the server?

This is something you need configure on the NFS Server. The rw in the export just means client could read and write if the filesystem permissions match. The UID of your user on the client still needs write access on the folder of the server you put in the export. If you don’t want to use the UID or GID you can use ACL, but this is a little bit more complicated.
This is a little bit different to something like samba. There is no account for the login, but the file permissions are used to determine which user can access which files. The username is not important, but the UID and GID are very important.

Yeah, ping etc. works fine both ways.

You mean a weird thing that I’m exporting the server’s /mnt/nfs_share? It is actually correct. Also the files on the server are owned by my user, and things work on the server side as expected.
My user is both UID,GID 1000 on both computers. On the client side the files in the share show as owned by root.

My long term goal is to make a Kerberos-LDAP solution on the server, but I cannot hope to defeat that task if I cannot make this easier one work…

What (maximum) version of NFS is on the server?

What is the output of the following, while it is currently mounted?

mount | grep nfs_share

According to the server’s rpcinfo -p | grep nfs I have nfs versions 3 and 4, whereas nfs_acl is on version 3 (if that means something).
$ nfsstat -m
/mnt/mounts4bind/nfs_share from broken-lenovo.lan:/mnt/nfs_share
Flags: rw,relatime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp6,timeo=600,retrans=2,sec=sys,clientaddr=fd5b:c76b:759c::eba,local_lock=none,addr=fd5b:c76b:759c::49f

Can you disable that on the server side to only use NFSv4 permissions?

Or try using the maproot=1000 (or mapall=1000) option on the server side?

It seems like the issue was related to the mount on the other PC. For some reason ls -l showed the files belonging to myuser, but upon reboot they changed to root. Adding the uid and gid explicitly on the server solved the issue. Now there is only one thing to take care of, and that is NFS stalling reboot. Is there a good practice to avoid this?

Edit: for the reboot I added this systemd unit and enabled it:

[Unit]
Description=Unmount drives before reboot
DefaultDependencies=no
Before=shutdown.target reboot.target

[Service]
ExecStart= /home/myuser/Linux/scripts/umount.sh
Type=oneshot
TimeoutStartSec=0

[Install]
WantedBy=shutdown.target reboot.target

and the referenced unmount.sh:

#!/bin/bash
umount /mnt/mounts4bind/nfs_share

Long time ago I wrote down my notes in tutorial form

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