How-to

The fsck Command: Check and Repair Linux Filesystems

fsck finds and fixes filesystem damage after a crash or a disk error. The golden rule: never run a repair on a mounted filesystem.

When do I actually need fsck?

Modern journaling filesystems such as ext4 and XFS recover from a crash on their own by replaying the journal at mount time. You need a manual check when the kernel logs filesystem errors, when ext4 remounts itself read only, when files return I/O errors, or after a drive reported media errors. Look for clues first:

journalctl -k -b | grep -iE 'ext4-fs error|xfs.*corrupt|i/o error'
sudo dumpe2fs -h /dev/nvme1n1p1 2>/dev/null | grep -iE 'state|errors|last checked'

A Filesystem state: not clean with errors line means ext4 has flagged itself for a check.

Why must the filesystem be unmounted?

fsck reads and rewrites metadata directly on the device. If the kernel is writing to the same filesystem at the same time, the two views clash and a repair can make damage worse. So unmount first. A read only check on a mounted ext4 filesystem is possible with -n, but its results are unreliable while the system writes.

How do I check a data partition?

sudo systemctl stop postgresql
sudo umount /var/lib/postgresql
sudo fsck -N /dev/nvme1n1p1          # show which checker would run
sudo e2fsck -f -n /dev/nvme1n1p1     # dry run, changes nothing
sudo e2fsck -f -p /dev/nvme1n1p1     # fix safe problems automatically

fsck is a front end that calls the right checker for each type: e2fsck for ext2, ext3 and ext4, fsck.vfat for the EFI partition. -f forces a full check even if the filesystem looks clean. Always do the -n dry run first to see what it plans. -p fixes problems that are safe without asking; for anything bigger, run e2fsck -f and answer the prompts, or -y to accept all fixes.

How do I repair XFS?

XFS does not use fsck at all; fsck.xfs just exits successfully. The real tool is xfs_repair:

sudo umount /srv/media
sudo xfs_repair -n /dev/nvme1n1p2     # check only
sudo xfs_repair /dev/nvme1n1p2        # repair

If it refuses because the log is dirty, mount and unmount the filesystem once to replay the log, then run it again. xfs_repair -L zeroes the log and can lose the last few seconds of changes, so use it only when mounting fails. AlmaLinux, Rocky and CentOS Stream use XFS by default; Debian and Ubuntu default to ext4.

How do I check the root filesystem on a remote server?

You cannot unmount root while the system runs, and our machines have no remote console, so the check has to happen during boot before root is mounted read write. systemd-fsck does that when asked:

sudo touch /forcefsck                      # older method, still honored on many systems
sudo tune2fs -C 200 /dev/nvme0n1p2         # set mount count above max to force a check
sudo grubby --update-kernel=ALL --args="fsck.mode=force fsck.repair=yes"   # RHEL family
sudo reboot

On Debian and Ubuntu, add fsck.mode=force fsck.repair=yes to GRUB_CMDLINE_LINUX in /etc/default/grub, run sudo update-grub, reboot once, then remove the arguments again. fsck.repair=yes lets the check fix problems without a prompt, which matters since nobody can type at the console. Afterwards, read the results with journalctl -b -u systemd-fsck-root.

What do the fsck exit codes mean?

CodeMeaning
0No errors
1Errors corrected
2Errors corrected, reboot needed
4Errors left uncorrected
8Operational error
16, 32Usage error, cancelled by user
128Shared library error

Codes add up, so 3 means 1 plus 2. In scripts, check $? right after the run and treat anything at or above 4 as a failure.

What if fsck keeps finding errors?

Repeated corruption on a filesystem that was cleanly repaired usually means the problem is below it: a failing drive or memory errors. Check the drive with Check disk health with smartctl and look for machine check messages with dmesg command. If either shows trouble, copy your data elsewhere and open a ticket from the panel with the output.

How do I avoid needing fsck?

  • Shut down and reboot cleanly from the shell or the panel instead of a power cut whenever you can.
  • Keep free space above ten percent; full filesystems fragment metadata and fail writes.
  • Run smartd so you hear about drive wear early.
  • Back up. A repair restores consistency, not lost files, and lost+found is a poor substitute for a backup.

Budget Machines for Everyday Linux Hosting

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
E2Intel Xeon E3-1270 v34 / 832 GB DDR31x 480 GB SSD2x 1 Gbps$38.32$114.96 (3 months)
S2Intel Xeon E-2356G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$58.39$58.39 (1 month)
S8Intel Xeon E-2388G8 / 16128 GB DDR41x 1 TB NVMe2x 10 Gbps$103.13$103.13 (1 month)

Keep Reading

Check disk health with smartctl

Read SMART and NVMe health data with smartctl, run self tests, and spot a drive that is wearing out before it takes your data with it.

Open

Linux mount command

Mount drives, partitions and network shares on Linux, make them permanent in fstab with UUIDs, and unmount safely when something is busy.

Open

dmesg command

Read the Linux kernel ring buffer with dmesg: hardware detection, disk and network errors, out of memory kills, with readable timestamps.

Open

Format a disk on Linux

Turn the blank second NVMe drive into usable space: identify it, partition with GPT, choose ext4 or XFS, mount by UUID and keep TRIM on.

Open

FAQ

Quick Answers

Not for repairs. Use -n for a read only look, and repair only after unmounting.

fsck.xfs is a stub. Use xfs_repair on an unmounted XFS filesystem.

Add fsck.mode=force fsck.repair=yes to the kernel command line and reboot once.

e2fsck puts recovered files without a name there after a repair.

ext4 checks on NVMe are usually quick, minutes rather than hours, because only metadata is read.