53

Is there an easy way to determine if a mounted filesystem is mounted as Read-Only or Read-Write? I was thinking just to pipe mount but I thought there might be an easier way.

2
  • 1
    The ideal system call for this would be statvfs which among other things returns a flag fields with a flag indicating a read only mount. Unfortunately I don't know a shell command to do this directly. I would have used stat -f, but that command shows everything except flags.
    – kasperd
    Apr 13, 2016 at 6:59
  • 2
    Note: as @Travis Campbell helpfully explains in his comment on serverfault.com/a/277449/236916, mount doesn't always tell you what you want: to paraphrase, it reads from /etc/mtab, which is a cached version of the data, and may be outdated in some cases. The info in /proc/mounts is what you really want.
    – mwfearnley
    May 23, 2016 at 11:31

9 Answers 9

74

This little one-liner will pop-out something if a ro file system exists.

grep "[[:space:]]ro[[:space:],]" /proc/mounts 

Assuming you don't usually have a ro file system like a CD in the drive, it is sufficient for some basic monitoring type stuff and doesn't require changing the file system to find the current state. It also doesn't assume your file system type. Pipe it into grep -v iso9660 if you want to keep your CDs out of the record.

5
  • 2
    I had to use grep -P "\sro[\s,]" /proc/mounts or grep " ro[ ,]" /proc/mounts Apr 7, 2015 at 10:11
  • 4
    This is a much better answer than "try and create a file" thankyou.
    – Vagnerr
    Feb 11, 2016 at 10:26
  • Doesn't this only tell you the options that were used to mount, not the current status? E.g. an entry with the options ext4 rw,noatime,nobarrier,errors=remount-ro,data=ordered 0 0 is set to remount as read only in the event of an error, so without checking mount you don't know if that remount has occurred, hence if it is in fact currently in ro.
    – Walf
    Apr 28, 2016 at 1:58
  • That has not been my experience.
    – flickerfly
    Apr 28, 2016 at 2:05
  • 4
    I just had one today. This is the root file-system in ro, but it was rw when it started. $ grep "\sro[\s,]" /proc/mounts Output: /dev/mapper/root / ext4 ro,relatime,errors=remount-ro,user_xattr,acl,barrier=1,data=ordered 0 0
    – flickerfly
    Apr 28, 2016 at 17:22
15

Old question, but I've came across it looking for same help and seems like found even easier way without the need to create file.

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

Of course, root-ro is ro mounted fs and root-rw is rw fs.

3
  • 4
    That seems to test filesystem permission, but not mount status. Oct 29, 2014 at 14:13
  • 2
    True, but it's very simple and can work for some cases (like mine).
    – Yajo
    Feb 25, 2015 at 11:51
  • From man dash for the -w option - 'The file is not writable on a read-only file system even if this test indicates true.' AFAIK this is the same for other shells.
    – Graeme
    Jun 26, 2015 at 9:21
9

If the file system is mounted, I'd cd to a temporary directory and attempt to create a file. The return code will tell you if the file system is Read-Only or Read-Write provided that the file system is not full (thanks Willem).

5
  • 2
    If you are just checking to see how a filesytem is mounted, getting the output from mount should be enough. But I have to agree, this is a more exhaustive way to check. There are occasions mount can report that it is mounted read/write, but is actually read-only. A common example of this is a large number of SCSI errors on a device causing it to protect itself by going read-only. Creating a file will verify read+write/read-only without a doubt.
    – Alex
    Oct 22, 2010 at 20:33
  • 1
    this would be tidy: touch afile && { rm afile; echo "read-write"; } || echo "read-only" Jun 6, 2011 at 14:59
  • 1
    The scriptlet as written has a race condition. I would use FILE=mktemp -p /filesystem/of/interest/ instead of just using 'afile' to generate the file and filename. best Jun 6, 2011 at 21:22
  • 1
    This will incorrectly report a full filesystem as read-only.
    – Willem
    Jan 7, 2015 at 14:20
  • @David, This seems like a stopgap hack instead of a true solution.
    – Pacerier
    Apr 23, 2015 at 16:08
5

I just had this issue and these are real pastes ...

Take a look at /proc/mounts -

egrep " ro,|,ro " /proc/mounts 
/dev/sda3 / ext4 ro,seclabel,relatime,barrier=1,data=ordered 0 0    
/dev/sda5 /var ext4 ro,seclabel,relatime,barrier=1,data=ordered 0 0

FYI - These two partitions show as being mounted rw when just using the mount command.

1
  • 2
    This will not work if 'ro' is the only mount option
    – Willem
    Jun 15, 2015 at 11:20
4

Based on a flickerfly's answer, influenced by a comment from WhiteKnight

Create a detector function the fly.

eval "function is_readonly () {
          $( grep -P "\sro[\s,]" /proc/mounts | awk '{print "if echo $1 | grep -q \""$2"\"; then return 0;fi"}' )
      return 1;}";    

use it to determine if a path is on a read only fs

is_readonly /path/to/file/on/read/only/fs && echo "sorry. can't delete that"

And dispose of it when done

#dump temp function
unset -f is_readonly;
1
  • It is a lot of fun to circle back here years later and see the iterative improvements made on a simple answer. Flickerfly -> WhiteKnight -> unsynchronized This internet thing might really take off.
    – flickerfly
    Sep 30, 2021 at 14:32
3

Here is my solution:

if findmnt -n -o OPTIONS ${YOUR_MOUNT_POINT} | egrep "^ro,|,ro,|,ro$"; then
  echo "Read only!"
fi
1

For example, to check if the root partition is in Read-Only mode:

if [[ ! -z `mount | grep "on / type ext3 (ro,"` ]]
then
   echo "It's in read-only mode"
fi
2
  • 2
    This doesn't catch all cases. /sbin/mount will look at /etc/mtab for the cached version of the currently mounted filesystems (and their current options). If / manages to remount ro for some reason, mtab may not be updated correctly, so / may appear rw still. /proc/mounts should always show the correct value though. Jan 11, 2012 at 18:15
  • 1
    I agree with the need to use /proc/mounts. I think this test should be reduced to a shell (bash since the OP asks that) function that makes sure the string being referenced is not a substring of another path.
    – Skaperen
    Aug 17, 2012 at 4:46
0

Similar to Antonio, you can use /proc/mounts to do the same thing. Use your own drive in place of sda4.

cat /proc/mounts | grep /dev/sda4 | awk '{print substr($4,1,2)}'

1
  • Just so you know : awk '$0 ~ "/dev/sda4" {print substr($4,1,2)}' /proc/mounts, no need cat if you use grep (or sed), and no need grep when you use awk
    – Zelnes
    Jun 10, 2020 at 16:06
0

Ubuntu has moved to snap packages which returns a lot of /snap/$package results. So my updated command is the following:

grep "[[:space:]]ro[[:space:],]" /proc/mounts | grep -v /snap | grep -v tmpfs

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .