Home / Alt manpages / mountpoint(1)

  • mountpoint(1)
  • User command
  • linux

Check Linux Mountpoints Reliably with mountpoint

After this guide, you will be able to tell whether a path is a mountpoint, use the result safely in a shell script, and distinguish a failed check from an ordinary "not mounted" answer. The commands are read-only and normally need no elevated privileges. Allow about 10 minutes.

What mountpoint actually checks

mountpoint checks whether a directory or file is mentioned in /proc/self/mountinfo. That is narrower and more useful than asking whether a path merely exists. A normal directory such as /tmp can exist without being a separate mount, while a file such as a bind-mounted configuration file can be a mountpoint in its own right.

This guide follows the installed mountpoint(1) manpage, which identifies util-linux 2.39.3. The executable found first on this machine is the Linuxbrew util-linux 2.41.3 build, so check both documentation and binary version when behaviour matters on a managed system:

$ mountpoint --version
mountpoint from util-linux 2.41.3

Checkpoint

You have confirmed which executable your shell will run. If this prints a different version, keep that difference in mind when comparing output with another host.

Test a path interactively

  1. Run mountpoint with the directory or file you want to inspect.
$ mountpoint /
/ is a mountpoint
$ mountpoint /tmp
/tmp is not a mountpoint
$ mountpoint /etc/passwd
/etc/passwd is not a mountpoint

A successful check prints that the path is a mountpoint. A negative check prints that it is not. The command accepts a path, not a device name, in this form. It does not mount anything, unmount anything or change the filesystem.

Do not treat the wording as the scripting interface. The exit status is the stable part:

  • 0 means the directory or file is a mountpoint.
  • 32 means the path is not a mountpoint.
  • 1 means the invocation failed, or a permission or system error occurred.

For example, this records the result without hiding an actual error:

if mountpoint -q -- /srv/data; then
    printf '%s\n' '/srv/data is mounted'
elif [ "$?" -eq 32 ]; then
    printf '%s\n' '/srv/data is not a mountpoint'
else
    printf '%s\n' 'mountpoint check failed' >&2
    exit 1
fi

The -- ends options before the path. It is a useful habit when a path comes from a variable, especially if an unexpected value could begin with a hyphen. The second call to $? is not needed here because the test is performed directly in the elif condition and its status is still available to that condition.

Checkpoint

Verify the two normal outcomes without relying on printed text:

$ mountpoint -q -- /
$ printf '%s\n' "$?"
0
$ mountpoint -q -- /tmp
$ printf '%s\n' "$?"
32

Keep checks quiet in scripts

  1. Add -q or --quiet when the exit code is all your script needs.

Quiet mode prints nothing for either an affirmative or negative answer. It still returns the same status values. This avoids mixing diagnostic text with a service manager, monitoring probe or command substitution.

if mountpoint --quiet -- /var/lib/my-service; then
    systemctl restart my-service
else
    status=$?
    if [ "$status" -eq 32 ]; then
        printf '%s\n' 'not restarting: data path is not mounted' >&2
    else
        printf 'mountpoint returned %s\n' "$status" >&2
        exit "$status"
    fi
fi

The example shows an operational boundary: mountpoint itself is harmless, but the following systemctl restart can interrupt a service. Use it only when restarting that service is an intentional response to the check. Add sudo only to the service action if your account needs it; do not use elevated privileges merely to ask whether a path is mounted.

Show the mounted filesystem device number

  1. Use -d or --fs-devno with a path that is already a mountpoint.
$ mountpoint --fs-devno /
9:2

The output is a major:minor device number. The value depends on the host, so do not hard-code 9:2 as a universal answer. On this machine, 9:2 is the device number reported for the root mount. With a non-mountpoint, the command reports the normal negative result and returns 32:

$ mountpoint --fs-devno /tmp
/tmp is not a mountpoint
$ printf '%s\n' "$?"
32

This option tells you about the filesystem mounted on the path. It does not identify the source string shown by tools such as findmnt, and it does not prove that two mounts have the same backing storage in every storage stack. Use the number as a kernel device identifier, not as a human-readable volume label.

Check a block device with -x

  1. Use -x or --devno when the input is a block device rather than a mount path.
$ mountpoint --devno /dev/sda
8:0
$ mountpoint --devno /dev/null
mountpoint: /dev/null: not a block device
$ printf '%s\n' "$?"
32

The first device exists on the machine used for these examples, but device names differ between hosts. Replace /dev/sda with a block device that actually exists on your system. Inspecting a device is read-only; do not confuse this option with mounting it.

For automation, treat status 32 as "not a block device" in this mode, and status 1 as an invocation, permission or system error. If you need to discover device names first, use your normal inventory tooling and pass only the resulting path to this command.

  1. Choose whether the final symbolic link should be followed before deciding that a path is mounted.

By default, mountpoint follows the final symbolic link in the path. --nofollow changes that behaviour. A link to the root mount therefore gives different answers:

$ ln -s / /tmp/mountpoint-root-link
$ mountpoint /tmp/mountpoint-root-link
/tmp/mountpoint-root-link is a mountpoint
$ mountpoint --nofollow /tmp/mountpoint-root-link
/tmp/mountpoint-root-link is not a mountpoint

The temporary link changes directory metadata, so remove it after a real test with unlink /tmp/mountpoint-root-link. If the name already exists, stop and inspect it first rather than replacing it. In production checks, prefer the canonical path or use --nofollow when following an untrusted final link would give the wrong security decision.

Common traps and recovery

A status of 32 is not the same as status 1. The former is an expected negative answer; the latter needs investigation. Check that the argument is present, that the path is accessible, and that /proc/self/mountinfo is available in the process's mount namespace. Containers can see a different mount table from the host, so run the check in the namespace whose view you intend to verify.

Do not put mountpoint after a command joined with && if you need to handle 32 separately. Use an if statement as above. Do not parse the human-readable sentence when the exit status is available. Do not use -d on an ordinary directory and expect it to print the directory's own device number: it reports the device mounted at that path.

The command is a check, not a repair. If a mount is missing, stop the dependent action or follow your site's documented mount procedure. Avoid adding an ad hoc mount command to a health check: mounting can require privileges, credentials, network access or service coordination.

Done means

  • You can check a directory or file and understand the printed answer.
  • Your script uses status 0 for mounted, 32 for a normal negative answer, and 1 for an error.
  • You use -q when output would be noise.
  • You use -d for a mounted path and -x for a block device.
  • You have made the final symlink policy explicit with default following or --nofollow.