How to check for bad sectors using badblocks

Nam Luong
Nam Luong
  • Updated

Document Scope 

This article demonstrates how to use the badblocks utility on a Linux system. Badblocks is a command-line utility in Linux used to search for bad sectors on a disk. It can identify unreadable sectors that may indicate failing hardware. This tool is especially useful for preemptively detecting disk errors and preventing data loss.

How badblocks Works

The badblocks utility scans a specified disk or partition and tests each block for read and write errors. It can operate in read-only or read-write mode. While the read-only mode is safer for data integrity, the read-write mode is more thorough as it writes a pattern to each block and then reads it back to verify the data.

Installing badblocks

badblocks is typically included with the e2fsprogs package, which is often pre-installed on many Linux distributions. If it's not already installed, you can install it using your package manager or check the version that already installed.

$ dpkg -l | grep e2fsprogs
ii e2fsprogs 1.46.5-2ubuntu1.1 amd64 ext2/ext3/ext4 file system utilities

For Debian-based distributions (e.g., Ubuntu):

sudo apt update
sudo apt install e2fsprogs

For Red Hat-based distributions (e.g., CentOS, Fedora):

sudo yum install e2fsprogs

Common Usage Examples

1. Check a Disk for Bad Blocks in Read-Only Mode

This mode is safe as it doesn’t modify any data on the disk.

EXAMPLE: Replace /dev/sdX with the appropriate device identifier (e.g., /dev/sda, /dev/sdb1).

$ sudo badblocks -v /dev/sdX
Checking blocks 0 to 1048575
Checking for bad blocks (read-only test): done
Pass completed, 0 bad blocks found. (0/0/0 errors)

2. Specifying the Block Size

If you need to specify a different block size (e.g., 4096 bytes), you can use the -b option.

EXAMPLE:

$ sudo badblocks -v -b 4096 /dev/sdX
Checking blocks 0 to 262143
Checking for bad blocks (read-only test): done
Pass completed, 0 bad blocks found. (0/0/0 errors)

Using 'badblocks' with 'e2fsck'

If you find bad blocks on a disk, you can mark them as bad in the filesystem metadata so that they are not used for storing data. You can do this using the e2fsck utility, which is also part of the e2fsprogs package.

sudo badblocks -v /dev/sdX > badblocks.txt
sudo e2fsck -l badblocks.txt /dev/sdX

Was this article helpful?

0 out of 0 found this helpful

Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.