When a Linux server experiences a sudden power failure or a hard crash, files that were actively being written to the disk can become corrupted, leaving the filesystem in an inconsistent state. This can cause the system to fail to boot, throw input/output errors, or mount the drive as “read-only” to protect the remaining data. To diagnose and repair these filesystem inconsistencies, Linux administrators rely on the fsck (file system consistency check) command.
The Golden Rule of fsck
Never run fsck on a mounted filesystem. Running this repair tool while the operating system is actively reading and writing to the disk will cause catastrophic, irreversible data corruption.
You must unmount the partition before checking it. For example, if you want to repair a secondary drive mounted at /mnt/data (which is physically /dev/sdb1), you must unmount it first:
sudo umount /dev/sdb1
Note: If you need to repair your primary root partition (/), you cannot unmount it while the system is running. You must either boot the server from a Live USB/CD to run the repair, or force fsck to run automatically during the next reboot.
How to Check and Repair a Filesystem
Once the target partition is safely unmounted, you can run the basic consistency check by pointing the command at the physical device path.
sudo fsck /dev/sdb1
The utility will scan the filesystem for errors, orphaned inodes, and corrupt blocks. If it finds an issue, the command will pause and ask you if you want to repair it (y/n).
Automating the Repair Process
If the filesystem is heavily corrupted, fsck might find hundreds of errors. Pressing ‘y’ and hitting Enter for every single error is incredibly tedious and time-consuming. You can force the command to automatically attempt to repair any error it encounters by adding the -y (yes) flag.
sudo fsck -y /dev/sdb1
The command will now run from start to finish without any human intervention, applying the safest possible fix to every inconsistency it discovers.
Forcing a Check on the Next Reboot
If your server crashed and you suspect the primary root filesystem (/dev/sda1) is corrupted, you cannot run fsck right now because the drive is actively in use. However, you can instruct the Linux kernel to automatically run a full fsck scan during the boot sequence, before the filesystem is officially mounted.
To do this, you simply create an empty file named forcefsck in the absolute root directory of the drive:
sudo touch /forcefsck
Now, type sudo reboot. During the boot process, the system will detect this file, pause the boot sequence, run a comprehensive fsck repair on the root drive, delete the /forcefsck file, and then boot normally.