Fix Hard Disk Bad Sectors in Linux

Bad Sector in a Hard Drive is a physical problem.  If bad sectors start appearing, it’s time to change the hard drive. Every OS has its own tools to scan and fix bad sectors. For example Windows has chkdsk application. Here we will discuss how to fix bad sectors in Linux.

  1. Download Ubuntu ISO and burn it on CD, DVD or a USB drive. If you have any other Linux live CD, that would also work.
  2. Boot system with the CD or USB created in step-1.
  3. Open a terminal window.
  4. Run command fdisk -l to find out the hard drive and partition device names.
  5. Type following command to run fix bad sectors application. Note: This command requires sudo privileges. Replace sda1 with the partition device name found in step-4.

            sudo e2fsck -cfpv /dev/sda1

The parameters have the following meanings: “c” searches for bad blocks and adds them to the list, “f” forces a check on the file system, “p” repairs anything that can be safely repaired and “v” is verbose mode so you can see the command progress.

This command can take a long time to run, even several hours on a particularly large drive.

Example:

ubuntu@ubuntu:~$ sudo fdisk -l
Disk /dev/loop0: 1.4 GiB, 1532116992 bytes, 2992416 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/sda: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x032959e3

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 1945352191 1945350144 927.6G 83 Linux
/dev/sda2 1945354238 1953523711 8169474 3.9G 5 Extended
/dev/sda5 1945354240 1953523711 8169472 3.9G 82 Linux swap / Solaris


Disk /dev/sdb: 28.7 GiB, 30752000000 bytes, 60062500 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x05160d8f

Device Boot Start End Sectors Size Id Type
/dev/sdb1 * 2048 60062499 60060452 28.7G c W95 FAT32 (LBA)


ubuntu@ubuntu:~$ sudo e2fsck -cfpv /dev/sda1
/dev/sda1: Updating bad block inode.

191888 inodes used (0.32%, out of 60792832)
 60 non-contiguous files (0.0%)
 194 non-contiguous directories (0.1%)
 # of inodes with ind/dind/tind blocks: 0/0/0
 Extent depth histogram: 144736/6
 4952754 blocks used (2.04%, out of 243168768)
 11 bad blocks
 1 large file

110870 regular files
 16787 directories
 55 character device files
 25 block device files
 0 fifos
 2 links
 64140 symbolic links (47056 fast symbolic links)
 2 sockets
------------
 191881 files
ubuntu@ubuntu:~$

Remove the live Linux CD or bootable USB and reboot the system.

13 thoughts on “Fix Hard Disk Bad Sectors in Linux

  1. How much time did it take for the command sudo e2fsck -cfpv /dev/sda1 to finish?
    I see the partition being scanned and fixed is 927gb. I have a similar size.

  2. Your post does not explain how to tell which sectors are bad…I’m not sure what the fdisk -l is showing me other than information on the disks. For me, it shows lots of “loop##”s and one “sdb”. Not sure what to make of all this information

  3. Adam, please don’t blame the author for your ignorance about this matter.
    $ fdisk -l
    Will “list” for you all the physical devices connected to your filesystem, and per each of them, the partitions (logical devices).
    – /dev/loop* are virtual devices, like a ramdisk, you never have to be worried about them.
    – Then you see there two physical devices:
    – sda, with one primary partition (type 83, which indicates it can be ext2/3/4), and a extended partition with a logical one inside for Linux swapping.
    – sdb, with one FAT32 primary partition.
    You use that command to know your partitions, so you can pick what you need to analyze, in the example the root one, /dev/sda1..
    Hope it helps, regardless of the time..

  4. Why does author tells to use e2fsck instead of fsck, how did author assume ext2 file system in 2018?

  5. The superblock could not be read or does not describe a valid ext2/ext3/ext4
    filesystem. If the device is valid and it really contains an ext2/ext3/ext4
    filesystem (and not swap or ufs or something else), then the superblock
    is corrupt, and you might try running e2fsck with an alternate superblock:
    e2fsck -b 8193
    or
    e2fsck -b 32768

  6. Alex: This error can be generated by a corrupt filesystem, but it can also be generated by trying to run e2fsck on (e.g.) an NTFS filesystem. “fsck” is supposed to figure out what sort of filesystem you have and run the appropriate program. See the man page.

  7. can i use it for exfat?
    or can i repair bad blocks and format the partition to exfat or another format?

  8. $ sudo e2fsck -cfpv /dev/sda

    e2fsck: Bad magic number in super-block while trying to open /dev/sda
    /dev/sda:
    The superblock could not be read or does not describe a valid ext2/ext3/ext4
    filesystem. If the device is valid and it really contains an ext2/ext3/ext4
    filesystem (and not swap or ufs or something else), then the superblock
    is corrupt, and you might try running e2fsck with an alternate superblock:
    e2fsck -b 8193
    or
    e2fsck -b 32768

    Can anyone tell me what happens with the ssd?

  9. You have to format the drive first to a Linux files system. You can use the “Disks” application for that, no need to do it in the Terminal.

  10. So if the hard disk containing linuxlite os, is showing :
    e2fsck: Invalid argument while trying to open /dev/sda1
    The super block could not be read or does not describe a valid ext2/ext3/ext4 filesystem. If the device is valid and it really contains an ext2/ext3/ext4 filesystem (and not swap or ufs or something else), then the super block is corrupt, and you might try running e2fsck with an alternate superblock:
    e2fsck -b 8193
    or
    e2fsck -b 32768

Leave a Reply

Your email address will not be published. Required fields are marked *

*