How to fix "sudo: unable to open ... Read-only file system"?

1,121,246

Solution 1

The filesystem will usually go into read-only while the system is running if there is a filesystem consistency issue. This is specified in fstab as errors=remount-ro and will occur when a FS access fails or an emergency read-only remount is requested via Alt+SysRq+U. You can run:

sudo fsck -Af -M

to force a check of all filesystems. As one of the other answers states, looking at dmesg is also very helpful.

Edit: Don't forget the -M on the command-line.

NOTE: As mentioned by Bibhas in his answer: If fsck gets stuck after its version banner:

$ sudo fsck -Af -M
fsck from util-linux 2.20.1

you may want to try using the EXT4-specific fsck

$ sudo fsck.ext4 -f /dev/sda1

Provided the partition in question /dev/sda1 was an ext4 filesystem.

Solution 2

The answer by hexafraction didn't work for me. Every time I tried executing sudo fsck -Af -M it just showed

$ sudo fsck -Af -M
fsck from util-linux 2.20.1

and nothing else. No error or anything. For me, booting into a live disc and executing this worked -

sudo fsck.ext4 -f /dev/sda1

Provided the partition in question /dev/sda1 was an ext4 filesystem.

Solution 3

Here is the command that solved my problem :

mount -o remount /

better than a reboot or sudo fsck -Af

Solution 4

If you want to force your root filesystem to remount as rw, you can do the following.

mount -o remount,rw /

Solution 5

Try running dmesg | grep "EXT4-fs error" to see if you have any issues related to the filesystem / journaling system itself. I would recommend you to restart your system, then. Also, sudo fsck -Af answer by ObsessiveSSOℲ won't hurt.

Share:
1,121,246

Related videos on Youtube

John
Author by

John

Updated on September 18, 2022

