Linux

How to Check and Repair a Linux Filesystem (fsck for ext4, xfs_repair for XFS)

Safety first:

Never repair a mounted filesystem, because it can destroy data. Take a backup or snapshot first, and always run a read-only check before you repair anything.

Short answer: Find the device and its filesystem type with lsblk -f. For ext4, unmount it and run fsck -n /dev/DEVICE to check, then fsck -y /dev/DEVICE to repair. For XFS, the default on AlmaLinux and Rocky Linux, use xfs_repair -n and xfs_repair instead, because fsck does nothing on XFS. The root filesystem cannot be unmounted, so repair it from rescue mode or force a check at the next boot.

Signs of filesystem problems

  • Errors such as Read-only file system when you save files: the kernel switched the disk to read-only to protect it.
  • Messages about ext4 or XFS errors in dmesg or the system journal.
  • Files or folders that cannot be opened, or show question marks in ls -l.
  • The server stops at boot and asks you to run fsck manually.
dmesg -T | grep -iE "ext4|xfs|i/o error" | tail -n 20
journalctl -k -p err -b --no-pager | tail -n 20

Step 1: find the device and filesystem type

lsblk -f          # devices, filesystem types and mount points
df -hT            # mounted filesystems with their types

Note the device for the problem partition, for example /dev/sda3, /dev/vda1 or an LVM volume such as /dev/mapper/almalinux-home, and its type.

Filesystem Check and repair tool Common on
ext4 fsck (runs e2fsck) Debian, Ubuntu, many VPS images
XFS xfs_repair AlmaLinux, Rocky Linux, RHEL, CloudLinux

Step 2: back up first

A repair can delete damaged files or move them to lost+found. Copy important data off the server, or take a snapshot, before you continue. See our backups guide.

Step 3: check and repair a data partition

For a partition other than /, such as /home or an extra disk, stop the services that use it, then unmount it:

# Find what is using the partition
lsof +f -- /home | head

# Unmount it
umount /home

ext4

fsck -n /dev/sda3     # check only, no changes
fsck -y /dev/sda3     # repair, answering yes to every fix

XFS

xfs_repair -n /dev/sda3     # check only, no changes
xfs_repair /dev/sda3        # repair

If xfs_repair says the log must be replayed, mount and unmount the filesystem once, then run it again. Use the -L option only as a last resort, because it discards the log and can lose recent changes.

Mount the partition again with mount /home and check your data.

Step 4: the root filesystem

You cannot unmount / while the server runs from it. You have two options:

  • Rescue mode: boot the server into your provider’s rescue system, then run the check on the root device, which is not mounted there. This works for both ext4 and XFS.
  • Check at next boot (ext4): add fsck.mode=force fsck.repair=yes to the kernel command line for one boot, then remove it again.
# AlmaLinux and Rocky Linux: force a check at the next boot, then reboot
grubby --update-kernel=ALL --args="fsck.mode=force fsck.repair=yes"
reboot

# After the server is back, remove the option again
grubby --update-kernel=ALL --remove-args="fsck.mode=force fsck.repair=yes"

Have console access ready in case the check needs your input. XFS is checked by xfs_repair from rescue mode, not at boot.

Read the fsck result

Exit code Meaning
0 No errors found
1 Errors were found and corrected
2 Errors corrected; the system should be rebooted
4 Errors left uncorrected
8 Operational error, for example a wrong device name

Check the code with echo $? right after fsck. Files fsck could not place are moved to lost+found on that filesystem.

When the disk itself is failing

Repeated errors can mean the disk is failing, not just the filesystem. On a dedicated server, check the disk health:

smartctl -H /dev/sda
smartctl -a /dev/sda | grep -iE "reallocated|pending|uncorrectable"

If the disk reports problems, copy your data off at once and contact support to replace it. On a VPS, the disk is virtual, so report repeated I/O errors to support.

Need help with this?

Disk or filesystem trouble on your server? Our team can check and repair it safely from rescue mode.

See server management plansContact @CustomerPanel

Frequently asked questions

Can I run fsck on a mounted filesystem?

No. It can corrupt data. Unmount the filesystem first, or use rescue mode for the root filesystem.

Why does fsck do nothing on AlmaLinux?

AlmaLinux uses XFS by default, and fsck does not check XFS. Use xfs_repair instead.

How do I check the root filesystem?

Boot into rescue mode and check the unmounted device, or force a check at the next boot on ext4.

What is the lost+found folder?

It holds files that fsck recovered but could not put back in their original folder.

Is it safe to answer yes to every fix?

Usually, after a read-only check and a backup. Without a backup, a repair can still remove damaged files.


Sources