The fsck
command is used to check perform consistency and repair Linux file systems. It is useful to repair corrupted file systems when system fails to boot, or a partition cannot be mounted. We will discuss about the fsck command in this article.
Make sure you are logged in with root or user with sudo privileges.
How to Use fsck
Following is the basic syntax of fsck command:
fsck [OPTIONS] [FILESYSTEM]
If you run the fsck command without any options, it will check the devices listed in the fstab
file.
It’s recommended to always unmount the mounted partitions before attempting to check or repair file systems. It may damage your mounted file systems.
The fsck
command accepts different options depending on the file system’s type. For example, to view the options available for fsck.ext4
, type:
man fsck.ext4
Repair Corrupted File System
It is a common use of the fsck
command to repair a non-root corrupted ext3
or ext4
file system.
1. Find the device name using fdisk
, df
or any other tool to find.
2. Unmount the device:
sudo umount /dev/sdc1
3. Run fsck
to repair the file system:
sudo fsck -p /dev/sdc1
Here, -p
option is used to repair the problems automatically safely without user intervention.
4. When the file system is repaired, again mount the partition:
sudo mount /dev/sdc1
Repair Root File System
In the current running system fsck
command cannot check the root file system. You can set the fsck
to run on boot, or use a live disk.
You also can run fsck
command in recovery mode:
- Go to the boot menu and choose Advanced Options
- Select the Recovery mode and then “fsck”.
- Choose “Yes” when it prompt to remount the root file system.
- 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
Generally, all Linux distributions runs fsck
at boot time, if a file system is marked as dirty.
Use the tune2fs
tool to get the current mount count, check frequency number, check interval, and the time of the last check for a specific partition:
sudo tune2fs -l /dev/sdc1 | grep -i 'last checked|mount count'
Mount count: 392
Maximum mount count: -1
Last checked: Tue Oct 01 10:11:08 2019
Check interval: 0 (<none>)
In Maximum mount count
shows the number of mounts after which the filesystem will be checked. If the value is 0
or -1
that means fsck
will never run.
Check interval
is the maximal time between two filesystem checks.
For example, you want to run fsck
after every 15
boots or mounts, you would type:
sudo tune2fs -c 15 /dev/sdc1
You also can set the maximal time between two checks. For instance, to set two month run:
sudo tune2fs -i 2m /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
Conclusion
The fsck
is a command-line tool for checking and repairing Linux file systems. Visit the fsck man page or type man fsck
in your terminal, to learn more about the fsck
command.
If you have any questions or feedback, feel free to leave a comment.