In This Article
What is fsck?
Short for file system consistency check, fsck refers to the specific program used on Linux, Unix, and their other variants. It is used to check for any errors existing in the file system.
Technically, it is the front-end for many filesystem checkers (fsck.fstype) available on Linux. Based on the file system, the checker is searched first in the /sbin, then in the /etc/fs and /etc, and finally in the directories recorded in the PATH setting variable.
- The fsck is a Linux utility used to check errors in the file system and also repair them automatically at times, along with proper reports.
- All Linux distributions come with this utility included by default and it is quite easy to use since it does not need specific process to be followed to install or run it.
- There are different commands and options to use while running the fsck utility program. Ideally, it is run at the boot time when specific conditions are detected.
- This utility program is similar to the ScanDisk utility in Windows and may fix the errors automatically or ask the users whether or not the errors are to be fixed. Sometimes, it may not fix them and display them as normal output.
- The fsck program can check different file systems stored on different disk drives in parallel. This reduces the time needed to check all the files. It will return diverse exit codes with unique values to specify precise conditions.
Ideally, fsck is a Linux utility. It checks for any unresolved issues or errors in the file systems, fixes the potential errors, and generates reports.
Available with the Linux distribution by default, this specific utility program does not need any specific installation procedures or steps to follow to use it.
You can start using the tool and exploiting its functionalities once the terminal is loaded.
The fsck program is pretty much the same as the ScanDisk utility available in Microsoft Windows and operates in two specific modes, namely:
- Interactive mode – In this mode, every time an error is found in the file system, the users are asked whether or not they want to fix the error or continue without rectifying it.
- Non-interactive mode – In this mode, the error in the file system is corrected automatically by the system without the user being asked for it.
It may also check the errors but not repair them. Instead, it may display them as regular output.
A few examples of using the fsck program to check a file system could be as follows:
- When the file is mounted on the /usr partition, which has to be unmounted before the fsck /usr command is used
- While checking a Linux JFS file system on a mdadm software RAID device when the fsck -t jfs /dev/md0 command is used
Typically, on most devices, the fsck program runs at boot time under specific circumstances such as:
- The file system is found to be ‘dirty’ or in an inconsistent written state with the data programmed to be written
- The file system is mounted without checking for a specific number of times
Usually, this program works in parallel on file systems stored on different physical disk drives. The primary intention is to reduce the time taken to check all these files.
If there is an -A option or any filesystem specified on the command line, fsck will serially check filesystems in /etc/fstab by default. This is the same as the mixture of the -A and -S options.
The program returns different exit codes. These are all sums of unique numbers that represent different condition values as follows:
- 0 – No error
- 1 – File system errors corrected
- 2 – System must be rebooted
- 4 – File system errors are not corrected
- 8 – Operational error
- 16 – Syntax or usage error
- 32 – Fsck canceled as per user request
- 128 – Shared-library error
While checking multiple file systems, the exit code returned is typically the bitwise OR of the exit codes of the individual file systems that the program has checked.
You can use fsck utility program in Linux under different situations such as:
- When you want to use it to check your file systems and plan the right types of preventive maintenance measures
- When you find that there is or can be a potential issue with your file system
- When you see that system fails to boot and ant to diagnose the actual issue
- When you see an error message for the input/output that may be the result of a corrupted file system
- When you want to check and be sure about the health of your external drives such as USB flash drives, SD cards and others
However, there are some prerequisites to using fsck utility program such as:
- You will need either a Linux or UNIX-like system.
- You will need access to a command line or terminal.
- You will need root permissions.
Once you have them, you can use the basic fsck syntax in the following pattern:
The file system can be anything such as:
- A device
- A partition
- A mount point
You may also use specific options at the end of the command based on the file system.
The fsck commands help in checking and repairing the errors in inconsistent file systems interactively. These commands should be run prior to mounting a file system to read the device file on which the file system exists.
Usually, the fsck command displays information about the inconsistencies in a file system, but if the file is consistent, it will simply display the following information in its report:
- The number of files
- The number of used blocks
- The number of free blocks existing in the file system
The commands are typically quite conservative in nature when it comes to repairing the existing errors in the file system. It usually tries to abstain from any action that may result in a potential loss of data from the files.
This means that, unless it is unavoidable as it is in some cases, the command will not recommend destroying a damaged file.
However, it is always recommended that you allow the fsck command to do the required rectification so that there are no inconsistent files in your system, loading which may even result in a system crash.
The command will check all of the file systems if you do not mention a file system in the FileSystem parameter. Ideally, the check attribute is set to True for the file systems that are listed in the /etc/filesystems file.
Such checks can be initiated with the command:
If there are multiple file systems to check, you can do so by grouping them in the /etc/filesystems file and changing the check attribute of it by using the following command:
If you want minor issues in the file systems to be fixed, the necessary command for it is:
While the default file-checking command is fsck, the specific command for checking a specific file system is:
The fsck Linux utility allows using different options with it that are both file-system specific and otherwise. Ideally, the options and arguments that are not understood are passed on to the checker for the specific file system.
Here are some of the common options used in the fsck utility program along with a brief description of each:
- -a – This option is without any prompts and used for repairing filesystem errors automatically.
- -A – This option is used to check all filesystems listed in /etc/fstab.
- -C – This option shows the advancement for ext2 and ext3 filesystems.
- -f – This option forces fsck to check a filesystem even if it is clean.
- -l – This option locks the device to stop other programs from using the partition while scanning or repairing.
- -M – This option will not check the mounted filesystems and will return an exit code 0 when it is mounted.
- -N – This option will do a dry run and will print an output without carrying out any action, along with error messages or warnings.
- -P – This option is used for running a scan in parallel on multiple filesystems.
- -R – This option will not check the root file system while using the -A option.
- -r – This option will print the statistics of the device.
- -t – This option specifies the type of file system to check.
- -T – This option will hide the title while the tool is running.
- -y – This option will attempt to repair the errors in the file systems automatically while performing a check.
- -V – This option is used for verbose output.
How to Run fsck Manually?
The fsck utility program can be run manually on an unmounted file system, if there is any system error on the disk by a superuser or the system administrator.
However, it is not supposed to run on a mounted filesystem because it may display an error message or even corrupt the data on the file system.
The process to follow includes entering the command /etc/fsck -n in the UNIX shell prompt and pressing Enter. You will be able to see all the inconsistencies in the file system without repairing them.
Count the number of errors, and if you find that it is more than 20, there are some serious issues with the disk.
However, if it is less than 20, re-run the fsck command and include the -y option.
This means that all the questions asked by fsck will be affirmative, and therefore the errors will be fixed, allowing the boot process to continue.
If there are any unreferenced file, these will be kept in the lost+found directory on the disk.
At this point, you should pay specific attention to messages like the following:
REBOOT HP-UX; DO NOT SYNC (USE reboot -n).
In that case, you will have to reboot the controller by entering: reboot -n. The controller will then reboot, and everything will be fine.
If you do not get any such message, enter “^D” at the prompt. This will continue running the bcheckrc program to finish the booting process.
What is the Use of fsck?
The fsck utility program is mainly used to check the file systems and repair the inconsistencies in them interactively.
It works on the data structures directly, and it is also used to check the unmounted, mounted read-only file systems, or the systems in special maintenance mode.
Typically, different fsck commands are used to check different types of inconsistencies in the file systems, which include and are not limited to the following:
- Fragments or blocks in the multiple files
- Divergences in the link count of a file and the number of directory references to the file
- Blocks and fragments that are allocated illegally
- Files containing a fragment and are larger than 32 KB in size but not a compressed file system
- Inconsistent disk map
- Overlapping numbers of blocks and fragments in the i-nodes
- Blocks and fragment numbers on the i-nodes that are out of range
- Blocks and fragment numbers in the i-nodes that are marked free on the disk map
- Corrupted blocks and fragment numbers in the i-nodes
- A fragment in the i-node which is not the last disk address in it, if it is not in a compressed file system
- Inconsistent i-node map
The fsck commands also perform size checks in the following conditions that are not compressed file systems:
- When the block numbers are incorrect
- When the size of the directory is not a multiple of 512 bytes
Apart from that, it can also perform specific directory checks in the following conditions:
- When the directory entry contains an i-node number that is marked free in the i-node map
- The i-node number is out of range
- The Dot (.) link does not point to itself or is missing
- The Dot Dot (..) link does not point towards the parent directory or is missing
- The files or directories that are not referenced or orphaned are not reachable
Ideally, these orphaned files or directories are reconnected in the root directory of the file system by putting them in the lost+found subdirectory if you allow it.
The name is assigned in the i-node number. If you do not allow it, then the command will request authorization to destroy the file.
It is also used to check the following inconsistencies in the superblock:
- The size of the file system
- The number of i-nodes
- The free-block count
- The free-inode count
Finally, apart from the messages, the fsck command will also record the output of the different checks done by it and repair the existing error through the exit value, as mentioned earlier.
Is It Safe to Run fsck?
Typically, the fsck utility program tries its best and most valiantly not to lose data during its operation, therefore. it is quite safe to use.
However, this is not a miracle tool and therefore it depends on the type of fsck command you are using for a specific file and the damage. If the damage is reasonably ordinary, this tool is reasonably safe.
Typically, there are a few particular commands such as e2fsck for ext2/ext3/ext4 file systems, that can slow down things when the -f option is used since they will check even clean file systems.
And when the -y option is used, it will fix the errors detected automatically. It may even perform a few tasks that were not advised by the expert administrator.
This means that there is always some risk involved in running fsck commands.
Therefore, it is not a good idea to run the automatic thing always or when the underlying hardware such as the CPU, RAM, hard drive, or disk controller is damaged. It will inevitably result in data loss.
So, never, ever run fsck without inspecting the issues in the kernel and eliminating the root cause.
How Do You Turn Off fsck?
You can turn off or disable the fsck utility program on a hard disk partition by editing the /etc/fstab file if the operating system does not use SystemD.
The two values to look for are, dump and pass, where dump signifies that the partition is discarded and Pass will have it checked.
To turn it off once:
- Tap any key to load PC
- Navigate to the main kernel on the GRUB menu
- Open boot arguments by hitting ‘e’
- Go to the kernel /vmlinuz line
- Add fastboot to the end of the line
To permanently disable it on a disk partition:
- Open /etc/fstab
- Look for the line checking the partion
- Change the dump and pass values to 0 and 0
- Save and exit
Does fsck Run in the Background?
Yes, the fsck utility program can run in the background, though it is designed to run at boot time and most users think it cannot run in the background.
Derived from the conventional disk-based fsck, it typically runs by taking snapshots and running traditional algorithms.
However, it cannot update the on-disk image or obtain kernel-level locks.
Can fsck Cause Data Loss?
Yes, there is a high chance of losing data from a disk when you use the fsck utility program while the disk is active.
You should always unmount the target disk before using this program to check and repair errors in it.
When Should You Fix Errors Using fsck?
You may also use it when the files are corrupted on a particular disk.
So, through this article, you now know what the fsck utility program on Linux is and how to use it to your advantage to keep a check on your file systems and even repair the potential issues existing in them.
Simply have root permission and run the commands listed in this article to run this useful tool on your system.