Using fsck to Find and Repair Disk Errors and Bad Sectors

Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

This guide is part of a series on Linux commands and features. Not all commands may be relevant to Linode-specific hardware, and are included here to provide an easy to access reference for the Linux community. If you have a command or troubleshooting tip that would help others, please submit a pull request or comment.

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 functions as a frontend utility for a combination of built-in tools to check the disk and generates a report of its findings.

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

Note
fsck is not installed by default when using Linode’s rescue mode, and instead the e2fsck tool, which fsck functions as a frontend for, will need to be called directly for standard ext filesystems. Please see our Troubleshooting Guide: Booting into Rescue Mode for guidance.

When to Use fsck

Use fsck to check your file system if your system fails to boot, if files on a specific disk become corrupt, or if an attached drive does not act as expected. Unmount the disks you intend to work on before attempting to check or repair them.

Important
Unmount the target disk first. You risk corrupting your file system and losing data if you run fsck on an active disk.

fsck Options and Arguments

OptionAction
-aAttempt to fix errors automatically. Use with caution.
-fForce fsck to check a file system even if it thinks it’s clean.
-ACheck all disks listed in /etc/fstab.
-CShow progress bar (ext2 and ext3 file systems only).
-MSkip mounted file systems.
-NTest run. Describes what would happen without executing the check itself.
-PUse with the -A option to run multiple checks in parallel.
-RIf using the -A option, do not check the root filesystem.
-tCheck only a specific type of filesystem.
-TSkip the title on startup.
-yInteractive repair mode.

Unmount the Disk

Boot into Rescue Mode

If you are using fsck on a Linode, the easiest and safest way to unmount your disk is to use Rescue Mode. Visit our Rescue and Rebuild guide for instructions on how to boot your Linode into Rescue Mode. If you’re working on a local machine, consider using the distribution’s recovery mode or a live distribution to avoid working on a mounted disk. fsck should be run only as a user with root permissions.

View Mounted Disks and Verify Disk Location

  1. Run df to view a list of currently mounted disks. If you are using Rescue Mode, the disk you want to check should not be listed:

    df -h
    
  2. Use fdisk to view disk locations:

    fdisk -l
    

    Copy the location of the target disk to use with the fsck command.

Configuration Profile

If you are working on a Linode but do not wish to use Rescue Mode, shut down the Linode from the Linode Manager. Unmount the disk from the Configuration Profile. Apply the changes and reboot the Linode.

Manual Unmount

If you are working on a local machine, unmount the disk manually.

  1. Use umount to unmount the disk location copied in the previous step:

    umount /dev/sda
    
  2. If the disk is declared in /etc/fstab, change the mount point to none there as well.

How to Check for Errors on a Disk

Run fsck on the target disk, using the desired options. This example checks all file systems (-A) on /dev/sda:

fsck -A /dev/sda

Understand fsck Error Codes

The error codes that fsck returns can be understood with the following table from man7.org:

CodeError Code Meaning
0No errors
1Filesystem errors corrected
2System should be rebooted
4Filesystem errors left uncorrected
8Operational error
16Usage or syntax error
32Checking canceled by user request
128Shared-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/sda, use this format:

fsck -y /dev/sda

What if fsck got interrupted?

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

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.