how to check bad sectors in hdd in ubuntu

How to Check Bad Sectors in HDD in Ubuntu

Bad sectors and blocks are those areas of your hard disk that cannot be read from or written to anymore, due to physical damage or other reasons. As bad sectors increase on your disk, its performance becomes worse over time. If left unchecked, they can even cause complete disk failure. So it is important to check bad sectors in HDD from time to time, and mark them as unusable, and start looking for a new disk, if possible. This is an important requirement for many system administrators. In this article, we will learn how to check bad sectors in HDD in Ubuntu using certain disk scanning tools.


How to Check Bad Sectors in HDD in Ubuntu

Here are a couple of ways to scan hard disk for bad sectors and blocks.


Using badblocks

You can use badblocks program to easily scan your disk for bad sectors and blocks. It can be used with hard disk or an external disk drive.

First, we will use fdisk command to list all our disk partitions available.

$ sudo fdisk -l

You will see a list of all disk partitions along with the mount path. For example, if you want to check bad sectors/blocks on partition /dev/sda1, run the following command.

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

In the above command, we use -v option to display the progress of scanning operation. We have also specified to store result of the scan operation in badsectors.txt file.

Once the scanning is complete, view the badsectors.txt file. If you find any bad sectors mentioned in the output file, unmount the partition and mark the bad sectors as unusable.

For this purpose, you need to use e2fsck (for ext2/ext3/ext4 file systems) or fsck command with the badsectors.txt file and the device file as shown in the command below.

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

OR

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

We use -l option to tell these commands to add bad sectors & blocks from the file badsectors.txt file. These commands will automatically unmount the specified partition, read the list of bad sectors and blocks from badsectors.txt file and mark them as unusable.


Using Smartmontools

You can also use smartmontools to look for bad blocks and sectors on modern disks such as ATA/SATA and SCSI/SAS hard drives and solid-state drives.

Here is the command to install smartmontools on your system.

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

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

Once it is installed, use -H or –health option to display the health of specific disk.

$ sudo smartctl -H /dev/sda1

If your disk is healthy, you will see the following message.

SMART overall-health self-assessment test result: PASSED

You can also use -a or –all option to display all information about the disk.

You can view all available options for smartmontools using its man pages.

$ man smartctl
$ smartctl -h

In this article, we have learnt how to check bad sectors or blocks in HDD in Ubuntu. In fact, it is important to regularly check your disks for such bad sectors and blocks, to discover issues early on. You can run these commands as cronjobs or shell scripts to automatically perform checks on a regular basis and inform you of issues.

Also read:

How to Encrypt & Decrypt Files using OpenSSL
How to Add New SSH Key in GitHub
pgAdmin Connect via SSH Tunnel
How to Connect to PostgreSQL Server via SSH Tunnel
How to Connect to MySQL via SSH Tunnel in Windows

Leave a Reply

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