Hard Disk Bad Sector Detection and Repair on Linux
A comprehensive guide to checking for bad sectors on Linux hard drives using smartctl, badblocks, and understanding S.M.A.R.T. data.
Introduction
A hard drive clicking, grinding, or simply returning I/O errors is every system administrator's nightmare. Bad sectors on a hard disk can range from a minor nuisance to a sign of imminent failure. Learning to detect them early, understand what the S.M.A.R.T. data is telling you, and taking appropriate action can save you from catastrophic data loss. This post covers the tools and techniques available on Linux for bad sector detection and remediation.
Environment
The examples in this post use a Seagate IronWolf 4TB NAS drive connected via SATA on a Debian 12 system with smartmontools 7.3 installed. The principles apply equally to any HDD or SSD, though the specific S.M.A.R.T. attributes differ between drive types.
smartctl --version
# smartctl 7.3 2022-02-28 r5338
lsblk -d -o NAME,SIZE,MODEL
# NAME SIZE MODEL
# sda 4T IronWolf ST4000VN006Problem
The signs of bad sectors can be subtle at first. You might notice:
- Slow file reads or writes in specific locations
- I/O errors in kernel logs
- Applications hanging during disk operations
Check the kernel ring buffer for disk errors:
dmesg | grep -i "error\|sector\|bad\|reset"
# [ 123.456] ata1.00: failed command: READ FPDMA QUEUED
# [ 123.456] ata1.00: cmd 60/00:00:a0:32:20/00:04:00:00:00/40 tag 27 pio 524288 in
# [ 123.456] res 51/40:00:a0:32:20/00:04:00:00:00/40 Emask 0x9 (media error)
# [ 123.457] ata1.00: status: { DRDY ERR }
# [ 123.457] ata1.00: error: { UNC }The UNC (uncorrectable) error is a clear indicator of a bad sector.
Analysis
S.M.A.R.T. (Self-Monitoring, Analysis, and Reporting Technology) drives track their own health. The key attributes to watch for bad sectors are:
- Reallocated Sector Count (ID 5): Sectors remapped from the bad area to the spare area
- Current Pending Sector Count (ID 197): Sectors waiting to be remapped
- Uncorrectable Sector Count (ID 198): Sectors that could not be recovered
You can check these with smartctl:
sudo smartctl -A /dev/sda
# ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE
# 5 Reallocated_Sector_Ct 0x0033 100 100 010 Pre-fail Always - 0
# 197 Current_Pending_Sector 0x0032 100 100 000 Old_age Always - 0
# 198 Offline_Uncorrectable 0x0030 100 100 000 Old_age Offline - 0A Raw_Value greater than zero for any of these attributes is a red flag.
Solution
1. Run a full S.M.A.R.T. self-test
# Start an extended self-test (can take several hours)
sudo smartctl -t long /dev/sda
# Check the results after completion
sudo smartctl -l selftest /dev/sda2. Use badblocks for a destructive test
Warning: This destroys all data on the device.
sudo badblocks -wsv -o /tmp/badblocks.log /dev/sdaOptions:
-w: Write test pattern (destructive)-s: Show progress-v: Verbose output-o: Output log file
3. Use badblocks for a non-destructive read-only test
sudo badblocks -nsv /dev/sdaThis reads every block and checks for read errors without modifying data.
4. Map bad sectors with dd
For individual files that trigger errors, use ddrescue to skip bad areas:
sudo ddrescue -f -n /dev/sda /mnt/backup/disk_image.img /mnt/backup/rescue.log5. Mark bad sectors in the filesystem
With ext4:
# Find the inode associated with the bad sector
sudo e2fsck -l /dev/sda1
# Force a filesystem check to mark bad blocks
sudo e2fsck -fy /dev/sda1With xfs:
sudo xfs_repair /dev/sda16. Set up continuous monitoring
# Add to crontab
0 */6 * * * /usr/sbin/smartctl -H /dev/sda | grep -q "PASSED" || echo "SMART health check FAILED" | mail -s "Disk Alert" admin@example.com
# Enable smartd daemon
sudo systemctl enable smartd
sudo systemctl start smartdConfigure /etc/smartd.conf:
/dev/sda -a -o on -S on -s (S/../.././02|L/../../6/03) -m admin@example.comLessons Learned
Bad sectors on an HDD are often a precursor to complete failure. A few reallocated sectors may be manageable, but increasing counts over time mean the drive should be replaced immediately. Always maintain backups, monitor S.M.A.R.T. data proactively, and do not wait for I/O errors to appear in application logs. For SSDs, pay attention to Media_Wearout_Indicator and Available_Reservd_Space instead.
This blog does not accept any external sponsorships, affiliate marketing, or ad revenue.