This post was originally published on go2linux.org. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements.
fsck (file system check) checks a file system for inconsistencies and, if you let it, repairs them. You need it after a power cut or a crash, when the kernel logs file system errors, or when a disk refuses to mount.
fsck itself is a front end. It finds out the type of the file system and runs the real checker: e2fsck (also called fsck.ext4) for ext2, ext3 and ext4, fsck.vfat for FAT, and so on.
The one rule: never on a mounted file system
Do not run fsck on a file system that is mounted read-write. The checker changes on-disk structures while the kernel is also changing them, and the result can be much worse corruption than what you started with. e2fsck will warn you and ask you to confirm. Answer no.
Unmount it first:
lsblk -f
sudo umount /dev/sdb1
lsblk -f shows each partition with its file system type and mount point. If umount says the target is busy, see how to find what is using a disk.
For the root file system, which you cannot unmount while the system is running, see Checking the root file system below.
Check without changing anything
Start with a read-only check. -n answers "no" to every question, so nothing is written:
sudo fsck -n /dev/sdb1
On ext4, a clean file system is skipped unless you force the check with -f:
sudo fsck -fn /dev/sdb1
e2fsck 1.47.0 (5-Feb-2023)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
data: 1543/1310720 files (0.4% non-contiguous), 127305/5242624 blocks
Five passes with no messages in between means the file system is consistent.
Repair
Interactive: fsck asks before each fix.
sudo fsck /dev/sdb1
On a badly damaged file system that can mean hundreds of questions. To answer yes to all of them:
sudo fsck -y /dev/sdb1
Or let it fix only what is safe to fix without a human, and stop if it finds something serious. This is what the boot process uses:
sudo fsck -p /dev/sdb1
(-a is an older synonym for -p.)
If the disk holds data you cannot lose and it is physically failing, copy it first with ddrescue and run fsck on the copy. Repairs write to the disk, and a dying disk may not survive them.
Files that fsck cannot place back in a directory end up in lost+found at the root of that file system, named by inode number. Look there after a big repair.
Options
Front end options, which go before any checker options:
-A— check every file system listed in/etc/fstab, in the order given by the sixth field (pass number).-R— with-A, skip the root file system.-N— do not run anything; just show what would be done.-t type— only check file systems of this type, for example-t ext4.-s— check one file system at a time instead of in parallel. Needed if the checks are interactive.-C— show a progress bar.
Common checker options (passed through to e2fsck):
-n— open read-only and answer no to everything.-y— answer yes to everything.-p— repair automatically what is safe, stop otherwise.-f— check even if the file system is marked clean.-c— scan for bad blocks withbadblocksand mark them unusable.
Exit codes
The exit code is the sum of these values, so a script can tell exactly what happened:
| Code | Meaning |
|---|---|
| 0 | No errors |
| 1 | Errors were found and corrected |
| 2 | Errors corrected; the system should be rebooted |
| 4 | Errors left uncorrected |
| 8 | Operational error |
| 16 | Usage or syntax error |
| 32 | Checking canceled by user request |
| 128 | Shared library error |
An exit code of 4 or higher after a repair means you are not done.
sudo fsck -p /dev/sdb1; echo "exit code: $?"
Checking the root file system
You cannot unmount / while the system is running, so the check has to happen at boot, before it is mounted read-write. On systemd distributions, systemd-fsck does this for every file system with a non-zero sixth field in /etc/fstab (1 for root, 2 for the rest, 0 to skip).
To force a full check at the next boot, add these to the kernel command line. At the GRUB menu press e, add them to the end of the line that starts with linux, and boot with Ctrl+X:
fsck.mode=force fsck.repair=yes
fsck.repair=yes answers yes to every question; use preen (the default) to only fix what is safe.
The old tricks, shutdown -F and touch /forcefsck, came from SysV init and do not work on current systemd distributions.
The other option is to boot from a live USB, where the root file system of the installed system is not mounted, and run fsck on it from there.
Periodic checks on ext4
ext2 and ext3 used to force a check every so many mounts, which made some boots take ages. New ext4 file systems have that disabled. You can see and change the settings with tune2fs:
sudo tune2fs -l /dev/sda2 | grep -iE 'mount count|last checked|check interval'
Mount count: 42
Maximum mount count: -1
Last checked: Sat Mar 2 10:14:08 2024
Check interval: 0 (<none>)
A maximum mount count of -1 means "never". To check every 30 mounts or every month, whichever comes first:
sudo tune2fs -c 30 -i 1m /dev/sda2
XFS and Btrfs: do not use fsck
- XFS:
fsck.xfsexists but does nothing and exits successfully. XFS replays its journal at mount time. To check, unmount and runsudo xfs_repair -n /dev/sdb1; drop-nto repair. - Btrfs:
fsck.btrfsalso does nothing. Usesudo btrfs scrub start /mountpointon the mounted file system to verify checksums.btrfs checkworks on an unmounted file system and is read-only by default. Avoidbtrfs check --repairunless the Btrfs documentation or developers tell you to; it can make things worse.
See also
man fsck, man e2fsck and man tune2fs. If you are setting up a new disk rather than fixing one, see how to partition a disk with fdisk.