fsck Command Examples

Sometimes bad things happen to good systems.

Fortunately, you're a Linux user and you have fsck (file system check) to help with a potentially corrupted filesystem. This utility is used for checking and (optionally) repairing the file system.

There are several scenarios where you may want to use fsck. Typically, you would want to run this command if your system will not boot, a device (external drives or storage media) is not functioning properly, or if you have seen evidence of file corruption.

Fsck is a actually a "front-end" for a number of file system specific checkers like fsck.vfat, fsck.ext2, etc. These do not need to be specified, but you may be able to find more advanced options in the man pages of these more precise commands.

Introduction to the fsck command

The fsck command follows a pattern similar to most Linux commands.

fsck [options] [filesystem]

If you do not specify a filesystem, the system will analyze your fstab file (/etc/fstab) for the devices to scan.

You will need to run the command either as root user or use it with sudo.

You can use fdisk or df command to list the hard drive in Linux. This way, you can specify which device to be checked with fsck command.

Disk /dev/nvme0n1: 238.49 GiB, 256060514304 bytes, 500118192 sectors
Disk model: THNSN5256GPUK NVMe TOSHIBA 256GB        
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 014A45DC-22A2-4FC0-BEEA-25A6F2406380

Device            Start       End   Sectors   Size Type
/dev/nvme0n1p1     2048   1050623   1048576   512M EFI System
/dev/nvme0n1p2  1050624  98563270  97512647  46.5G Linux filesystem
/dev/nvme0n1p3 98564096 500117503 401553408 191.5G Linux filesystem

Unmount the device before running fsck

Do not run fsck on a mounted device, you will need to unmount the target first to avoid damage to your files.

If you try running fsck on a mounted device, you should see an error like this:

chris@handbook:~$ sudo fsck /dev/sda3
fsck from util-linux 2.34
e2fsck 1.45.5 (07-Jan-2020)
/dev/sda3 is mounted.
e2fsck: Cannot continue, aborting.

Running fsck on a normal, healthy drive looks like this:

chris@handbook:~$ sudo fsck /dev/sda2
fsck from util-linux 2.34
fsck.fat 4.1 (2017-01-24)
/dev/sda2: 5 files, 1967/1972 clusters

While fsck accepts a device name like /dev/sda, you may elect to enter the UUID to avoid confusion with mounting and unmounting devices. The UUID is a fixed value assigned to your device and will not be affected by these system changes.

Understanding exit codes for the fsck command

This is a list of the codes that may be returned from fsck after inspecting a disk. Your exit code will be a sum of these codes if you analyze one disk. If you are using fsck on multiple devices, it will return the bit-wise OR of the two sums.

  • 0 - No errors
  • 1 - File system errors corrected
  • 2 - System should be rebooted
  • 4 - File system errors left uncorrected
  • 8 - Operational error
  • 16 - Usage or syntax error
  • 32 - Fsck canceled by user request
  • 128 - Shared library error

You can check the exit code of the last run command using echo $? command.

Practical usage of the fsck command

Now that you are a tad bit familiar with the fsck command, let's see practical use cases of this command.

Repair a USB disk and other removable devices

For our purpose, let's assume that you have already identified the problematic device /dev/sdb.

First, you need to make sure that the drive has been unmounted:

sudo umount /dev/sdb

Now run the fsck command:

sudo fsck /dev/sdb

Check the output for any errors. If none displayed, check the exit code with echo $?.

There are also some option flags that we can add to allow some automated correction. These commands aren't standardized though, and you should verify the filesystem type and compare documentation from that specific man page.

Despite that, generally you can use -p to allow fsck to automatically apply repairs.

sudo fsck -p /dev/sdb

Similarly, -y will apply corrections to any detected filesystem corruption.

Repair the root file system

You cannot unmount the root partition while the system is active. If you suspect your main file system is corrupted, you have to use a different approach here.

There are actually a few different options that you can use. You can run fsck at boot time, in rescue mode, or use a recovery-themed live cd.

Many Linux distributions will automatically force fsck at start up after a certain number of failed boot attempts. If you prefer to take matters into your own hands, you can schedule the system to do this ourselves.

Most modern Linux versions feature a tool called tune2fs.

sudo tune2fs -c 1 /dev/sda

Presuming your root device is dev/sda, this is the command you would enter.

Now, what's actually happening is that you're changing the system settings so that fsck is run every n number of boots (1 in the example). You could also set this to a standard time interval. The options are days, weeks, or months.

Let's say that you want fsck to run any time that you boot if there hasn't been a check in a week. You could use -i to specify the interval and the command would look like this.

sudo tune2fs -i 1w /dev/sda	

If you're using systemd, you can force run fsck at your next boot by entering the following:

fsck.mode=force
fsck.repair=yes

Conclusion

You can always turn to the man-pages for more information. Just use man fsck in the terminal.

I hope you learned something new about the fsck command. If you have any comments or questions, please leave them below.