dd

LINUXLPIC1-101

tl;dr

dd if=/dev/sda of=/dev/null # test read speed of a disk

dd if=/dev/sda of=backup_mbr.bin bs=512 count=1 # backup of your MBR

dd if=/dev/sda of=/dev/sdb # cloning a disk

dd if=/dev/zero of=/dev/sda # wiping a disk with zeros

dd if=/dev/urandom of=/dev/sda # wiping a disk with random noise

dd if=/dev/zero of=/swapfile bs=1M count=1024 # creates a swap file of 1GB

status=progress option: Shows a live progress bar (very helpful for large disks).

---------------------------------------------------------------------------

The dd command (often jokingly called "Disk Destroyer" because a small typo can wipe a drive) is a low-level utility used for copying and converting raw data. It works block-by-block, ignoring filesystems and focusing on the underlying bits.

1. The Command: dd if=/dev/sda of=/dev/null

This specific command is essentially a read-speed test for your hard drive.

  • if=/dev/sda: Input File. It reads the raw data from your first physical hard drive (sda).

  • of=/dev/null: Output File. It sends all that data to the "bit bucket"—a virtual device that discards everything sent to it.

The result: You aren't saving any data, but when the command finishes, dd will report how much data was processed and the average transfer speed. It tells you how fast your disk can read data.

2. Backing up the MBR (Master Boot Record)

The MBR sits in the first 512 bytes of a disk. If it gets corrupted, the system won't boot.

dd if=/dev/sda of=mbr_backup.bin bs=512 count=1

  • bs=512: Set the Block Size to 512 bytes.

  • count=1: Only copy one block.

3. Cloning a Whole Drive*

You can mirror an entire drive to another identical drive.

dd if=/dev/sda of=/dev/sdb

Warning: This will overwrite everything on /dev/sdb! The author is not responsible for any damages on the system.

4. Wiping a Disk (Security)

If you are throwing away a computer, you can fill the drive with zeros or random data so the old files can't be recovered.

dd if=/dev/zero of=/dev/sda # Fills with zeros

dd if=/dev/urandom of=/dev/sda # Fills with random noise

5. Creating a Swap File

If you run out of RAM and need more swap space quickly, you can use dd to create a large empty file.

dd if=/dev/zero of=/swapfile bs=1M count=1024 # Creates a 1GB file

Flags to Memorize

Flag Meaning

if= Input source (file or device).

of= Output destination.

bs= Block Size (e.g., 512, 1M, 4k). Crucial for speed performance.

count= Number of blocks to copy.

status=progress Shows a live progress bar (very helpful for large disks).

Notes:

* Cloning a whole drive: after cloning an operating system, you need to consider a few things to make it work so your settings don't collide on the network. Take a look at this checklist.

A practical example: wiping a USB drive

Important: determine what is the drive you want to wipe. dd has no undo options!

lsblk, df and mount commands can help you with this.

Then, unmount the partition:

umount /dev/sdXX

Wipe the USB drive:

sudo dd if=/dev/zero of=/dev/sdXX bs=4M status=progress

Can you use other values for bs?

Absolutely. Common choices are bs=1M, bs=4M, bs=8M. The sweet spot depends on the hardware, but 4M is a widely used practical default that works well for USB drives and disk imaging.

In my case I got a "No space left on device" message from the "dd" command. I tried Ctrl+c but it didn't work. Ctrl+z seemed to work but it took a minute. Then I had to kill the "dd" process with a "kill -9", since "kill" on itself didn't work.

ps aux | grep -w dd

Now back at the USB drive, create a new partition table with fdisk:

sudo fdisk /dev/sdX

Options: o (to create a MBR/FAT partition table), n (new), p (primary), 1 (partition number), Enter (default values for sector), Enter (default values for sector again), w (write an exit).

Verify the partition has been created:

lsblk

Format the partition:

sudo mkfs.vfat /dev/sdXX

Unplug the USB drive and plug it again so it is auto-mounted.

Contact

Email

hello@unixtips.eu

© 2025. All rights reserved.