Linux fsck Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we explore the Linux fsck (file system check) command and its practical applications. The fsck command is a critical tool used to check and repair file system inconsistencies, ensuring the integrity and reliability of your data. We start by understanding the purpose and importance of the fsck command, then learn how to use it to check the integrity of a file system and repair any issues that may arise. This lab provides a comprehensive understanding of the fsck command and its role in maintaining the health of your Linux file systems.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/mount("`File System Mounting`") subgraph Lab Skills linux/rm -.-> lab-422688{{"`Linux fsck Command with Practical Examples`"}} linux/dd -.-> lab-422688{{"`Linux fsck Command with Practical Examples`"}} linux/df -.-> lab-422688{{"`Linux fsck Command with Practical Examples`"}} linux/mount -.-> lab-422688{{"`Linux fsck Command with Practical Examples`"}} end

Understanding the Purpose and Importance of fsck Command

In this step, we will explore the purpose and importance of the fsck (file system check) command in Linux. The fsck command is a critical tool used to check and repair file system inconsistencies, ensuring the integrity and reliability of your data.

The fsck command is typically run during system boot-up or when a file system is suspected to have issues. It performs a thorough check of the file system's metadata, such as directory structures, inodes, and block allocations, to identify and fix any errors or inconsistencies.

Let's start by understanding the purpose of the fsck command:

sudo fsck --help

Example output:

Usage: fsck.ext4 [-panyrcdfvtDFV] [-b superblock] [-B blocksize] [-l|-L bad_blocks_file] [-C fd] [-j external_journal] [-E extended-options] device

The fsck command can be used to check and repair various file systems, including ext2, ext3, ext4, XFS, and more. It helps ensure the integrity of your file system, preventing data loss and ensuring the proper functioning of your system.

Checking Filesystem Integrity with fsck

In this step, we will learn how to use the fsck command to check the integrity of a file system in Linux.

First, let's create a test file system using a loopback device:

sudo dd if=/dev/zero of=test.img bs=1M count=100
sudo losetup /dev/loop0 test.img
sudo mkfs.ext4 /dev/loop0

Now, we can use the fsck command to check the integrity of the file system:

sudo fsck /dev/loop0

Example output:

fsck from util-linux 2.37.2
e2fsck 1.46.5 (30-Dec-2021)
/dev/loop0: clean, 11/25600 files, 6400/102400 blocks

The output shows that the file system is clean, with no issues detected.

Let's introduce an error to the file system by corrupting the superblock:

sudo dd if=/dev/zero of=test.img bs=1 count=1 seek=1024
sudo fsck /dev/loop0

Example output:

fsck from util-linux 2.37.2
e2fsck 1.46.5 (30-Dec-2021)
/dev/loop0: Superblock invalid, trying backup blocks...
/dev/loop0: Going to read-only
/dev/loop0: Backup superblock options:
        -b 32768
/dev/loop0: Superblock has an invalid journal (inode 8).
Clear journal (y/n)? y
/dev/loop0: Clearing journal inode
/dev/loop0: The filesystem size (according to the superblock) is 102400 blocks
The physical size of the device is 204800 blocks
Either the superblock or the partition table is likely to be corrupt!
/dev/loop0: ***** FILE SYSTEM WAS MODIFIED *****
/dev/loop0: 11/25600 files (0.0% non-contiguous), 6400/102400 blocks

In this example, we can see that the fsck command detected the corrupted superblock and provided options to repair the file system. It is important to carefully review the output and follow the recommended actions to ensure the integrity of your file system.

Repairing Filesystem Issues Using fsck

In the previous step, we introduced an error to the file system by corrupting the superblock. Now, let's learn how to use the fsck command to repair the file system.

First, let's check the file system again to see the issues:

sudo fsck /dev/loop0

Example output:

fsck from util-linux 2.37.2
e2fsck 1.46.5 (30-Dec-2021)
/dev/loop0: Superblock invalid, trying backup blocks...
/dev/loop0: Going to read-only
/dev/loop0: Backup superblock options:
        -b 32768
/dev/loop0: Superblock has an invalid journal (inode 8).
Clear journal (y/n)? y
/dev/loop0: Clearing journal inode
/dev/loop0: The filesystem size (according to the superblock) is 102400 blocks
The physical size of the device is 204800 blocks
Either the superblock or the partition table is likely to be corrupt!
/dev/loop0: ***** FILE SYSTEM WAS MODIFIED *****
/dev/loop0: 11/25600 files (0.0% non-contiguous), 6400/102400 blocks

As you can see, the fsck command detected the corrupted superblock and provided an option to clear the journal.

To repair the file system, we can use the -y (yes) option to automatically answer "yes" to all prompts:

sudo fsck -y /dev/loop0

Example output:

fsck from util-linux 2.37.2
e2fsck 1.46.5 (30-Dec-2021)
/dev/loop0: Superblock invalid, trying backup blocks...
/dev/loop0: Going to read-only
/dev/loop0: Backup superblock options:
        -b 32768
/dev/loop0: Superblock has an invalid journal (inode 8).
Clear journal (y/n)? y
/dev/loop0: Clearing journal inode
/dev/loop0: The filesystem size (according to the superblock) is 102400 blocks
The physical size of the device is 204800 blocks
Either the superblock or the partition table is likely to be corrupt!
/dev/loop0: ***** FILE SYSTEM WAS MODIFIED *****
/dev/loop0: 11/25600 files (0.0% non-contiguous), 6400/102400 blocks

The -y option automatically answers "yes" to all prompts, allowing the fsck command to repair the file system without user intervention.

After the repair, you can remount the file system and continue using it.

Summary

In this lab, we learned the purpose and importance of the fsck (file system check) command in Linux. The fsck command is a critical tool used to check and repair file system inconsistencies, ensuring the integrity and reliability of your data. We also learned how to use the fsck command to check the integrity of a file system, and how to repair issues by introducing an error to the file system and running fsck to detect and fix the problem.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like