4 commands to erase disks on Linux

Sometimes you may need to delete or format the hard disk for certain operations. The most common use case for completely and irrevocably erasing a device is a device that will be donated or sold, as this is really important for data protection. There are some commands to help you perform these operations in Linux.

This article shows 4 commands to erase hard disk or partition data on Linux system

table of Contents

  • 1) dd
  • 2) chopped
  • 3) Wipe
  • 4) scrubbing

1) dd

The dd command can help you copy and convert files. This command covers the entire disk with zeros and is much faster than generating gigabytes of random data. It provides operands to specify the required format. The syntax is:

dd if= of= [Options]

Only superusers can run this command because improper use can cause data loss.

# dd if=/dev/zero of=/dev/sda2 bs=512 count=1
1+0 records in
1+0 records out
512 bytes copied, 0.0116875 s, 43.8 kB/s

You can erase the disk by writing new data on each bit. To make the recovery process more complicated, we will write random data across the drive.

# dd if=/dev/urandom of=/dev/sda2 bs=4096

2) chopped

The shred command is used to repeatedly overwrite a specified file and makes it difficult for software to recover data. The syntax is as follows

shred [option] 

The command also overwrites the data in the file or the entire device with random bits, making it almost impossible to recover. This command will permanently delete the data. This command has some options that you can use to erase the disk, such as:

  • -F Change permissions to allow writing when needed
  • -n (Number of iterations = N) override N times instead of default (3)
  • -s (Size = N) Specify the number of bytes to Chopped
  • -u Truncate and delete files after overwriting
  • -v Show detailed information about progress
  • -X Can’t round file size to next full block
  • -z Add final zero cover to hide shreds
  • -u Delete files after overwriting

So to erase the partition you can use

# shred -vfz -n 10 /dev/sda2
shred: /dev/sda2: pass 1/11 (random)...
shred: /dev/sda2: pass 2/11 (ffffff)...
shred: /dev/sda2: pass 3/11 (249249)...
shred: /dev/sda2: pass 4/11 (000000)...
shred: /dev/sda2: pass 5/11 (555555)...
shred: /dev/sda2: pass 6/11 (random)...
shred: /dev/sda2: pass 7/11 (6db6db)...
shred: /dev/sda2: pass 8/11 (aaaaaa)...
shred: /dev/sda2: pass 9/11 (db6db6)...
shred: /dev/sda2: pass 10/11 (random)...
shred: /dev/sda2: pass 11/11 (000000)...

Fragments can be used on random data as shown below

# shred -v --random-source=/dev/urandom -n10 /dev/sda2
shred: /dev/sda2: pass 1/10 (random)...
shred: /dev/sda2: pass 2/10 (db6db6)...
shred: /dev/sda2: pass 3/10 (aaaaaa)...
shred: /dev/sda2: pass 4/10 (ffffff)...
shred: /dev/sda2: pass 5/10 (6db6db)...
shred: /dev/sda2: pass 6/10 (random)...
shred: /dev/sda2: pass 7/10 (249249)...
shred: /dev/sda2: pass 8/10 (555555)...
shred: /dev/sda2: pass 9/10 (000000)...
shred: /dev/sda2: pass 10/10 (random)...

3) Wipe

The Erase command can be used to securely erase files from magnetic media. You can use the erase command to erase the data on the disk. The command of the erase command is as follows

wipe [options] 

This command does not exist by default, you need to install it.

Up Ubuntu 16.04 / 18.04

# apt install wipe
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following NEW packages will be installed:
 wipe
0 upgraded, 1 newly installed, 0 to remove and 322 not upgraded.
Need to get 41.8 kB of archives.

On Centos 7, You need to download rpmforge first

# wget http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el7/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm

Install it

# rpm -Uvh rpmforge-release*rpm
warning: rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 6b8d79e6: NOKEY
Preparing... ################################# [100%]
Updating / installing...
 1:rpmforge-release-0.5.3-1.el7.rf ################################# [100%]

Then, install the wipe command

# yum install wipe
Loaded plugins: fastestmirror
rpmforge | 1.9 kB 00:00:00 
rpmforge/primary_db | 125 kB 00:00:15

You can erase partitions as follows:

# wipe /dev/sda2
Okay to WIPE 1 special file ? (Yes/No) yes
Operation finished. 
1 file wiped and 0 special files ignored in 0 directories, 0 symlinks removed but not followed, 0 errors occured.

4) scrubbing

The scrub command can be used to overwrite data using patterns. This command is not available by default, you need to install it on a Linux system. The syntax is

scrub [option] 

To clean up your hard drive, you can do the following

# scrub /dev/sda5
scrub: using NNSA NAP-14.1-C patterns
scrub: please verify that device size below is correct!
scrub: scrubbing /dev/sda5 1071644672 bytes (~1022MB)
scrub: random  |................................................|
scrub: random  |................................................|
scrub: 0x00    |................................................|
scrub: verify  |................................................|

Scrub uses certain methods to operate on disk. You can use the dod method to erase the disk.

# scrub -p dod /dev/sda5 -f
scrub: using DoD 5220.22-M patterns
scrub: please verify that device size below is correct!
scrub: scrubbing /dev/sda5 1071644672 bytes (~1022MB)
scrub: random  |................................................|
scrub: 0x00    |................................................|
scrub: 0xff    |................................................|
scrub: verify  |................................................|

Wiping the drive is dangerous because you could lose your data permanently. You must ensure that you select the correct disk. Also, depending on your concerns about someone getting data, it can be difficult to ensure that some drives are truly unrecoverable.

Sidebar