Marcio Cunha

How to revive hard drives with bad sectors on Linux

Learn how to use low-level Linux commands to reallocate damaged blocks and rescue files before a total hard drive failure occurs.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • Bad sectors occur due to natural physical wear of the drive or minor magnetic instabilities on the platter surfaces.
  • The SMART utility monitors internal hard drive health and warns about increasing block reallocation rates.
  • Forced read commands allow extracting important data from critical areas before the system loses access permanently.
  • Forced reallocation replaces unstable blocks with reserve sectors managed by the hard drive firmware itself.
  • Maintaining updated backup copies remains the only true protection against permanent data loss on mechanical storage.

What are bad sectors and why do they appear on drives

Traditional hard drives, known as HDDs, work by writing data to spinning magnetic platters using a microscopic read and write head. Over the years, natural mechanical wear, excessive vibration, and the aging of the magnetic material start taking their toll. When a small region of the drive loses its ability to retain a magnetic charge stably, what we call a bad sector or bad block emerges.

In practice, this means the operating system tries to read an important file but hits a mute spot where information has corrupted or vanished. Most of the time, the file system detects the issue, reports a read error, and temporarily freezes. Before discarding the hardware or attempting a drastic swap, you can use native Linux tools to diagnose the severity of the damage, isolate problematic areas, and extract vital files, much like how we organize incremental backup routines to mitigate future losses.

How to diagnose real drive health with SMART

Before running any destructive commands or repair attempts, we need to ask the drive itself how it is feeling. Modern drives feature an internal monitoring system called SMART, which stands for Self-Monitoring, Analysis, and Reporting Technology. This system logs vital statistics about mechanical operation, such as hours of use, temperature, and the exact number of failed sectors.

To check this data in Linux, we use the smartmontools package through the terminal. The basic command to inspect a specific drive, such as /dev/sda, is executed with administrative privileges. In practice, we look closely at attributes like Reallocated_Sector_Count, which indicates how many bad blocks have been replaced by reserve sectors, and Current_Pending_Sector_Count, which shows unstable sectors waiting for a reallocation decision.

sudo smartctl -a /dev/sda

How to extract data from damaged areas using ddrescue

When a hard drive starts showing severe failures, attempting to copy ordinary files using the graphical file manager is the shortest path to destroying what remains. The head movement trying to reread damaged blocks generates heat and accelerated wear, locking up the operating system. To avoid this unnecessary stress, the ideal tool in the Linux ecosystem is ddrescue, designed specifically to recover data from media with physical defects.

Unlike the traditional dd command, which gives up upon encountering the first read error, ddrescue uses an intelligent two-phase algorithm. First, it quickly copies all healthy drive areas, leaving problematic regions for last. In the second phase, it returns to the corrupted sections and applies multiple reverse attempts to extract the maximum readable bits before the motor stops spinning.

sudo ddrescue -d -r3 /dev/sda /mnt/backup/disk_image.img /mnt/backup/error_map.log

How to force bad block reallocation with the badblocks utility

After rescuing important data and saving what was critical, we can try to recover temporary drive usability by forcing the firmware to reallocate defective blocks. Linux features the badblocks tool, capable of testing the magnetic surface destructively or non-destructively. The non-destructive mode reads and writes data back, but to force a real clean and trigger factory remapping, the destructive write mode is the most efficient.

In practice, the command executes test patterns across the entire block extent. When the drive controller notices that a write failed on a specific sector, it automatically marks that address as unusable and replaces it with a reserve sector kept in an isolated drive area. Keep in mind that this operation requires extreme caution, as any error in identifying the drive could erase the main operating system.

sudo badblocks -v -s -w /dev/sda

How to validate file system integrity after repair

After mapping and reallocating bad sectors, the file system on top of the partition can become inconsistent. Metadata and allocation tables pointing to corrupted blocks need correction so the operating system can resume stable operation. For this task, we use the fsck command, which checks and repairs corrupted structures in ext4, xfs, and other formats supported by the kernel.

It is crucial to run fsck with the partition properly unmounted, because trying to repair an actively used file system results in catastrophic data corruption. During the process, the utility sweeps through blocks, reconnects orphan files to lost folders, and adjusts internal pointers, preparing the ground for the hardware to regain basic operational functionality.

sudo umount /dev/sda1 && sudo fsck -y -f /dev/sda1

Final considerations on the lifespan of recovered drives

Reviving a hard drive through bad sector reallocation and low-level command rescue is an excellent emergency strategy to save crucial files or keep legacy systems running temporarily. However, you must view this procedure as a palliative measure and never as a definitive solution. A drive that has started developing mechanical bad blocks indicates physical wear has reached a point of no return, and cascading failures are likely to happen soon. The best engineering practice remains the preventive replacement of the damaged unit and rigorous maintenance of automated backup routines.