How To Use Fsck Command.
In this article, we will talk about the fsck command.
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.
How to Use fsck
The fsck command takes the following general form:
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:
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:
Run fsck to repair the file system:
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:
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:
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:
“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:
You can also set the maximal time between two checks. For example, to set it one month you would run:
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:
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:
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.
fsck is a command-line tool for checking and optionally repairing Linux file systems.