Skip to content

How to Use fsck Command

homepage-banner

What is fsck?

Fsck, short for file system consistency check, is a utility that examines the file system for errors and attempts to repair them if possible. It uses a combination of built-in tools to check the disk and generates a report of its findings.

On some systems, fsck automatically runs after an unclean shutdown or a certain number of reboots.

When to Use fsck

To check your file system in case of system boot failure, file corruption on a specific disk, or unexpected behavior of an attached drive, use fsck.

Verify Disks are Unmounted

  1. Before running fsck, ensure that the disks you wish to check are unmounted. Failure to do so may result in file system corruption and data loss. To verify this, enter the following command:

    df -h
    
  2. You will see a similar output:

    root@ttyS0:~# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    tmpfs           739M 1016K  738M   1% /media/ramdisk
    /dev/sdh        160M  160M     0 100% /media/sdh
    /dev/loop0      146M  146M     0 100% /media/compressed_root
    unionfs         739M 1016K  738M   1% /
    devtmpfs         10M     0   10M   0% /dev
    

    Your primary disks should not appear in the list. If your device does not appear in the example output from the df -h command, you can run a filesystem check on it.

How to Check for Errors on a Disk

Do not run fsck on a mounted disk. Make sure that the target disk is unmounted before continuing. Running fsck on an active disk can corrupt your file system and result in data loss.

To run fsck on the target disk, use the desired options. For example, the following command checks all file systems (-A) on /dev/sdb:

fsck -A /dev/sdb

fsck Options and Arguments

Option Action
-A Check all disks listed in /etc/fstab.
-M Skip mounted file systems.
-N Test run. Describes what would happen without executing the check itself.
-P Use with the -A option to run multiple checks in parallel.
-R If using the -A option, do not check the root filesystem.
-t Check only a specific type of filesystem.
-y Interactive repair mode.

Understand fsck Error Codes

The error codes that fsck returns can be understood with the following table from man page

Code Error Code Meaning
0 No errors
1 Filesystem errors corrected
2 System should be rebooted
4 Filesystem errors left uncorrected
8 Operational error
16 Usage or syntax error
32 Checking canceled by user request
128 Shared-library error

Use fsck to Repair File System Errors

Use the -r option to use the interactive repair option.

This example uses fsck to check all file systems except the root, and will attempt repair using the interactive feature:

fsck -AR -y

To check and attempt to repair any errors on /dev/sdb, use this format:

fsck -y /dev/sdb

What if fsck Gets Interrupted?

If fsck is interrupted, it will complete any checks in progress but will not attempt to repair any errors it finds.

Leave a message







Disclaimer
  • Welcome to visit the knowledge base of SRE and DevOps!
  • License under CC BY-NC 4.0
  • Made with Material for MkDocs and improve writing by generative AI tools
  • Copyright issue feedback me#imzye.com, replace # with @