System A (Ubuntu server 18.04 LTS) exports a directory for NFS mounting by systems B, C, or D. On A (the server) is there a way to tell whether any of the other systems currently has the directory mounted?

The objective is to avoid shutting down A if any of B, C, or D have mounted the exported directory.

An automated (shell scripted) means of doing so would be ideal, but manual is OK.


There is no direct NFS utils (provided by nfs-utils) to list the clients connected to NFS server (mounts exported directories).

However, if NFSv4 is used, on the NFS server side clients can easily be identified using ss or netstat because it uses only 1 port 2049 for both UDP and TCP:


netstat -naptule | grep :2049

root@n54l:~# exportfs -rav

root@n54l:~# netstat -naptule | grep :2049
tcp        0      0  *               LISTEN      0          32620      -                 
tcp        0      0       ESTABLISHED 0          94689      -                 
tcp6       0      0 :::2049                 :::*                    LISTEN      0          32631      -                 

ss -tuna | grep :2049

root@n54l:~# ss -tuna | grep :2049
tcp   LISTEN     0      64        *
tcp   ESTAB      0      0 
tcp   LISTEN     0      64                   [::]:2049               [::]:*

We can see the NFS server, there was 1 client connected to it.

Combination of either command with text processing (grep, cut, awk, sed, etc.) can assemble a shell script to achieve what you want at ease.

NOTE: nfsstat and nfsiostat (client side) may provide some insight but the results/stats are not direct.

| improve this answer | |

From the No Good Deed Goes Unpunished Department, here is a simple script which accomplishes my mission. Thanks again to Terry Wang. The script which calls this enters a perpetual "do" loop which attempts shutown and sleeps for a few minutes.


    # Check for open Samba share.
    # All shares are named "share" something,
    # so grep for "share" is usable

    smbstatus | grep -i share > /dev/nul

    # Check for open NFS mount.
    # Grep for port 2049, then
    # grep that for "ESTAB"

    netstat -naptule | grep :2049 | grep ESTAB > /dev/nul

    # If either came back zero, something is active.

    if [[ $samba != 0 && $nfs != 0 ]]; then
       shutdown -h now
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.