Oct 26, 2023 5 min read

Fsck Command in Linux (Repair File System)

Use Fsck Command in Linux (Repair File System) with our step-by-step tutorial. The fsck checks Linux file systems and performs repairs.

Fsck Command in Linux (Repair File System)
Table of Contents

Introduction

Before we begin talking about fsck Command in Linux (Repair File System), let's briefly understand – What is fsck command?

fsck (file system check) is a command-line utility that allows you to check the consistency of one or more Linux file systems and perform interactive repairs. It uses programs designed specifically for the kind of file system it checks.

In cases when the system is unable to boot or a partition cannot be mounted, you can use the fsck command to repair corrupted file systems.

In this tutorial, you will understand fsck Command in Linux (Repair File System). We will also address a few FAQs on fsck Command in Linux (Repair File System).

How to Use fsck

The fsck command has the following general syntax:

fsck [OPTIONS] [FILESYSTEM]

The buffer can only be cleared by root or users with sudo privileges.

fsck checks the devices listed in the fstab file when no FILESYSTEM is provided as an argument.

Never execute fsck on mounted partitions since it could harm the file system. Always unmount a file system before attempting to check or fix it.

The fsck command is a wrapper for the various Linux file system checkers (fsck.*), and it accepts multiple options based on the type of file system.

For more details on a particular checker, consult the manual pages. For example, to see the available options for fsck.ext4, enter:

man fsck.ext4

Repair Corrupted File System

The fsck command is most commonly used to repair a non-root corrupted ext3 or ext4 file system.

1) If you do not know the name of the device, utilize fdisk, df, or another tool to find it.

2) Unmount the device as follows:

sudo umount /dev/sdc1

3) To repair the file system, run fsck:

sudo fsck -p /dev/sdc1

The -p option directs fsck to automatically repair any issues that can be safely repaired without user participation.

4) Mount the partition after the file system has been repaired:

sudo mount /dev/sdc1

Repair Root File System

fsck cannot check the root file system on a running system, since it cannot be unmounted.

There are various methods you can use to check or repair the root file system. You can use a live CD, boot the system in recovery mode, or configure the fsck to execute when the system boots.

To execute fsck in recovery mode, do the following:

1) Enter the boot menu and select Advanced Options.

2) Choose Recovery mode, then “fsck.”

3) Select “Yes” when asked to remount the root file system.

4) Once completed, carry on with the normal boot.

To execute fsck from a live distribution:

1) Start the live distribution.

2) To find the name of the root partition, use fdisk or parted.

3) Run the following command in the terminal:

sudo fsck -p /dev/sda1

4) When finished, reboot the live distribution and boot your system.

Check File Systems on Boot

Most Linux distributions run fsck at boot time if a file system is marked as dirty or after a certain number of boots or times.

Use the tune2fs utility to see the current mount count, check frequency number, check interval, and time of the last check for a given partition:

bl
Output

Mount count:              292
Maximum mount count:      -1
Last checked:             Tue Jul 24 11:10:07 2018
Check interval:           0 (<none>)

  • The number of mounts after which the file system will be checked is the “maximum mount count.” If the value is 0 or -1, fsck will never execute.
  • The maximum duration between two file system checks is known as the “check interval.”

For instance, to run fsck every 25 boots (mounts), enter the following command:

sudo tune2fs -c 25 /dev/sdc1

The maximum amount of time between checks can also be adjusted. To set it, for instance, for one month, you would enter:

sudo tune2fs -i 1m /dev/sdc1

Pass the following kernel boot parameters on SystemD distributions to force fsck to run at boot time:

fsck.mode=force
fsck.repair=yes

If the /forcefsck file is present, fsck will run at boot time on earlier distributions:

sudo touch /forcefsck

fstab Options

fstab is a configuration file that instructs the system how and where to mount the partitions.

The /etc/fstab file includes a list of entries in the below form:

# [File System] [Mount Point] [File System Type] [Options] [Dump] [PASS]
/dev/sda1       /             ext4               defaults  0      1
/dev/sda2       /home         ext4               defaults  0      2
server:/dir     /media/nfs    nfs                defaults  0      0

The option that determines the sequence of file system checks performed at reboot time is found in the sixth column ([PASS]), which is the last column.

  • 0 – Do not check.
  • 1 - The file systems to be checked first and individually.
  • 2 - All additional file systems that are checked later and possibly concurrently.

The value of the root file system should be 1, and the values of all other file systems you want to be checked should be 2.

FAQs on fsck Command in Linux (Repair File System)

How do I run the fsck command? 

To run fsck, you need to specify the device or partition you want to check. For example, you can run fsck /dev/sda1 to check the file system on the first partition of the sda disk.

Can I run fsck on a mounted file system? 

It is generally recommended to unmount a file system before running fsck for better accuracy and reliability. However, you can run fsck on a mounted read-only file system.

What are the different repair options available with fsck?

fsck has multiple repair options such as -a (automatically repair without user interaction), -r (interactively repair issues), and -y (answer "yes" to all repair prompts). It is important to carefully review the options and understand their effects before proceeding.

Is it safe to run fsck on a live system? 

Running fsck on a live system can potentially cause data loss or introduce further corruption. It is generally advised to perform file system checks during system boot or from a live CD/USB for better reliability.

Can fsck repair all types of file systems? 

fsck supports various file system types, including ext2, ext3, ext4, XFS, and more. However, different file systems may have specific purpose-built tools like xfs_repair or e2fsck that provide more features and optimizations.

How long does fsck take to repair a file system? 

The time taken by fsck depends on factors such as the size of the file system, the severity of the issues, and the overall system performance. Repairing small file systems might take seconds, while large file systems or severe issues can take extended periods.

Can fsck fix all file system errors automatically?

fsck can automatically resolve some common file system errors, but it may prompt for user intervention in more complex situations. It is important to carefully review and address these prompts to ensure proper repair.

Conclusion

Linux file systems can be checked and, if necessary, repaired using the command-line tool fsck.

Visit the fsck man page or enter man fsck in your terminal to find out more information about the fsck command.

If you have any queries, feel free to leave a comment below, and we'll be happy to help.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.