Scan and Repair Disk Bad Sectors in Ubuntu Linux, Fedora

3 min


There are terminal utilities available in Linux which can help you to manage hard disk bad sectors. You can scan and mark them as unusable as well using these utilities.

Bad sectors or bad blocks are damaged portion of your mechanical hard disk drive which can not be used at all for data storing purposes. However, the operating system still can write to those sectors unless you specifically mark them as ‘bad’ or unusable. Also if you have data stored in those areas of the hard drive, it is very difficult to recover those as well.

Although, the latest computer storage technology such as SSD, etc almost eliminates this problem. However, there is still a huge number of hard disk drives in use today which is aging and might slowly start having bad sectors. 

Hence, you should periodically scan your hard drive (especially aging ones) for bad sectors if you feel your system is slowing down, or, disk IO is increasing. In Linux (Ubuntu, Fedora, and other distributions), you can easily do this via below terminal commands. 

How to Manage Disk Bad Sectors in Linux

It is better to run below commands when your disk is not mounted with the operating system. Hence I would suggest, you try this using LIVE operating system boot from a USB stick. You can create a LIVE USB using this guide with any Linux operating system of your choice (recommended: Ubuntu).

However, you can still run these commands in your installed Linux distribution but you should not scan or mark the mounted “/” root filesystem.

Scan for Bad Sectors

As a first step, identify the disk partition which you want to scan for bad sectors. If you have GParted installed, it is easy to find out. Otherwise, you can run below command (lsblk – List block devices) to view your disk partitions. 

sudo lsblk -o name,mountpoint,label,size,uuid

If you are running above command via LIVE USB, make sure you can identify your HDD and USB stick. Typically HDD should be defined as /dev/sda.

Then you can run badblocks command as below with the verbose (-v) switch. And save the output to a text file for further investigation. This is just a verification whether you have bad sectors in the hard drive or not. 

sudo badblocks -v /dev/sda1 > ~/bad_sectors.txt

Repair Bad Sectors

For ext2, ext3, and ext4 file systems, you can use e2fsck utility to check and repair bad sectors. In the terminal run below command with admin privilege to check and repair. 

sudo e2fsck -cfpv /dev/sda1

Make sure to replace sda1 with the proper device identifier. The parameters “c” searches for bad blocks and add it to a list, “f” does a check on the file system. The “p” parameter repairs anything if possible and “v” is the verbose mode which gives you the terminal output of the command progress.

You can also specify the bad_sectors.txt file created in the earlier steps as well to force e2fsck to repair those in the file only via the below command.

sudo e2fsck -l bad_sectors.txt /dev/sda1

For other file systems (such as FAT32), you can use fsck.

sudo fsck -l bad_sectors.txt /dev/sda1

However, the above command execution might take several hours to run depending on your disk partition size and health of your disk. So be ready before you start the command. Try not to terminate the command via CTRL+C or CTRL+Z while it is in progress. 

I hope this tutorial helped you to identify issues in your hard drive and mark them as bad to prevent further data loss. Remember that even if you mark the sectors as ‘bad’, the disk is still physically damaged already. It is merely a software fix to mark those tracks as bad to tell the operating system not to access. If your hard disk started having bad sectors, in the long run, it would become worse. Hence is it recommended that you should start backing up your data and replace your hard drive with a new one or go for the latest SSD drives.


We bring the latest tech, software news and stuff that matters. Stay in touch via Telegram, Twitter, YouTube, and Facebook and never miss an update!

Join our Telegram channel and stay informed on the move.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.


Arindam

Creator of debugpoint.com. All time Linux user and open-source supporter. Connect with me via Telegram, Twitter, LinkedIn, or send us an email.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x