12

Currently, I'm using grep '\s/location/\s.*noexec' /proc/mounts.
(Is that a right way of checking this?)

  • 1
    That's fine as long as you only need to work on newer versions of Linux. – jordanm Sep 17 '13 at 15:03
  • 1
    Check this Q/A – dawud Sep 17 '13 at 15:11
12

You should use the mount(8) command, which is available out of the box on all Linux and UNIX systems.

If you run mount without any additional arguments, it will list all the currently mounted partitions on your system, file system type and any mount options, such as noexec, rw, or nosuid.

Eg:

% mount
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
/dev/sda1 on /boot type ext4 (rw,relatime,data=ordered)
/dev/mapper/basement-root on / type ext4 (rw,relatime,data=ordered)
  • 5
    On Linux, I recommend using /proc/mounts in preference to mount. If /etc/mtab is not updated (e.g. because / is read-only), the output of mount may not be up-to-date. Also, for some options (not noexec), mount gives you filtered output which can be misleading for some combinations of versions of the kernel and mount (e.g. with the atime-related options). – Gilles 'SO- stop being evil' Sep 17 '13 at 22:34
  • To find the partition the file or directory mounts on - df -P file/goes/here | tail -1 | cut -d' ' -f 1 – brainLoop Jul 9 at 6:13
4

Assuming that you're running this on Linux, yes, this is fine. It would be a little more robust to check that noexec is between commas or at the beginning or end of its column.

grep -Eq '^[^ ]+ /location [^ ]+ ([^ ]*,)?noexec[, ]' /proc/mounts

This might be clearer in awk:

awk -v location="/location" '$2 == location {exit(!($4 ~ /(^|,)noexec($|,)/))} END {exit(2)}'

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.