find bad sectors in linux

How to Check Bad Sectors in Linux

Bad sectors are those disk sectors that cannot be read/written to anymore. It may be due to physical damage or overuse. As bad sectors keep growing the amount of available disk space on your HDD or USB drive goes on decreasing. If left unchecked, it may crash your disk. So it is important to keep an eye on the bad sector blocks on your disk, to know when you need to replace it. There are several tools available for this purpose. In this article, we will learn how to check bad sectors in Linux.

How to Check Bad Sectors in Linux

Here are the steps to check bad disk sectors.

1. Using badblocks

Badblocks is a useful utility to keep track of bad sectors on your disk. Here is the command to install it.

## Debian/Ubuntu
$ sudo apt-get install e2fsprogs

## RHEL/CentOS
$ yum install e2fsprogs

Before you use badblocks, use fdisk to list all your drives.

$ fdisk -l

The above command will list all the available drives on your system. Let us say you want to check bad sectors on disk /dev/sda2. In such cases, run the following command to identify bad blocks on the device and save the output to badsectors.txt file. Replace /dev/sda2 with your device location.

$ sudo badblocks -v /dev/sda2 > badsectors.txt

If you see any bad sectors in the output, you will need to use tools like e2fsck (for ext2/ext3/ext4) and fsck with badsectors.txt file to instruct your operating system not to write to those bad sectors.

# For ext2/ext3/ext4 file-systems ------------ 
$ sudo e2fsck -l badsectors.txt /dev/sda10


# For other file-systems ------------ 
$ sudo fsck -l badsectors.txt /dev/sda10

2. Using Smartmontools

This method is applicable for modern disks (ATA/SATA and SCSI/SAS hard drives and solid-state drives) that have a (Self-Monitoring, Analysis and Reporting Technology) system to detect, report and log their health status so that you can easily find out any disk health issues. Here is the command to install smartmontools.

------------ On Debian/Ubuntu based systems ------------ 
$ sudo apt-get install smartmontools

------------ On RHEL/CentOS based systems ------------ 
$ sudo yum install smartmontools

Once the smartmontools are installed use smartctl to manage these tools. You can use this command to quickly check the health of your disk.

$ sudo smartctl -H /dev/sda2

If you want overview of your disk, use -A option.

$ sudo smartctl -A /dev/sda2

If you want S.M.A.R.T related information, you can use -X option. In this article, we have learnt how to find bad sectors on your disk in Linux.

Also read:

How to Convert Markdown to HTML in Linux
How to Create SFTP User for Specific Directory
How to Open File Dialog Box in Python
How to Share Folders Between Linux Systems
How to Disable SELinux in CentOS & RHEL

Leave a Reply

Your email address will not be published. Required fields are marked *