Blog / Linux/ Linux fsck Command Guide: Parameters, Usage, and Filesystem Repair

Linux fsck Command Guide: Parameters, Usage, and Filesystem Repair

Linux fsck 命令详解:参数、使用场景与文件系统修复指南

Introduction to fsck

The fsck (File System Consistency Check) command is a vital tool in Linux and Unix systems for checking and repairing file system consistency. It is used when file systems become corrupted due to unexpected power loss, forced reboots, or hardware failures. This guide details common parameters, usage scenarios, and important considerations for using fsck.

Basic Syntax

fsck [options] [filesystem]

The filesystem can be specified as a device path (e.g., /dev/sda1) or a mount point (e.g., /, /usr).

Common Parameters

Check Scope Control

  • -A: Check all filesystems listed in /etc/fstab (typically excludes the root partition / unless combined with -R).
  • -R: When used with -A, skips checking the root filesystem.
  • -C: Show a progress bar (works with ext2/ext3/ext4 filesystems).
  • -V: Verbose mode, displaying detailed information during the check.
  • -N: Dry run; shows what would be done without performing any actions.

Filesystem Type Specification

  • -t filesystem_type: Specify the filesystem type to check (e.g., ext4, xfs, btrfs). Can be omitted if the system auto-detects it or it's defined in /etc/fstab.
  • -T: Do not show the title on startup.

Repair Mode Parameters

  • -a: Automatically repair all detected issues without user confirmation (may not be supported in older versions).
  • -y: Automatically answer "yes" to all questions, performing automatic repairs (recommended for non-interactive scripts).
  • -n: Automatically answer "no" to all questions, checking only without repairs.
  • -r: Interactive repair, asking the user for each issue (largely superseded by -y or -n).
  • -p: Automatically repair "safe" problems (those unlikely to cause data loss). When used with -A, allows parallel checking of multiple filesystems.

Other Parameters

  • -s: Perform checks serially to avoid multiple fsck instances running concurrently (deprecated; modern systems handle this automatically).
  • -f: Force a check even if the filesystem is marked "clean."
  • -M: Do not check mounted filesystems (prevents potential data corruption).

Usage Examples

Check and Automatically Repair a Specific Partition

sudo fsck -y /dev/sda1

Check All Filesystems (Except Root) with Progress Bar

sudo fsck -AR -C

Check Only an ext4 Filesystem Without Repairing

sudo fsck -t ext4 -n /dev/sdb2

Important Considerations

  1. Unmount the Filesystem: Always unmount the target filesystem (umount /dev/sdX) before checking. The root partition (/) cannot be unmounted; check it in rescue mode or during boot.
  2. Data Backup: Back up important data before performing repairs, as the process may lead to data loss.
  3. Checking at Boot: If the system fails to boot and prompts for a filesystem check, follow the instructions or boot from a Live CD/USB to run fsck.
  4. Modern Alternatives: For specific filesystems (e.g., xfs_repair for XFS, btrfs check for Btrfs), use their dedicated tools, which are often more efficient than the generic fsck.

Summary

fsck is a crucial tool for maintaining Linux filesystem health, especially for the ext family. Mastering common parameters (e.g., -y, -f, -t) helps administrators quickly diagnose and repair filesystem errors. For production environments, test operations in a safe setting first and ensure you have complete backups.

Post a Comment

Your email will not be published. Required fields are marked with *.