Fsck Command in Linux

FSCK (File System Consistency Check) is a command-line utility to check and repair Linux filesystem errors. This makes sure filesystem for integrity and consistency.

fsck is a front-end program and it calls the respective program for the specific filesystem to run. Fsck usually runs after the system fails to boot, the file system becomes corrupted, or the attached drive fails to function properly.

In this tutorial, we will learn about the fsck command and how to repair filesystems on a Linux distribution.

Prerequisites

  • Linux or Unix-like system
  • A user with root access

When using fsck command

  • The system automatically finds system inconsistent usually after a system crash or power loss or after unclean unmount.
  • System fails to boot
  • When the system has I/O error
  • Schedule to run fsck for filesystem intergerity on boot or every few months

fsck command in Linux

The fsck command follows a basic syntax.

$ sudo fsck [Option] [Filesystem]

The [Option] in the syntax is the options that are available with the fsck utility (fsck option are given at end of this tutorial). The [Filesystem] can be a device, a partition, a mount point, and so on. If no credential is added to the [Filesystem], fsck checks the devices listed in the fstab file.

fsck is included by default in all Linux distributions. Having a good backup is a safe point to run the fsck command. fsck keeps all the files intact and only checks the integrity of the filesystem. This command can be run manually or automatically.

Before attempting to check or repair filesystems, always remember to unmount the partition. If this is not done, the filesystem may get be damaged.

Check and Repair Filesystem Errors

Fsck is commonly used to repair errors on corrupted ext3 or ext4 filesystems. To use the fsck utility, you must first ensure that the partition has been unmounted. You will get an error and your process will be aborted if you try to run the fsck command on partition without unmounting it. After the process is complete, you can again mount the filesystem.

If you are not sure about the device name, use df command, lsblk, or fdisk (fdisk -l) to find it.

$ sudo df -h

Use the unmount command to avoid causing any filesystem damage.

Syntax:

$ sudo umount [filesystem]

You can use the -p option along with the fsck utility to automatically repair any problems that can be safely resolved without the user's interference.

Syntax:

$ sudo fsck -p [Filesystem]

For Example:

$ sudo umount /dev/sdc
$ sudo fsck -p /dev/sdc

Output:

$ sudo fsck -p /dev/sdc
 fsck from util-linux 2.34
 exfatfsck 1.3.0
 Checking file system on /dev/sdc.
 File system version           1.0
 Sector size                 512 bytes
 Cluster size                128 KB
 Volume size                  55 GB
 Used space                    4 MB
 Available space              55 GB
 Totally 1 directories and 3 files.
 File system checking finished. No errors found.

After the check and repair make sure to mount the disk.

Repair Root File System Error

Because a root machine cannot be unmounted, Fsck cannot check for errors. However, you have you can run fsck in recovery mode.

By rebooting the machine while in Rescue mode, you can run fsck. You can run fsck to repair root system file errors using the following steps.

Enter the boot menu and select "Advanced Options" during the reboot.

Choose “Recovery mode” from the advanced options menu, and then pick “fsck” from the drop-down menu.

A message window will popup, asking if you want your / filesystem remounted. Select the option "Yes".

By selecting the option "Resume," you may now return to normal boot.

fsck Options

There is a list of options that are available with the fsck utility for specific purposes. Some of the useful fsck options are:

1. Perform fsck dry run - This performs a test run.

fsck -N /dev/sdc

2. Run non-interactively - answers yes to all questions, this will avoid all prompts

fsck -y /dev/sdc

3. Just Print fsck Error to Stdout without repair

fsck -n /dev/sdc

4. Run fsck on all filesystems

fsck -AR

The -R will skip the root filesystem as its cant be unmounted on a running machine

5. Run fsck for a specific filesystem

The fsck command is a wrapper and internally uses the respective filesystem checker command (fsck.*). You can find the following various fsck checker commands such as fsck.ext2, fsck.ext3, fsck.ext4, etc.).

# cd /sbin
# ls fsck*
 fsck  fsck.cramfs  fsck.ext2  fsck.ext3  fsck.ext4  fsck.minix  fsck.xfs

The following table shows all options of the fsck command.

OptionDescription
-ACheck all file systems present in /etc/fstab
-CDisplay progress bar
-fCheck filesystem forcefully
-lLock the device
-MDo not check mounted filesystems
-N Print output without executing any actions
-PCheck multiple filesystems parallelly
-pAutomatically repair any issues that can be safely resolved without the need for user interaction
-RDo not check the root filesystem when used with -A
-rPrint statistics for each device that has been checked
-TDo not show the Title
-tSpecify the filesystem types to be checked (This can be done using man command)
-vProvide a detailed output
-yAssume 'yes' to all the questions
fsck Options

fstab is a file that directs the operating system on how and where to mount the partitions. You may find a list of entries in the fstab file by opening it with /etc/fstab.

The <pass> option specifies the order in which file system checks are performed during a reboot. If the value is 0 then it does not check. If the value is 1, the file systems are checked one at a time, but if the value is 2, all of the file systems are searched simultaneously. The root file system has a value of 1, and all other file systems you want to check should have a value of 2.

A sample /etc/fstab file:

<filesystem>    <mount point>   <type> <options>              <dump> <pass>
 /dev/sda        /               ext4    errors=remount-ro        0      1
 /dev/sdb        none            swap    sw                       0      0
 /dev/sdc        /mnt/data       ext4    defaults,noatime,nofail  0      2

Conclusion

In this tutorial, we learned how to use the fsck command to check and repair filesystems in Linux. You can refer fsck man pages for a more complete description.

Leave a Comment