Read a Process Mount Namespace with /proc/pid/mountstats
You will inspect the mounts visible to one Linux process, read the stable fields in each record, and avoid confusing another process's view with your own. Allow about ten minutes. The examples are read-only and normally need no elevated privileges.
The route
Jump straight to the step you need, or tick off Done means at the end.
This guide follows the installed proc_pid_mountstats(5) page from Linux man-pages 6.7, packaged here as manpages 6.7-2, on a Linux 6.8.0-139-generic host. The file has existed since Linux 2.6.17. The installed man page describes the interface; it does not promise that every filesystem will provide optional statistics.
1. Read the current shell's mount view
Use /proc/self/mountstats when you want the mounts visible to the process doing the reading. Start with a bounded view so a long NFS statistics section or a large mount list does not take over your terminal:
$ sed -n '1,18p' /proc/self/mountstats
device sysfs mounted on /sys with fstype sysfs
device proc mounted on /proc with fstype proc
device udev mounted on /dev with fstype devtmpfs
device devpts mounted on /dev/pts with fstype devpts
device tmpfs mounted on /run with fstype tmpfs
Each ordinary record identifies the mounted device, the words mounted on, the mount point, the words with fstype, and the filesystem type. The device is reported as nodevice when there is no corresponding device. The precise list is host-specific, so do not paste the example as if it were a required default.
Checkpoint: confirm that the file is readable and that it is the proc entry you meant to inspect:
$ test -r /proc/self/mountstats && echo readable
readable
$ readlink /proc/self
12345
The number from readlink is your shell's process ID at that moment. Its value will differ on your machine.
2. Inspect a specific process
Replace PID with a process ID you own. Using the shell's own ID first makes the ownership rule obvious:
$ PID=$$
$ printf 'Inspecting PID %s\n' "$PID"
Inspecting PID 12345
$ sed -n '1,12p' "/proc/$PID/mountstats"
device sysfs mounted on /sys with fstype sysfs
device proc mounted on /proc with fstype proc
device udev mounted on /dev with fstype devtmpfs
/proc/pid/mountstats describes mounts in that process's mount namespace, not an abstract list of every mount on the host. Two processes can therefore produce different results when they run in different namespaces. A container process is a common example, but the file itself does not tell you why the namespaces differ.
Do not assume that a PID remains valid. Processes exit and PIDs can be reused. Capture the PID from the same controlled operation that you are investigating, then read the file promptly.
3. Count records without parsing optional data
If you only need a quick inventory count, count lines beginning with the documented device marker:
$ awk '$1 == "device" { count++ } END { print count }' /proc/self/mountstats
18
Your count will differ. This check deliberately ignores the optional fourth field, because its contents depend on the filesystem. The man page currently says that NFS filesystems are the ones exporting information there. Treat that section as filesystem-specific data, not as a universal column layout.
To keep a copy for comparison, redirect the read to a new file in a directory you control:
$ umask 077
$ sed -n '1,18p' /proc/self/mountstats > mountstats.sample
$ test -s mountstats.sample && echo sample-written
sample-written
This changes local file state by creating or truncating mountstats.sample. The proc file and the system mounts are not changed. If the sample is sensitive in your environment, remove it after review with rm -- mountstats.sample; that deletion is irreversible, so check the path before running it.
4. Handle access failures accurately
The file is readable only by the owner of the process. A failure for another user's process is not evidence that the mount disappeared. Check the target process owner before reaching for privilege:
$ PID=12345
$ ps -o pid=,user=,comm= -p "$PID"
12345 andy example-service
$ test -r "/proc/$PID/mountstats" && echo readable || echo 'not readable'
readable
Use a real PID from your machine, not the placeholder. If the process is not yours, an administrator may need to perform the read under the appropriate account or security policy. Do not add sudo automatically: elevated access can show a different process context and is unnecessary for your own processes. If the process exits between the two commands, repeat the check with a current PID.
A missing path can also mean that the process has gone away, that procfs is not mounted where expected, or that the target is outside the proc instance you are inspecting. Check the read-only facts first:
$ findmnt -T /proc
$ test -d "/proc/$PID" && echo 'process directory exists' || echo 'process directory missing'
findmnt is only a diagnostic here. It does not mount anything, and this guide does not ask you to change procfs or namespace configuration.
5. Keep the output in context
The first three fields are useful for a human check: device name, mount point and filesystem type. They are not a complete replacement for a mount table tool, and they do not prove that a path is usable by your application. Permissions, propagation, namespace membership and later changes can affect what a process can actually access.
For a repeatable comparison, read the same process's file twice and compare the saved files:
$ PID=$$
$ sed -n '1,18p' "/proc/$PID/mountstats" > before.mountstats
$ sed -n '1,18p' "/proc/$PID/mountstats" > after.mountstats
$ cmp --silent before.mountstats after.mountstats && echo unchanged || echo changed
The two reads are snapshots, not a lock. A mount can change between them. Remove these temporary samples when they are no longer needed, or store them with the access controls required by your operational records.
Done means
- You read
/proc/self/mountstatsor a verified PID-specific path. - You can identify the device, mount point and filesystem type in an ordinary record.
- You treated the optional statistics field as filesystem-specific, with NFS as the documented case.
- You checked process ownership and PID lifetime before diagnosing an access failure.
- You kept the process's mount namespace in view and made no mount or namespace changes.