Fsck Command in Linux (Repair File System)
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
How to Use
fsck command takes the following general form:
fsck [OPTIONS] [FILESYSTEM]
Only root or users with
sudo privileges can clear the buffer.
FILESYSTEM is provided as an argument,
fsck checks the devices listed in the
fsck on mounted partitions as it may damage the file system. Before attempting to check or repair file systems always
unmount it first.
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
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
df, or any other tool to find it.
Unmount the device:
sudo umount /dev/sdc1
fsckto repair the file system:
sudo fsck -p /dev/sdc1
fsckto 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
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.
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.
fsck from a live distribution:
Boot the live distribution.
partedto 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
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
fsckwill 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
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.
/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
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.