How to Use ‘fsck’ to Repair Linux File System Errors

Introduction

The Linux file system is a powerful tool for managing data, but it can sometimes become corrupted or damaged. Fortunately, there is a tool available to help you repair these errors: fsck. Fsck is a command-line utility that can be used to check and repair Linux file system errors. In this guide, we will explain how to use fsck to repair Linux file system errors. We will also discuss some of the common errors that fsck can help you fix. By the end of this guide, you should have a better understanding of how to use fsck to repair Linux file system errors.

How to Use ‘fsck’ to Repair Linux File System Errors

1. Boot your Linux system into single user mode. To do this, press the ‘Ctrl’ + ‘Alt’ + ‘Del’ keys simultaneously and select the ‘Single User Mode’ option from the menu.

2. Once you are in single user mode, type ‘fsck’ followed by the name of the file system you want to check. For example, if you want to check the root file system, type ‘fsck /’.

3. The fsck utility will then scan the file system and report any errors it finds.

4. If the fsck utility finds any errors, it will prompt you to fix them. You can either choose to fix the errors automatically or manually.

5. Once the errors have been fixed, type ‘reboot’ to restart your system.

6. Your system should now be running normally.

Linux Filesystems are responsible for organizing how data is stored and recovered. One way or another, with time, the filesystem may become corrupted and certain parts of it may not be accessible. If your filesystem develops such inconsistency it is recommended to verify its integrity.

This can be completed via a system utility called fsck (file system consistency check), which checks the root file system automatically during boot time or ran manually.

In this article, we are going to review the fsck command and its usage to help you repair Linux disk errors.

When to Use fsck Command in Linux

There are different scenarios when you will want to run fsck. Here are a few examples:

  • The system fails to boot.
  • Files on the system become corrupt (often you may see input/output error).
  • The attached drive (including flash drives/SD cards) is not working as expected.

fsck Command Options

The fsck command needs to be run with superuser privileges or root. You can use it with different arguments. Their usage depends on your specific case. Below you will see some of the more important options:

  • -A – Used for checking all filesystems. The list is taken from /etc/fstab.
  • -C – Show progress bar.
  • -l – Locks the device to guarantee no other program will try to use the partition during the check.
  • -M – Do not check mounted filesystems.
  • -N – Only show what would be done – no actual changes are made.
  • -P – If you want to check filesystems in parallel, including root.
  • -R – Do not check the root filesystem. This is useful only with ‘-A‘.
  • -r – Provide statistics for each device that is being checked.
  • -T – Does not show the title.
  • -t – Exclusively specify the Linux filesystem types to be checked. Types can be comma-separated lists.
  • -V – Provide a description of what is being done.

Run fsck Command to Repair Linux File System Errors

In order to run fsck, you will need to ensure that the partition you are going to check is not mounted. For the purpose of this article, I will use my second drive /dev/sdb mounted in /mnt.

Here is what happens if I try to run fsck when the partition is mounted.

# fsck /dev/sdb
Run fsck on Mounted Partition
Run fsck on Mounted Partition

To avoid this unmount the partition using.

# umount /dev/sdb

Then fsck can be safely run with.

# fsck /dev/sdb
Run fsck on Linux Partition
Run fsck on Linux Partition

Understanding fsck Exit Codes

After running fsck, it will return an exit code. These codes can be seen in fsck’s manual by running:

# man fsck

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

Fsck Repair Linux Filesystem

Sometimes more than one error can be found on a filesystem. In such cases, you may want fsck to automatically attempt to correct the errors. This can be done with:

# fsck -y /dev/sdb

The -y flag, automatically “yes” to any prompts from fsck to correct an error.

Similarly, you can run the same on all filesystems (without root):

$ fsck -AR -y 

How to Run fsck on Linux Root Partition

In some cases, you may need to run fsck on the root partition of your system. Since you cannot run fsck while the partition is mounted, you can try one of these options:

  • Force fsck upon system boot
  • Run fsck in rescue mode

We will review both situations.

Force fsck Upon System Boot

This is relatively easy to complete, the only thing you need to do is create a file called forcefsck in the root partition of your system. Use the following command:

# touch /forcefsck

Then you can simply force or schedule a reboot of your system. During the next bootup, the fsck will be performed. If downtime is critical, it is recommended to plan this carefully, since if there are many used inodes on your system, fsck may take some extra time.

After your system boots, check if the file still exists:

# ls /forcefsck

If it does, you may want to remove it in order to avoid fsck on every system boot.

Run fsck in Rescue Mode

Running fsck in rescue mode requires a few more steps. First, prepare your system for reboot. Stop any critical services like MySQL/MariaDB etc and then type.

# reboot

During the boot, hold down the shift key so that the grub menu is shown. Select “Advanced options”.

Grub Advance Options
Grub Advanced Options

Then choose “Recovery mode”.

Select Linux Recovery Mode
Select Linux Recovery Mode

In the next menu select “fsck”.

Select fsck Utility
Select fsck Utility

You will be asked if you wish to have your / filesystem remounted. Select “yes”.

Confirm Root Filesystem
Confirm Root Filesystem

You should see something similar to this.

Running fsck Filesystem Check
Running fsck Filesystem Check

You can then resume normal boot, by selecting “Resume”.

Select Normal Boot
Select Normal Boot
Conclusion

In this tutorial, you learned how to use fsck and run consistency checks on different Linux filesystems. If you have any questions about fsck, please do not hesitate to submit them in the comment section below.

If You Appreciate What We Do Here On Jassweb, You Should Consider:

Jassweb is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit Jassweb! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

How to Use ‘fsck’ to Repair Linux File System Errors

The Linux file system is a complex and powerful system, but it can sometimes suffer from errors that can cause data loss or system instability. Fortunately, Linux provides a powerful tool called ‘fsck’ that can be used to repair these errors and restore the file system to a healthy state. In this article, we’ll explain how to use ‘fsck’ to repair Linux file system errors.

What is ‘fsck’?

‘fsck’ stands for ‘file system check’. It is a command-line utility that is used to check and repair Linux file systems. It can be used to repair errors in the file system, such as corrupted inodes, lost blocks, and other issues. It can also be used to check the integrity of the file system and make sure that it is in a healthy state.

How to Use ‘fsck’

Using ‘fsck’ is relatively straightforward. First, you need to boot your system into single-user mode. This can be done by pressing the ‘Ctrl+Alt+Del’ keys at the same time. Once you are in single-user mode, you can run the ‘fsck’ command. The syntax for the command is as follows:

fsck [options] [filesystem]

The ‘options’ parameter is used to specify the type of check that you want to perform. The ‘filesystem’ parameter is used to specify the file system that you want to check. For example, if you want to check the root file system, you would use the following command:

fsck -f /

The ‘-f’ option tells ‘fsck’ to perform a full check of the file system. If you want to perform a more thorough check, you can use the ‘-c’ option. This will check for bad blocks and other errors that may not be detected by the ‘-f’ option.

Once ‘fsck’ has finished checking the file system, it will report any errors that it has found. If it finds any errors, it will prompt you to repair them. You can choose to repair the errors or ignore them. If you choose to repair the errors, ‘fsck’ will attempt to fix them. Once it has finished, it will report whether or not the errors were successfully repaired.

Conclusion

The ‘fsck’ command is a powerful tool that can be used to repair Linux file system errors. It can be used to check the integrity of the file system and repair any errors that it finds. It is important to remember to always back up your data before running ‘fsck’ to ensure that you can recover your data if something goes wrong.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?