Comments

  • John
    John over 1 year

    The title might not be as descriptive as I would like it to be but couldn't come up with a better one.

    My server's file system went into Read-only. And I don't understand why it does so and how to solve it.

    I can SSH into the server and when trying to start apache2 for example I get the following :

    username@srv1:~$ sudo service apache2 start
    [sudo] password for username:
    sudo: unable to open /var/lib/sudo/username/1: Read-only file system
     * Starting web server apache2                                                                                                                                                                                                               (30)Read-only file system: apache2: could not open error log file /var/log/apache2/error.log.
    Unable to open logs
    Action 'start' failed.
    The Apache error log may have more information.
    

    When I try restarting the server I get :

    username@srv1:~$ sudo shutdown -r now
    [sudo] password for username:
    sudo: unable to open /var/lib/sudo/username/1: Read-only file system
    

    Once I restart it manually it just start up without any warning or message saying something is wrong.

    I hope somebody could point me into the right direction to resolve this issue.

    • Forty-Two
      Forty-Two over 9 years
      I recommend to @John to change the answer to the last posted answer by Bibhas as it actually works where the other answers are not helpful at all actually.
    • Ciro Santilli OurBigBook.com
      Ciro Santilli OurBigBook.com over 7 years
    • vanduc1102
      vanduc1102 almost 6 years
      Everyone, this question is for a server, not a PC. If you have this issue on your dual boot PC, Please check The **quick start** option can be found in **power options** in the control panel of Windows. I'm having the same issue with an Ubuntu on AWS
    • Parisa.H.R
      Parisa.H.R over 2 years
      I have this problem too, and non of solutions work fo me i use ubuntu 18.04 , and i don't know what makes this happened , I should restart my system and then it show me page that contain (initramfs) there when I run fsck /dev/sda1 -y and reboot os work and again after 30 minutes to 1 hour problem happens.
    • Admin
      Admin almost 2 years
      if disk is NTFS and used by Windows - run from it shutdown /f /r /t 0, it helped for me
  • Drew Noakes
    Drew Noakes about 11 years
    Not better if the OS made your disk read-only to prevent possible corruption.
  • isomorphismes
    isomorphismes about 11 years
    What should one look at / grep for in dmesg output?
  • gertvdijk
    gertvdijk almost 11 years
    I think you should not force a filesystem check on other r/w mounted filesystems. That will potentially corrupt your data. Add the -M option to skip mounted filesystems. (-M Do not check mounted filesystems and return an exit code of 0 for mounted filesystems. from FSCK(8))
  • Nathan Osman
    Nathan Osman over 10 years
    Not only that - only root will be able to remount the filesystem and sudo isn't going to work if the filesystem is read-only.
  • Nathan Osman
    Nathan Osman over 10 years
    Unfortunately if you opted to encrypt your home folder, dmesg is bound to be full of useless errors from ecryptfs.
  • Mark Kramer
    Mark Kramer over 10 years
    This doesn't help, I just get the same error when trying to run that command. sudo: unable to open /var/lib/sudo/kuplack/1: Read-only file system fsck from util-linux 2.20.1
  • Mark Kramer
    Mark Kramer over 10 years
    Doesn't help, I just get the same message: sudo: unable to open /var/lib/sudo/kuplack/1: Read-only file system mount: cannot remount block device /dev/sda2 read-write, is write-protected
  • Forty-Two
    Forty-Two over 9 years
    Thank you for this answer. This is the only thing that had actually worked
  • naught101
    naught101 about 9 years
    But... -M will skip mounted file systems, including /dev/sda1 (or what ever mounted drive includes /var/lib/sudo/... ). @MarkKramer: I highly recommend always setting the root password when installing linux, with sudo su; passwd. Then you can use su -c fsck -Af -M.
  • dalore
    dalore about 9 years
    -M means not to do mounted filesystems. Your filesystem /dev/sda1 was mounted (I'm guessing at /). So it was skipped.
  • R T
    R T over 8 years
    o yes this one worked.
  • David Foerster
    David Foerster about 8 years
    Welcome to Ask Ubuntu! I recommend editing this answer to expand it with specific details about how to do this. (See also How do I write a good answer? for general advice about what sorts of answers are considered most valuable on Ask Ubuntu.)
  • FreeSoftwareServers
    FreeSoftwareServers almost 8 years
    Yea for me I needed to remove -M since /dev/sda1 was mounted, and to make your life easier, add -Afy (The y means answer yes to all prompts)... I play fast and loose with VM's so I'm usually ok with this type of solution, but if this is unbacked up hardware, might take a different approach and read dmesg.
  • efkan
    efkan about 7 years
    sudo fsck.ext4 -f /dev/sda1 works. Still a restart needs.
  • Darshan Chaudhary
    Darshan Chaudhary about 7 years
    running fsck on a mounted filesystem is not a good idea
  • nanofarad
    nanofarad about 7 years
    @DarshanChaudhary The -M flag causes fsck to skip mounted filesystems. See the fsck (8) manpage.
  • Sinan Gül
    Sinan Gül about 7 years
    command succeed after reboot the system.
  • Eduardo Lucio
    Eduardo Lucio over 6 years
    UP! For -> "The filesystem will usually go into read-only while the system is running if there is a filesystem consistency issue."
  • Rosário Pereira Fernandes
    Rosário Pereira Fernandes over 6 years
    It worked for me on ubuntu 16.04.3 Thank you
  • nomen
    nomen over 6 years
    I am fairly certain this is happening to me right now, given that my computer told me it forgot its time this morning.
  • Rishabh Agrahari
    Rishabh Agrahari almost 6 years
    mount: cannot remount /dev/sda8 read-write, is write-protected
  • Fathur Rohim
    Fathur Rohim about 5 years
    this was solved my problem, I use hetzner cloud with 160gb ssd, never had such error before
  • birdman
    birdman almost 5 years
    This answer does not work for me. After executing the suggested command I receive fsck from util-linux 2.29.2
  • Blairg23
    Blairg23 almost 5 years
    This actually did end up corrupting my filesystem :(
  • NelsonGon
    NelsonGon almost 5 years
    Only one that worked. At the cost of startup speed.
  • Pranav
    Pranav almost 5 years
    @hexafraction many humans don't have "SysRq" keyboard buttom in their laptop keyboard, I am one of them. How do I pull this off?
  • nanofarad
    nanofarad almost 5 years
    @PrabeshBhattarai SysRq is the same (physical) key as PrintScreen, even if your laptop manufacturer didn't physically print "SysRq" on it; alternatively you may need Fn+[something]. That said, you don't need to press that key combination; I mention that keyboard shortcut as an example of something that can cause a filesystem to fail read-only, not as a step that you need to take.
  • ScottyBlades
    ScottyBlades over 4 years
    sudo fsck -Af -M gives me: fsck: illegal option -- A fsck: ? option?
  • ScottyBlades
    ScottyBlades over 4 years
    fsck from util-linux 2.20.1 gives me: fsck usage: fsck [-fdnypqL] [-l number]
  • ScottyBlades
    ScottyBlades over 4 years
    sudo fsck.ext4 -f /dev/sda1 gives me: sudo: fsck.ext4: command not found
  • ScottyBlades
    ScottyBlades over 4 years
    mount_apfs: unrecognized option 'remount'
  • nanofarad
    nanofarad over 4 years
    @ScottyBlades This answer is seven years old, and the flags have likely changed--I don;t have a copy of fsck from util-linux 2.20.1, and my copy handles -A just fine, so I don't have any great advice, aside from manually invoking on each volume that needs to be checked.
  • ScottyBlades
    ScottyBlades over 4 years
    @hexafraction, it's cool. I'm new to web dev. So I think I'm making more obvious/basic mistakes that would go under most seasoned developer's radars. MY SOLUTION: When I clicked "save as" I was saving to the root directory, not the directory I actually wanted to save in. Making sure I saved to the correct location solved the problem.
  • nanofarad
    nanofarad over 4 years
    @ScottyBlades Understood. Looks like your situation was mostly unrelated to actual file system corruption then, meaning that there was no need to get fsck involved.
  • ScottyBlades
    ScottyBlades over 4 years
    @hexafraction, good to know
  • Farid Chowdhury
    Farid Chowdhury almost 4 years
    Awesome brother 💪
  • Forty3
    Forty3 almost 4 years
    Here we are, 2020, and this saved my Ubuntu laptop. Thanks @Bibhas!
  • Salem F
    Salem F over 3 years
    Im on virtual machine (KVM VPS) and I got this error mount: /: cannot remount /dev/mapper/vg_jh-root read-write, is write-protected.
  • Coconut
    Coconut over 3 years
    If someone needs to remount the disk in read/write mode, try this: $ sudo mount -vo remount,rw /
  • Òscar Raya
    Òscar Raya over 3 years
    TY, I had a problem with snappy daemon and tryied a lots of things. Your sudo fsck.ext4 -f /dev/sda1 saved me (in my case on sda2).
  • Muhammad bin Yusrat
    Muhammad bin Yusrat over 3 years
    Thanks! This worked! :)
  • Heisenberg
    Heisenberg over 3 years
    Didn't worked but I think windows is causing this issue
  • Sarato
    Sarato about 3 years
    This worked for me, when my off-brand mp3 player switched to "read-only" all of a sudden
  • Bhuwan Adhikari
    Bhuwan Adhikari almost 3 years
    this was kind of similar for me too.
  • Yinon_90
    Yinon_90 almost 3 years
    I solve my issue in a similar way, I posted here: askubuntu.com/a/1346172/1330220
  • Mark E
    Mark E almost 3 years
    ¡CAUTION! I was fixing a remote server and it didn't turn on after reboot which made things way harder, I recommend testing other solutions before trying to rebbot specially if you're not physically with the computer. For the first time in the engineering world, turning it off and on again was a big problem instead of a solution :(
  • flm
    flm over 2 years
    I get mount: /: cannot remount /dev/sda2 read-write, is write-protected.
  • Aleks
    Aleks over 2 years
    Oh, this is perfect.
  • Zaman Oof
    Zaman Oof over 2 years
    @MarkE Rebooting Against Availability so should be the latest solution
  • lightonphiri
    lightonphiri over 2 years
    Thank you for this: I urgently need to pull files from my problematic installation and this helped tremendously! Incidentally, I ran fsck.ext4 via my "(Initramfs)" shell and it worked perfectly!
  • kyriakosSt
    kyriakosSt about 2 years
    This answer should have more upvotes. It is the most failsafe option, that requires far less understanding from the user. Thank you.
  • Fathy
    Fathy about 2 years
    this solved my problem i remount the partition only aka sudo mount -o remount, rw 0A4819AC48393