Learning how to check disk for errors on Linux is the easiest way to safeguard your system from abrupt data loss—we’re here to guide you through it.
A disk on your computer system may encounter different types of errors depending on the extent of the problem. For instance, the entire disk may be corrupted; there can be faulty sector or partition issues. Whatever the cause of the problem, it is essential to identify the type of error and resolve it to prevent any further issues.
There can be severe issues, such as the whole disk being corrupted or losing your essential data, if the proper steps are not taken earlier to identify, locate and resolve these issues. In this guide, we will discuss how we can check and fix disk errors in Linux.
Common types of disk errors
There can be a variety of issues with disks. Common disk errors are physical failures, bad sectors or blocks, and incompatible file systems. These errors can be identified using standard utilities of Linux, as discussed in the following sections. Also, the disk should not be mounted when performing these scans.
Usually, when the users are logged in, you can’t unmount the disk. So the standard approach is to use an installer disk and boot from it. Alternatively, there is another option to boot the system in rescue mode.
Tip: To check for the list of the currently mounted disk, you can use the following command:
How to Check Disk for Errors on Linux
Now we will discuss how we can check the disk for errors using various utilities. All the commands that will be used in this section should be run as the root user. We will use the following command to check for disk problems:
badsector: A utility to check for bad sectors in Linux
S.M.A.R.T: Monitor the hard drive’s reliability and predict drive failures
fsck: It is a command for file system consistency check. It attempts to identify file system errors and then repair and make the possible fix. It is primarily a front end for a range of utilities in Linux to check the disk
Now, take the following steps to ensure the reliability of your disk:
As the first step, open the Terminal by clicking the system menu and selecting the menu item ‘Terminal’.
Choose a partition to check for errors
Then, you should select the partition to check for errors. The following command can be used to list down the partitions in your system:
$ sudo fdisk -l
After executing this command, all the partitions in your system will be shown. Choose the partition you want to work on further from this list to check for disk errors. Alternatively, you can use the following command to list down the drive on your system:
Scan and save the errors in a text file
Now, we will perform the scanning of the selected partition using the following command and redirect the list of errors to a file:
$sudo badblocks -v /dev/sda > /errors/errors.txt
In the above command,
/dev/sda is the partition to work on. Using the redirection operator ‘
>’, we have saved the output to the file errors.txt. The command will look for all the bad sectors in your system.
Ensure the disk is not mounted while performing the scan of disk errors. If the disk is mounted, you can unmount it by running the following command:
$ sudo unmount /dev/sda
Pass the errors file through the fsck utility
In the next step, we will pass the errors file through the
fsck utility. Run the following command on your Terminal:
$ sudo fsck -l/errors/errors.txt /dev/sda
The above command uses the switch
–l to pass the
errors.txt file to
fsck utility. This ensures that the system doesn’t use the marked bad sectors in the future. This will avoid the issues encountered in using and considering those particular sectors as error-free.
Alternatively, you can use a wide range of other options with
–a option to let the
fsck attempt to fix the errors,
-f to force check the file system even if it is clean. You can use
–P to run multiple checks in parallel. Use
–y to run the
fsck in interactive repair mode.
fsck command returns various error codes as the output of the execution of the command. The meaning of these error codes is as follows:
- 0 – No errors
- 1 – File system errors corrected
- 2 – System should be rebooted
- 4 – Errors left uncorrected
- 8 – Operational error
- 16 – Usage error
- 32 – Command canceled by the user
- 128 – Shared library error
Check S.M.A.R.T health status using smarctl
Now check the health status of the disk using the
smarctl command. The following command illustrates the health check usage:
$sudo smartctl -H /dev/sda
In this guide, we have discussed how we can check disk errors in Linux. We discussed how we identify the list of bad sectors in the system and how we can pass the list of bad sectors to
fsck utility to mark and avoid using them in the future. We also covered the smart tool that can predict disk failures.
Besides, other smart monitoring tools can be used for checking and fixing disk errors.
If this guide helped you, please share it.