Fsck Command in Linux (Repair File System)

Posted 

4 min read

fsck (file system check) is a command-line utility that allows you to perform consistency checks and interactive repairs on one or more Linux file systems. It uses programs specific to the type of the file system it checks.

You can use the fsck command to repair corrupted file systems in situations where the system fails to boot, or a partition cannot be mounted.

In this article, we will talk about the fsck command.

How to Use fsck

The fsck command takes the following general form:

fsck [OPTIONS] [FILESYSTEM]

Only root or users with sudo privileges can clear the buffer.

When no FILESYSTEM is provided as an argument, fsck checks the devices listed in the fstab file.

Never run fsck on mounted partitions as it may damage the file system. Before attempting to check or repair file systems always unmount it first.

The fsck command is a wrapper for the various Linux filesystem checkers (fsck.*) and accepts different options depending on the file system's type.

Check the manual pages for more information about a specific checker. For example, to view the options available for fsck.ext4, type:

man fsck.ext4

Repair Corrupted File System

The simplest use case of the fsck command is to repair a non-root corrupted ext3 or ext4 file system.

  1. If you don't know the device name, use fdisk, df, or any other tool to find it.

  2. Unmount the device:

    sudo umount /dev/sdc1
  3. Run fsck to repair the file system:

    sudo fsck -p /dev/sdc1

    The -p option tells fsck to automatically repair any problems that can be safely fixed without user intervention.

  4. Once the file system is repaired, mount the partition:

    sudo mount /dev/sdc1

Repair Root File System

fsck cannot check the root file system on a running machine because it cannot be unmounted.

If you want to check or repair the root file system, you have several options at your disposal. You can set the fsck to run on boot, boot the system in recovery mode, or use a live CD.

To run fsck in recovery mode:

  1. Enter the boot menu and choose Advanced Options
  2. Select the Recovery mode and then “fsck”.
  3. When prompted to remount the root file system choose “Yes”.
  4. Once done, resume the normal boot.

To run fsck from a live distribution:

  1. Boot the live distribution.

  2. Use fdisk or parted to find the root partition name.

  3. Open the terminal and run:

    sudo fsck -p /dev/sda1
  4. Once done, reboot the live distribution and boot your system.

Check File Systems on Boot

On most Linux distributions, fsck runs at boot time if a file system is marked as dirty or after a certain number of boots or time.

To see the current mount count, check frequency number, check interval, and the time of the last check for a specific partition, use the tune2fs tool:

sudo tune2fs -l /dev/sdc1 | grep -i 'last checked\|mount count'
Mount count:              292
Maximum mount count:      -1
Last checked:             Tue Jul 24 11:10:07 2018
Check interval:           0 (<none>)
  • “Maximum mount count” is the number of mounts after which the filesystem will be checked. The value of 0 or -1 means that fsck will never run.
  • “Check interval” is the maximal time between two filesystem checks.

If for example, you want to run fsck after every 25 boots (mounts), type:

sudo tune2fs -c 25 /dev/sdc1

You can also set the maximal time between two checks. For example, to set it one month you would run:

sudo tune2fs -i 1m /dev/sdc1

To force fsck to run at boot time on SystemD distributions pass the following kernel boot parameters:

fsck.mode=force
fsck.repair=yes

On older distributions fsck will run on boot if the /forcefsck file is present:

sudo touch /forcefsck

fstab Options

fstab is a configuration file that tells the system how and where to mount the partitions.

The /etc/fstab file contains a list of entries in the following form:

/etc/fstab
# [File System] [Mount Point] [File System Type] [Options] [Dump] [PASS]
/dev/sda1       /             ext4               defaults  0      1
/dev/sda2       /home         ext4               defaults  0      2
server:/dir     /media/nfs    nfs                defaults  0      0

The last, 6th column ([PASS]) is the option that controls the order in which the file system checks are done at reboot time.

  • 0 - Do not check.
  • 1 - The file systems to be checked first and one at a time.
  • 2 - All other file systems which are checked later and possibly in parallel.

The root file system should have a value of 1, and all other file systems you want to be checked should have a value of 2.

Conclusion

fsck is a command-line tool for checking and optionally repairing Linux file systems.

To learn more about the fsck command, visit the fsck man page or type man fsck in your terminal.

If you have any questions or feedback, feel free to leave a comment.