Learning how to use fsck command on Linux allows you to manage data efficiently, and here’s a detailed guide on how to do it.
Filesystems are in charge of managing how data is saved and retrieved. In one way or another, a filesystem may get corrupted over time and lose access to its components. It is also advised to check your filesystem’s integrity if it exhibits such inconsistent behavior.
This process can be done using the fsck system utility (file system consistency check). This check can be done automatically during boot time or run manually. In this article, we’ll discuss the fsck command on Linux—how it’s used, and its purpose.
How to Use fsck Command on Linux
Fsck can be used for various reasons, such as:
- When you get input/output errors.
- System boot issues.
- Operating system performance issues.
- You want to check your external drives, such as USB or SD cards, for errors.
Installation
Fsck is a built-in utility available in Linux, so you don’t need to install anything. It uses a range of other Linux utilities and generates various types of reports on file system problems. Note that Linux also runs the fsck command during system boot. Also, you will need root permissions to run fsck.
Usage
The basic usage of an fsck command is as follows.
Input:
fsck <options> <filesystem>
Here, the file system refers to a mounted point, partition, or a device.
Steps for checking a file system for errors
Check the partitions and mounted disk
As the first step, you should know the partitions available on your system and mounted devices. You can run the following command for this info:
$ sudo fdisk -1
You can run the df command to locate the disk you might want to scan.
Input:
$ df -h
To see the partition for the first disk on your system, run the following command:
sudo parted /dev/sda 'print'
Where sda, sdb, sdc are the partitions on your system. This is the Linux preferred way of naming disks.
Unmounting a disk
Note that you shouldn’t run the fsck command on mounted drives as it can damage the file system. You will get a warning if you try to run fsck over a mounted device. To unmount a previously mounted device, run the following command:
$ sudo umount /dev/loop8
Checking the system for error
After unmounting the disk, you can run the fsck command to check for errors.
sudo fsck /dev/loop8
Note: If no file system is provided as an option, fsck checks the device listed in fstab
.
Mounting the disk again
Once the fsck command has been run over the device, you will like to mount it again using the following command:
mount /dev/loop8
Running fsck on root partition
Sometimes, you want to run the fsck command on the root partition. As we mentioned earlier, if the device is mounted, you can’t run the fsck command. There are two approaches to running fsck on a root partition. You can either run in rescue mode or at boot time.
Run fsck command in rescue mode
Boot your system in rescue mode and check the root partition using the fsck command. During boot, You will be shown the GRUB menu if you hold the shift key. Choose Advanced and select recovery mode. Upon seeing the fsck menu, select ‘Yes’. After running the fsck command, you can resume booting your system.
Forced fsck during system boot
To force the system to run fsck during system boot, first create a file using the following command:
sudo touch /forcefsck
If the file forcefsck is unavailable, it will automatically be created with the touch command.
Check file system on boot
Fsck usually runs during boot in Linux if the file system is marked as dirty or after the system performs a certain number of boots. You can use the tune2fs tool to check information such as the time for the last file system check using the following command:
$ sudo tune2fs -1/dev/sdc1 | grep -i 'last checked\mount count'
To run the file system check after every ten boots, you can use the following command:
$ sudo tune2fs -c 10 /dev/sdc1
Fsck Options
-A
: Run fsck on all file system
Example:
fsck –AR
-N
: Perform a test run
Example:
sudo fsck -N /dev/loop8
-M
: Run fsck always on a mounted device
Example:
sudo fsck -M /dev/loop8
-t
: Skip a specific file system
Example:
sudo fsck -AR -t noext3 –y
-f
: Force to always check file system
Example:
sudo fsck -f /dev/sdb
-y
: Automatically repair detected errors
Example:
sudo fsck -y /dev/sdb
-n
: Skip repair when checking for errors
Example:
sudo fsck -n /dev/sdb
Never interrupt fsck
You should not interrupt fsck while it is running. In that case, it will finish the ongoing consistency check and stop. If it finds any errors, it will not repair them. However, you can rerun the process again next time to let it finish.
Understanding fsck exit codes
Upon completion of tasks, fsck returns an exit code as an integer value. Each code number has a specific meaning, such as:
0
: No errors1
: Filesystem errors corrected2
: System should be rebooted4
: Filesystem errors left uncorrected8
: Operational error16
: Usage or syntax error32
: Checking canceled by user request128
: Shared-library error
Now that we have discussed the fsck command and its various options in detail, you have a better idea of how to put it to good use. To run the fsck command, make sure you have root permissions. If you have any questions on how to use the fsck command on Linux OS, don’t forget to leave a comment down below.
If this guide helped you, please share it.