1. Overview

When working with a Linux system, it’s crucial to know the read/write permissions of a filesystem. Checking these permissions is essential for managing the files effectively.

In this tutorial, we’ll explore various methods to determine if a filesystem is read-only or read-write. First, we’ll discuss the findmnt and mount utilities to check for the file system permissions. Then, we’ll explore the /proc/mounts as well /etc/fstab files.

Finally, we’ll tackle the -w file test operator and create a temporary file to determine the file system permissions.

2. findmnt

The findmnt utility retrieves detailed information about mounted filesystems. By default, it’s installed on most Linux distributions as it’s part of the util-linux package.

Here’s the general syntax for finding mount options:

$ findmnt -n -o OPTIONS <mount_point>

Let’s break this down:

  • -n disables the column header in the output
  • -o specifies the output format, which in our case is only OPTIONS
  • <mount_point> is the path on which the file system is mounted

As an example, we’ll check the mount options of /:

$ findmnt -n -o OPTIONS /

Similarly, it prints ro for read-only file systems:

$ findmnt -n -o OPTIONS /dev/loop0

3. mount

mount provides us with information about mounted filesystems, including their read-only (ro) or read-write (rw) permissions. It’s installed on most Linux distributions, so it should be readily available.

We can list the mounted file systems by just typing mount:

$ mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
tmpfs on /run type tmpfs (rw,nosuid,nodev,noexec,relatime,size=399108k,mode=755,inode64)
/dev/vda2 on / type ext4 (rw,relatime)

Here, we’re interested in our drive, which is /dev/vda mounted on /. In the parentheses, we can see the options that were used to mount this file system. In this case, it’s rw, which signifies that we have read-write permission enabled on it.

Moreover, we can narrow the result to a specific file system through grep:

$ mount | grep /dev/vda
/dev/vda2 on / type ext4 (rw,relatime)

4. /proc/mounts

The /proc/mounts special file contains a comprehensive overview of the system’s mounted filesystems. It includes mount points, device names, and mount options.

As an example, we’ll print out the file system entries that are mounted as read-only (ro):

$ grep "[[:space:]]ro[[:space:],]" /proc/mounts
ramfs /run/credentials/systemd-sysusers.service ramfs ro,nosuid,nodev,noexec,relatime,mode=700 0 0
ramfs /run/credentials/systemd-tmpfiles-setup-dev.service ro,nosuid,nodev,noexec,relatime,mode=700 0 0
/dev/loop0 /snap/code/131 squashfs ro,nodev,relatime,errors=continue,threads=single 0 0

Here, we printed the lines that contain the string “ro” surrounded by whitespace or commas.

In addition, we can also make a function that makes this process a bit easier:

function is_readonly() {
  grep "[[:space:]]ro[[:space:],]" /proc/mounts

We can put this function inside our shell profile, so we don’t have to type it every time.

5. /etc/fstab

The /etc/fstab file contains file systems to be mounted during system startup. It contains useful information about file system permissions. 

For example, let’s examine the details for our core disk:

$ grep /dev/vda /etc/fstab
/dev/vda / ext4 defaults 0 1

We can see that the file system mounts on / with defaults options. It implies a standard set of mount options that are typically suitable for most situations. Therefore, defaults include the rwsuid, and exec permissions.

On the other hand, we might also come across entries containing specific file permissions:

$ grep /dev/vdb /etc/fstab
/dev/vdb /mnt/fdd   ext4   defaults,noexec   0   2

Here, the defaults option specifies the exec permission. However, the noexec option negates that. The noexec option ensures that no executable files can be run from /mnt/fdd.

6. The -w Test Operator

In some cases, we might need to check the permissions of a file system for the current user ($USER). For that purpose, we can use the –w test operator in the shell.

The -w operator allows us to verify if a specific directory or file is writable. Let’s take a look at this example:

$ [ -w / ] && echo "rw" || "ro"

We can see that we only have read permission on /. If we use another mount point that we have write access to, it should print “rw“:

$ [ -w /mnt/fdd ] && echo "rw" || echo "ro"

Notably, the mount point for a pen drive has read-write permission for the $USER.

7. Touch a Temporary File

Alternatively, we can create a temporary file in a file system we’re checking the permissions for. It can be an effective way to check if a filesystem is read-only or read-write.

By navigating to a temporary directory and attempting to create a file, we can analyze the return code to determine the filesystem’s permissions:

$ touch /mnt/fdd/temp.txt
$ echo $?

Similarly, if we were to create a file on a read-only file system, it would print an error:

$ touch /temp.txt
touch: cannot touch 'temp.txt': Permission denied
$ echo $?

However, there’s one problem with this command. We’re creating the file as a regular non-root user. If we have root access, we can easily create the file in /, so that would mean that the file system is mounted as “rw” but for users with root access. 

Therefore, if we really want to be sure whether the file-system is rw, we need to have either root access to create the file or check the permissions for the given directory:

$ sudo touch /temp.txt
$ echo $?

8. Conclusion

In this article, we learned how we can check for the permissions of a file system. We discussed techniques like using findmnt and mount utilities, examining the /proc/mounts and /etc/fstab files, utilizing the -w test operator, and creating temporary files to assess permissions.

Comments are closed on this article!