Highlights: Sometimes there is a necessity to eliminate a disk that is malfunctioning or needs to be cleared for security reasons. But how do you achieve this without the fancy tools available online? Well, for Mac or Linux users, the process is quite simple using utilities like dd and shred (not very straightforward on Windows).
Countless times I have encountered situations where I had to wipe a troublesome disk and then reformat it. In a household with Windows, Linux, and Mac systems, there is always a computer complaining about a disk not being properly formatted.
I have faced scenarios where drives were so corrupted that even my preferred tools, such as gParted (along with Disk Utility before it lost most of its features and kept only the basic ones), would consistently throw errors when attempting to format or set up a new partition table. It is quite frustrating when reliable tools refuse to function properly. So, what can you do to securely remove a disk in Linux and Mac?
Explore Partition Tables Here
The ideal approach is to erase the drive. While many tools can securely erase drives, they might be slow. If that doesn’t suit your requirements, what steps should be taken when your regular disk tools are behaving erratically? To erase disks efficiently and swiftly, it is essential to revert to the fundamental methods.
NOTE: For SSDs, extra caution is needed during the erasing process. Due to the compression techniques employed in SSDs, simply zeroing-out a drive will not truly wipe it clean. Refer to the SSD manual for secure wiping instructions; the manufacturer often provides a dedicated tool or specific guidelines.
Removing Disk in Linux and OS X using the dd command
The primary tool, and arguably one of the pivotal utilities for disk erasure in Linux and Mac, is dd, (although Windows versions exist, the usage varies slightly). The dd command for wiping drives is akin to a data scalpel. It allows you to precisely extract data of desired sizes from specific locations and relocate it as needed, offering a plethora of options. Despite its archaic syntax, it is straightforward.
dd if=/dev/zero of=/dev/sda bs=1M count=1
In this command, we incorporate four flags:
- if – Denotes the input file, where we utilize /dev/zero to generate zero or null bytes.
- of – Refers to the output file, which is our disk.
- bs – Dictates the block size (usually not crucial in most cases).
- count – Specifies the number of blocks copied.
The above command will write one megabyte of zeros to the disk, effectively erasing the partition table and enabling a partitioning program to create a new table and partitions.
If you wish to securely erase an entire drive, it can still be achieved using dd. Simply transition your input from /dev/zero to /dev/random or /dev/urandom, and conduct as many passes as deemed sufficiently secure. A for loop can streamline this drive-wiping process, avoiding the need for manual intervention and repetitions, thus enhancing efficiency.
for i in $( seq 0 2 ); do dd if=/dev/urandom of=/dev/sda; done
We have omitted the bs and count options in this scenario since they are dispensable when writing across an entire disk. dd will cease once it reaches the end of the file, which, in this context, denotes the conclusion of the disk. Alternatively, achieving this with cat is also feasible.
cat /dev/urandom > /dev/sda
Additionally, you may leverage dcfldd, which encompasses all functionalities of dd and more, notably presenting the amount of data copied.
PRO TIP: Aside from utilizing dd for disk erasure in Linux and Mac, it can be employed to inscribe an ISO onto a drive by specifying the ISO as the input file, facilitating the creation of a bootable USB disk effortlessly. Moreover, you can designate a disk as the input file and point to a nonexistent filename to generate an image of your disk.
Discover The Comprehensive Compilation of Linux Commands
Deleting Disk in Linux and Mac using shred
The subsequent option for drive erasure, compatible with both Linux and Mac but not universally pre-installed on Linux, is shred. It is remarkably user-friendly.
shred -n 3 /dev/sda
Executing this command to wipe a hard drive is essentially analogous to our dd command within a for-loop. shred utilizes data from /dev/urandom to overwrite the specified disk (or file). The -n parameter signifies the number of passes to execute. In my experience, shred tends to operate faster than directly reading from /dev/urandom using dd, however, it lacks the widespread usage that dd enjoys.
Various other tools are available for disk erasure in Linux and Mac, but they may not always be readily accessible, particularly on systems restricted to the command line. While Windows offers its command-line tools for disk management, caution is advised as they may not be as dependable as perceived. I have encountered instances where I had to construct a partition table multiple times in diskpart before Windows acknowledged it as a viable disk.
For a more secure disk erasure, follow your preferred method from the aforementioned solutions and subsequently dismantle the disk platters, destroying them using your preferred technique. This method ensures the utmost security, although it may be considered excessive except for scenarios involving nuclear warhead codes.
Share your preferred techniques for disk erasure in Linux and Mac by dropping a comment below.
Examine The List of 10 Riskiest Linux Commands