The FSCK is a system utility. It is a tool that is used to check the consistency of a file system in the Unix-like operating systems. It is a tool that will check and repair inconsistencies in Unix-like systems including Linux. The tool can be used with the help of ‘fsck’ command in Linux. This is equivalent to the ‘CHKDSK’ in the Microsoft Windows.
In this article, we will talk about the fsck command.
When to use fsck in Linux
There are different scenarios when you will want to run fsck. Here are few examples:
- The system fails to boot.
- Files on the system become corrupt (often you may see input/output error).
- Attached drive (including flash drives/SD cards) is not working as expected.
fsck Available options
Fsck command needs to be run with superuser privileges or root. You can use it with different arguments. Their usage depend on your specific case. Below you will see some of the more important options:
-A– Used for checking all filesystems. The list is taken from
-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.
-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. This is useful only with ‘
-r– Provide statistics for each device that is being checked.
-T– Does not show the title.
-t– Exclusively specify the filesystem types to be checked. Types can be comma separated list.
-V– Provide description what is being done.
Repair corrupted file system
The simplest use case of the fsck command is to repair a non-root corrupted ext3 or ext4 file system.
If you don’t know the device name, use fdisk, df, or any other tool to find it.
Unmount the device:
$ sudo umount /dev/sdc1
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.
Once the file system is repaired, mount the partition:
$ sudo mount /dev/sdc1
Understanding fsck exit codes
After running fsck, it will return an exit code. These cods can be seen in fsck’s manual by running:
# man fsck 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
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:
- Enter the boot menu and choose Advanced Options
- Select the Recovery mode and then “fsck”.
- When prompted to remount the root file system choose “Yes”.
- Once done, resume the normal boot.
To run fsck from a live distribution:
- Boot the live distribution.
- Use fdisk or parted to find the root partition name.
- Open the terminal and run:
$ sudo fsck -p /dev/sda1
- 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: Sat Dec 14 19:10:07 2019 Check interval: 0 ()
- “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:
On older distributions fsck will run on boot if the /forcefsck file is present:
$ sudo touch /forcefsck
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:
# [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.
In this tutorial you learned how to use fsck and run consistency checks on different Linux filesystem. If you have any questions about fsck, please do not hesitate to submit them in the comment section below.