Home » Linux » Shell » Fsck

fsck to Repair Linux File System Errors [4 Commmon Examples]

Modern computers are incredibly reliable, but things can still go wrong – and the worst thing that can usually go wrong is losing your data – be it important work or your precious photos.

Alongside a robust backup systemfsck (File System cheCK) is probably the most important tool you can have on hand to prevent data loss. This tutorial explains how to use fsck to repair file system errors in Linux and takes you through four common examples.

fsck verifies the integrity and repairs errors in your Linux file system.

Syntax

fsck is an interface for a number of file system checkers available for the file systems used by Linux and has the following syntax:

fsck [OPTIONS]... [FILESYSTEMS]...

Note that:

  • [OPTIONS]… is a list of options from the below table
  • [FILESYSTEMS] is the list of file systems to be checked/repaired
  • Options that are not understood by fsck will be passed on to the specific file system checker used for the specific file system you are checking.
  • If no filesystems are specified, fsck will default to checking all file systems in /etc/fstab (i.e., it defaults to the -A option)

Options

fsck has several options. You can view the full list by running:

man fsck

…in your terminal. Here’s a summary of the options you’ll most likely need to use:

Here are the options commonly used when using fsck, straight from the manual (but with some redactions made for brevity – check the manual for more advanced usage detail):

-t fslist Specifies the type(s) of file system to be checked. The fslist parameter is a comma-separated list of filesystems and options specifiers. Normally, the filesystem type is deduced by searching for filesys in the /etc/fstab file and using the corresponding entry.
-A Walk through the /etc/fstab file and try to check all file systems in one run. This option is typically used from the /etc/rc system initialization file, instead of multiple commands for checking a single file system.
-M Do not check mounted filesystems and return an exit code of 0 for mounted filesystems.
-N Don’t execute, just show what would be done.
-R When checking all file systems with the -A flag, skip the root file system (in case it’s already mounted read-write).
-V Produce verbose output, including all file system-specific commands that are executed.

Options to different filesystem-specific fsck’s are not standardized. If in doubt, please consult the man pages of the filesystem-specific checker. Although not guaranteed, the following options are supported by most file system checkers:

-a Automatically repair the file system without any questions (use this option with caution).
-n For some filesystem-specific checkers, the -n option will cause the fs-specific fsck to avoid attempting to repair any problems but simply report such problems.
-r Interactively repair the filesystem (ask for confirmations).
-y For some filesystem-specific checkers, the -y option will cause the fs-specific fsck to always attempt to fix any detected filesystem corruption automatically.

If you want to look at the options available for the specific checker for a specific file system, run:

man fsck.ext4

Exit Codes

The exit code returned by fsck is the sum of the following conditions
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
The exit code returned when multiple file systems are checked is the bit-wise OR of the exit codes for each file system that is checked.

Examples

Here are some common usage examples of fsck – checking local disks, including the root filesystem.

Unmounting

You can’t check a filesystem that’s in use, as this could corrupt the filesystem and result in data loss. To unmount a file system so it is not in use and can be checked, run:

sudo umount /dev/filesystem

… where filesystem is the name of the filesystem to unmount.

If you don’t know the name of the filesystem, run:

df -h

… to get a list of filesystems and mount points.

Re-Mounting

Once you have finished checking and repairing, you can remount a file system using:

sudo mount /dev/filesystem

Check & Repair Corrupted File System

sudo fsck -p /dev/filesystem

Note that:

  • filesystem is the name of the filesystem being checked and looks something like “sda1” or “sdc1”
  • the -p option tells fsck to fix any problems which do not require user interaction automatically

Check on Boot / Checking root File System

As you can’t check a filesystem that is in use, you can’t check the root file system in Linux while the system is fully booted and it is in use.

Running the check on boot is the best way of doing this, as it works even if you’re logged into a system remotely.

Most Linux distribution are configured by default to run fsck automatically if the filesystem is marked as “dirty” (requiring checking) or after a fixed interval of boots.

To force a check of a specific at each system boot (including the root filesystem), use tune2fs to adjust the filesystem parameters:

tune2fs -c 1 /dev/filesystem

Where filesystem is the name of the filesystem to be checked. 1 is the number of boots the fsck is run after – it defaults to 30 in Ubuntu distributions, so to return things to normal, you can run:

tune2fs -c 30 /dev/filesystem

If this doesn’t work, older distributions will run fsck on boot if the /forcefsck file is present:

sudo touch /forcefsck

Once an fsck has been done at boot, the results will be logged at the location:

/var/log/fsck

… Ready for you to check.

If your system is damaged to the point that it won’t boot at all, you can boot from a recovery disc or USB stick and then run fsck from there as the root filesystem you wish to check will not be mounted.

Conclusion

For more information, you can read the fsck manual by running:

man fsck

In your terminal.

Check our other explainers on common Linux shell tools.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment