Skip to main content

How to Identify and Fix Bad Sectors on Your Hard Drive

Guide: How to Identify and Fix Bad Sectors on Your Hard Drive (for Linux systems including OpenMediaVault).

Download script

Online command to download and execute the script from online source.

curl -sSL https://docs.greenhome.stream/attachments/30 -o find_fix_bad_sectors.sh && chmod +x find_fix_bad_sectors.sh && ./find_fix_bad_sectors.sh

English Version

What are Bad Sectors? Bad sectors are parts of your hard drive that have become unreliable or unreadable due to physical damage or data corruption. If bad sectors are not dealt with, they can cause data loss or system instability.

Step 1: Identify Bad Sectors
  • Use the Linux tool smartctl (part of smartmontools) to check the health of your drive and see if there are any "pending" or "unreadable" sectors.

Example command:

sudo smartctl -a /dev/sdX

Replace /dev/sdX with your actual drive (e.g., /dev/sdc).

  • Look for the attribute Current_Pending_Sector which shows how many sectors are currently unreadable but not yet reallocated.
  • For a deeper scan of the disk surface, use badblocks to find bad sectors (non-destructive read-only scan recommended):
sudo badblocks -sv /dev/sdX > badsectors.txt

This will scan your drive and save any identified bad sector numbers to badsectors.txt.

Step 2: Fix or Relocate Bad Sectors
  • Hard drives automatically reallocate bad sectors by mapping them to spare sectors, but only when the system writes to those sectors.
  • You can force this reallocation by explicitly writing to the bad sectors using two methods:

  • Using hdparm to overwrite a specific sector:

sudo hdparm --write-sector [sector_number] --yes-i-know-what-i-am-doing /dev/sdX

Replace [sector_number] with the exact bad sector number and /dev/sdX with your drive. This command forces the drive firmware to remap the sector if faulty.

  1. Using dd to write zeros to the sector:
sudo dd if=/dev/zero of=/dev/sdX bs=512 count=1 seek=[sector_number]

Also replace [sector_number] and /dev/sdX. This will overwrite the sector and trigger reallocation if needed.

  • Repeat these commands for all bad sectors found by badblocks or shown in the SMART report.
Step 3: Verify and Monitor
  • After forcing sector writes, check your drive health again:
sudo smartctl -a /dev/sdX
  • The number of Current_Pending_Sector should decrease and Reallocated_Sector_Ct may increase, meaning the drive successfully remapped bad sectors.
  • Keep backing up important data frequently — bad sectors often indicate a drive that's failing and replacement is recommended.
Important Notes
  • Always back up your data before attempting these operations, as writing to bad sectors can cause data loss.
  • Running destructive tests like badblocks in write mode (-w) erases all data — only do this on empty or fully backed-up drives.
  • If bad sectors keep appearing or increasing, it is a sign to replace your hard drive promptly.

Greek Version (Ελληνική Έκδοση)

Τι είναι οι Κακοί Τομείς (Bad Sectors); Οι κακοί τομείς είναι περιοχές στον σκληρό δίσκο που έχουν γίνει μη αξιόπιστες ή μη αναγνώσιμες λόγω φυσικής φθοράς ή καταστροφής δεδομένων. Αν δεν αντιμετωπιστούν, μπορεί να προκαλέσουν απώλεια δεδομένων ή αστάθεια συστήματος.

Βήμα 1: Εντοπισμός Κακών Τομέων
  • Χρησιμοποιήστε το εργαλείο Linux smartctl (μέρος του smartmontools) για να ελέγξετε την υγεία του δίσκου και να δείτε αν υπάρχουν "pending" ή "μη αναγνώσιμοι" τομείς.

Παράδειγμα εντολής:

sudo smartctl -a /dev/sdX

Αντικαταστήστε το /dev/sdX με τη σωστή συσκευή του δίσκου σας (π.χ. /dev/sdc).

  • Εντοπίστε την παράμετρο Current_Pending_Sector που εμφανίζει τον αριθμό τομέων που είναι προς το παρόν μη αναγνώσιμοι αλλά δεν έχουν ακόμα επανατοποθετηθεί.
  • Για πιο λεπτομερή έλεγχο, χρησιμοποιήστε το badblocks για σάρωση επιφάνειας δίσκου (προτεινόμενη ανάλυση χωρίς καταστροφή δεδομένων):
sudo badblocks -sv /dev/sdX > badsectors.txt

Αυτό σαρώνει τον δίσκο και αποθηκεύει τους αριθμούς των κακών τομέων στο αρχείο badsectors.txt.

Βήμα 2: Επιδιόρθωση ή Μεταφορά Κακών Τομέων
  • Οι σκληροί δίσκοι αυτόματα μεταφέρουν (reallocate) τους κακούς τομείς σε εφεδρικές θέσεις μόνο όταν γίνει εγγραφή σε αυτούς.
  • Μπορείτε να αναγκάσετε αυτή την επανεκχώρηση γράφοντας άμεσα στους κακούς τομείς με δύο τρόπους:

  • Με hdparm για να επανεγγράψετε έναν συγκεκριμένο τομέα:

sudo hdparm --write-sector [αριθμός_τομέα] --yes-i-know-what-i-am-doing /dev/sdX

Αντικαταστήστε το [αριθμός_τομέα] με τον ακριβή αριθμό τομέα και το /dev/sdX με τη συσκευή του δίσκου σας. Η εντολή αυτή αναγκάζει το firmware του δίσκου να κάνει remap αν ο τομέας είναι κατεστραμμένος.

  1. Με dd για να γράψετε μηδενικά στον τομέα:
sudo dd if=/dev/zero of=/dev/sdX bs=512 count=1 seek=[αριθμός_τομέα]

Επίσης αντικαταστήστε τα κατάλληλα πεδία. Αυτό θα αναγκάσει το δίσκο να επανατοποθετήσει κακούς τομείς.

  • Επαναλάβετε για κάθε κακό τομέα που βρέθηκε με badblocks ή αναφέρεται στην έκθεση SMART.
Βήμα 3: Επιβεβαίωση και Παρακολούθηση
  • Μετά τις ενέργειες αυτές, ελέγξτε πάλι την υγεία του δίσκου:
sudo smartctl -a /dev/sdX
  • Ο αριθμός Current_Pending_Sector πρέπει να μειωθεί και ο Reallocated_Sector_Ct να αυξηθεί, που σημαίνει ότι ο δίσκος έκανε επιτυχημένα remap στους κακούς τομείς.
  • Συνεχίστε να κρατάτε back up τα σημαντικά σας δεδομένα συχνά, καθώς η παρουσία κακών τομέων συνήθως σηματοδοτεί πως ο δίσκος αρχίζει να αποτυγχάνει και συνιστάται αντικατάσταση.
Σημαντικές Σημειώσεις
  • Πριν κάνετε οποιεσδήποτε ενέργειες, δημιουργήστε αντίγραφα ασφαλείας (backup) των δεδομένων σας, γιατί η εγγραφή σε κακούς τομείς μπορεί να προκαλέσει απώλεια δεδομένων.
  • Η χρήση του badblocks με επιλογή εγγραφής (-w) διαγράφει ολόκληρο το περιεχόμενο του δίσκου — να γίνεται μόνο σε κενό ή πλήρως ασφαλισμένο δίσκο.
  • Αν οι κακοί τομείς αυξάνονται ή παρουσιάζονται ξανά, είναι σημάδι ότι πρέπει να αντικαταστήσετε τον δίσκο.
Download the script find_fix_bad_sectors.sh

Or create a new find_fix_bad_sectors.sh and copy/paste the below code.

#!/bin/bash

# Define device
DEVICE="/dev/sdC"

# Run badblocks to find bad sectors and save to badsectors.txt
sudo badblocks -sv "$DEVICE" > badsectors.txt

# Check if badsectors.txt is not empty
if [ -s badsectors.txt ]; then
    echo "Bad sectors found, running hdparm commands..."
else
    echo "No bad sectors found."
    exit 0
fi

# Read each sector from the file and execute hdparm command
while read -r sector; do
    # Remove any whitespace or invalid lines
    if [[ "$sector" =~ ^[0-9]+$ ]]; then
        sudo hdparm --write-sector "$sector" --yes-i-know-what-i-am-doing "$DEVICE"
    fi
done < badsectors.txt

Then make it executable by running the below command.

chmod +x find_fix_bad_sectors.sh

Ins0mniA