17

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

user
  • 339

2 Answers2

15

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)
zygis
  • 196
  • 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
  • 1
    To find the partition the file or directory mounts on - df -P file/goes/here | tail -1 | cut -d' ' -f 1 – brainLoop Jul 09 '19 at 06: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)}'