<Marc Qualie/>

Wiping Hard Drives on Ubuntu

Photo of Marc Qualie

I'm regularly buying new hard drives/swapping them between machines in my homelab. I run the same bunch of commands over and over so I figured I'd drop them here for easy access. This will delete all data on the entire disk so use with caution!

Identify The Disk

It's very important to identify the correct disk to wipe to avoid accidental data loss. Always backup your system before attempting any kind of destructive actions like this.

lsblk -f

This will show you all devices and their sizes so you can easily reference them. If you've added a new Sata SSD then it will likely be /dev/sda or /dev/sdb. The last letter will increase as you add more drives. NVMe drives will have the pattern /dev/nvme0n1 with numbers instead of letters.

We will use /dev/sdX for the examples but make sure you use the correct reference for your drive.

Prepare the disk

If the hard drive has been used before you want to ensure no data still exists on there. Even if it's a brand new hard drive it's a good idea to ensure the drive is fully clean before use.

SATA Spinning Disks

# fast and fine for new drives, writes 0s to every bit
sudo dd if=/dev/zero of=/dev/sdX bs=1M status=progress

# much much slower but more secure, writes random data to every bit
sudo dd if=/dev/urandom of=/dev/sdX bs=1M status=progress

SATA SSDs

The above commands will still work on SSD but due to the architecture it's super slow and cause a lot of unnecessary write wear on the drive that you can avoid.

Ensure your SSD supports secure erase with the following command:

sudo hdparm -I /dev/sdX

If the disk is marked as "frozen" then you can suspend the machine with systemctl suspend then wake it up for a quick refresh.

Set a temporary user/password for secure erase. It can be anything, just use the same values in the next step:

sudo hdparm --user-master u --security-set-pass p /dev/sdX

Now you're ready to fully erase the drive with the following command:

sudo hdparm --user-master u --security-erase p /dev/sdX

# or, if device supports it you can use secure erase enhanced
sudo hdparm --user-master u --security-erase-enhanced p /dev/sdX

NVMEs

NVMe drives also have their own special wipe command which is much simpler than the hdparm option above.

# fastest (crypto erase, if supported)
sudo nvme format /dev/nvme0n1 -s 2  

# fallback (full user-data erase)
sudo nvme format /dev/nvme0n1 -s 1

The -s 2 enables a fast and full cryptographic wipe if supported. If this does not work you can fall back to -s 1 which is secure erase similar to above. In the unlikely event neither of these are supported (really old drives) you can omit the -s option for a standard wipe.

Reinitialize the drive

Now that the drive is an empty shell we need to create a file system to use it with an OS. This example we will use ext4 with on Ubuntu 24.04.

Partition the disk

You can split the drive into multiple partitions but I like to dedicate the entire drive to a single task for simplicity.

sudo parted /dev/sdX

Once inside the parted prompt you can enter the following to quickly setup a full disk partition:

mklabel gpt
mkpart primary ext4 0% 100%
quit

Now we create an ext4 file system on the newly created /dev/sdX1 partition.

sudo mkfs.ext4 /dev/sdX1

Create Mount Directory

This is where the drive will be accessible on your file system:

sudo mkdir -p /mnt/disk1

Get UUID

Each disk has a unique ID that we can use to auto mount on system boot.

sudo blkid /dev/sdX1

Grab the UUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" string.

Update /etc/fstab

sudo nano /etc/fstab

Add the following line:

UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /mnt/disk1 ext4 defaults 0 2

Some versions of Linux require a daemon-reload

sudo systemctl daemon-reload

Mount!

Now it's time to mount the drive to the folder you created.

sudo mount -a

That's it! As long as there are no errors then the drive is working and will be auto mounted on every boot. You can verify this by creating a file then rebooting to check if the file is still there.

echo "hello world" | sudo tee /mnt/disk1/test.txt
cat /mnt/disk1/test.txt

Now you can take any disk, clean it, and use it for your next project with a new filesystem.

If you have any questions about this post, or anything else, you can get in touch on Bluesky or browse my code on Github.