run fsck to fix file system

How to Run Fsck to Fix File System Error in Linux

Sometimes your Linux filesystem may become corrupt or inaccessible due to frequent use or some other reason. In such cases, it is advisable to use tool like fsck to check system integrity. In this article, we will learn how to run fsck to fix file system error in Linux. You can run fsck automatically during boot time or do it manually.


How to Run Fsck to Fix File System Error in Linux

You should run fsck in case your system is not booting properly, some of the files on the disk have become corrupt, or one of the attached drives is not working properly. You need to run fsck as root user or user with sudo privileges. It provides tons of useful options to easily check data integrity of different parts of your disk. Here are some of the commonly used options.

  • -A – Used for checking all filesystems
  • -C – Show progress bar
  • -l – Locks the device to guarantee no other program will try to use the partition during the check.
  • -M – Do not check mounted filesystems
  • -r – Provide statistics for each device that is being checked.
  • -T – Does not show the title
  • -N – Only show what would be done – no actual changes are made.
  • -P – If you want to check filesystems in parallel, including root.
  • -R – Do not check root filesystem
  • -t – Exclusively specify the filesystem types to be checked
  • -V – Provide description what is being done.


How to Run Fsck to Repair Linux File Systems Errors

You can run fsck on unmounted partitions & devices only. Let us say you need to check integrity of /dev/sdb, then here is the command for it.

# fsck /dev/sdb

When you run the above command, if you get an error message “cannot continue, aborting” it means the device is not unmounted. In such cases, unmount the device first.

# umount /dev/sdb

After this command, you can run the above fsck command.

When you run fsck, it provides an exit code in output. Here are the most common exit codes and their meaning.

0      No errors
1      Filesystem errors corrected
2      System should be rebooted
4      Filesystem errors left uncorrected
8      Operational error
16     Usage or syntax error
32     Checking canceled by user request
128    Shared-library error        


How to Repair Linux Filesystems with fsck

Fsck can not only list different errors on your filesystem, but also attempt to automatically fix it. For this purpose, you need to use -y option (for yes to prompt).

# fsck -y /dev/sdb

If you want fsck to automatically attempt repairing all filesystems, run the following command.

# fsck -AR -y 


How to Run Fsck on Root Partition

By default, you can run fsck only on unmounted partitions. But how to run fsck on root partition that is always mounted. In this case, there are two options:

  • Force fsck upon system boot
  • Run fsck in rescue mode

We will look at both these options below.


Force fsck upton system boot

In order to run fsck on system boot, you need to create a file forcefsck in root partition of your system.

# touch /forcefsck

Then you just need to reboot your system. On next reboot, fsck will automatically run to check root partition integrity. But please remember, that if there are many inodes on your root partition, running fsck will take some time. So please plan for some downtime beforehand.

After the system has booted, remove the above file to avoid running fsck on every system boot.

# rm /forcefsck


Run Fsck in Rescue Mode

In this case, before you reboot your system, stop all critical services such as databases and servers. Then reboot your system.

# reboot

During system reboot, hold down shift key to view grub menu. Select Advanced Options from grub menu.

Then choose Recovery mode.

On the next page, select fsck

You will see the following prompt that asks if you want your root partition to be re-mounted. Select Yes.

You will see the following screen that displays report of fsck progress. Press Enter to proceed further.

On the next page, select resume option to resume normal boot.

In this article, we have learnt how to use fsck to check disk integrity in Linux.

Also read:

How to Increase Open File Limit in Linux
How to Count Total Lines of Code in Directory
How to Fix Yum Error: Database Disk Image Malformed
How to Fix Failed to Mount /etc/fstab
How to Compress Images in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *