The dd command in Linux allows you to do many powerful things – from creating bootable USB drives to testing disk performance. As a developer, knowing dd can help you be more efficient when working with images, backups, and storage benchmarking. In this comprehensive guide, I‘ll cover the key uses of dd and how to use it safely and effectively.

An Introduction to dd

dd stands for "data duplicator". It copies blocks of data from one place to another, doing byte-by-byte copies. What makes dd so useful is that its input and output can be files, disks, partitions – essentially anything that the OS sees as a block device.

Some key advantages of dd:

  • It‘s a versatile tool included in all Linux distros
  • Allows low-level access for copying data
  • Useful for writing ISO files to USB drives
  • Can test disk performance by timing copies

However, dd is also dangerous – it has the power to overwrite data and destroy drives if used carelessly. So let‘s look at how to use dd safely.

Using dd Safely

The basic syntax of dd is:

dd if=input_file of=output_file [options]

This copies input_file to output_file byte-by-byte. However, what if you swap input and output by accident? You could overwrite important data! So I recommend these safety tips:

Double Check Inputs and Outputs

Before hitting enter, verify if= and of= are what you intended. If you ever swap them accidentally, data loss could result.

Use oflag=noclobber

This option prevents accidental overwriting of output files. If the output file already exists, dd will fail rather than overwrite:

dd if=/dev/sdb of=image.iso oflag=noclobber

Test First with Truncated Copies

If you‘re copying from/to a disk or drive, use a smaller count first to test, e.g:

dd if=/dev/sdb of=image.iso count=1000

This makes sure data is being read/written properly before copying the whole disk.

Creating Bootable USB Drives

One of the most common uses of dd is to write ISO files to USB drives to make them bootable. This allows you to install operating systems from USB or run live environments.

For example, to create a Ubuntu 20.04 bootable USB from an ISO:

Step 1: Identify the USB Drive

Insert your drive and use lsblk to identify it – let‘s assume it‘s /dev/sdb:

$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   480G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
├─sda2   8:2    0    16M  0 part 
└─sda3   8:3    0 479.5G  0 part 
  ├─vg1-root 254:0    0 464.6G  0 lvm  
  └─vg1-swap 254:1    0  14.9G  0 lvm  [SWAP]
sdb      8:16   1  14.9G  0 disk

Step 2: Write the ISO

Use dd to write the ISO to the drive (‘/dev/sdb‘)*:

$ sudo dd if=ubuntu.iso of=/dev/sdb bs=4M status=progress conv=fdatasync

The options:

  • if=ubuntu.iso: Input ISO file
  • of=/dev/sdb: Output to USB drive (not a partition)
  • bs=4M: Block size of 4MB for faster writing
  • status=progress: Display progress bar
  • conv=fdatasync: Improved data integrity
dd: writing to ‘/dev/sdb‘: No space left on device
799+0 records in
798+0 records out
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 149.912 s, 29.0 MB/s

This will write the ISO contents directly onto the drive, overwriting any existing partitions.

*Make absolutely sure of the correct drive before using dd for this – you can end up overwriting your system drives if not careful!

Step 3: Reboot from the USB

Once written, reboot your system and boot from the USB drive to launch Ubuntu and test it out!

The same process applies for any Linux distro – just substitute the ISO file and output drive as needed.

Creating Disk Images and Partitions

dd can also be used to create images of entire disks or partitions. When combined with compression tools like gzip, this provides an easy way to backup disks and partitions.

For example, to create a compressed image of partition /dev/sda1:

$ dd if=/dev/sda1 bs=4M status=progress | gzip > sda1.img.gz

This pipes the output of dd straight into gzip for on-the-fly compression into sda1.img.gz.

To restore later, simply reverse the process:

$ gzip -dc sda1.img.gz | dd of=/dev/sda1

This decompresses the image file via gzip and pipes it into dd to write to the partition /dev/sda1.

You can also create disk images to deploy OS installs and disk cloning using this approach.

Some key advantages over graphical backup tools:

  • No need to prepare empty backup files
  • Compression can be done on-the-fly
  • Easy to automate with scripts

Overall, dd offers a simple and fast way to image partitions. But as always – double check your input and output devices!

Benchmarking Disk Performance

While tools like hdparm exist for disk benchmarking, dd provides a simple way to measure basic performance. By timing copies to and from disks, we can calculate the read and write speeds.

Let‘s look at some examples for an external USB SSD mounted at /mnt/ssd.

Write Speed

Use dd to write a large file to test writes:

$ sudo dd if=/dev/zero of=/mnt/ssd/tempfile bs=1G count=10 oflag=dsync
10+0 records in
10+0 records out
10737418240 bytes (11 GB, 10 GiB) copied, 5.93105 s, 1.8 GB/s

Key parameters:

  • if=/dev/zero: Input file filled with zeros
  • bs=1G: 1GB block size
  • count=10: Create a 10GB file
  • oflag=dsync: Force write synchronization for accuracy

We can see from the output that the total 10GB took 5.93s, giving a write speed of 1.8GB/s.

Read Speed

To test reads, copy the file back off the disk:

$ sudo dd if=/mnt/ssd/tempfile of=/dev/null bs=1G count=10
10+0 records in
10+0 records out
10737418240 bytes (11 GB, 10 GiB) copied, 2.19087 s, 4.9 GB/s

With similar parameters, we copy to /dev/null instead of an actual file to discard the data.

The time to read 10GB is much faster at 2.19s – giving us a read speed of 4.9GB/s.

This shows the SSD has faster reads than writes, which is typical for many types of storage media. The numbers match what we‘d expect for a high-speed SSD.

While not as in-depth as tools like fio, dd provides a simple way to get basic read/write benchmarks. And since it‘s included in all Linux distros, it can be useful when lacking other benchmarking tools.

Creating Swap Files

Linux allows the use of swap files to augment RAM when physical memory fills up. These reside on disk but allow the OS to temporarily shift less active memory pages for improved performance vs swapping.

dd can easily create swap files as it sets up the underlying file infrastructure needed to work with Linux‘s memory management.

For example to create a 4GB swap file:

$ sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
4096+0 records in
4096+0 records out
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 5.31004 s, 806 MB/s

This creates /swapfile filled with zeros for the swap header.

We then configure it as a swap file:

$ sudo mkswap /swapfile  
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)

Finally, we enable the swap file:

$ sudo swapon /swapfile
$ sudo swapon --show
NAME      TYPE  SIZE USED PRIO
/swapfile file   4G   0B   -2

Now Linux can utilize this file on disk as virtual memory. All bootstrapped easily using dd and some swap configuration!

Converting and Transforming Data

While simply copying data is its primary purpose, dd can also transform data in useful ways via conversion settings like:

  • conv=ucase – Convert to uppercase
  • conv=swab – Byte swap (change endianness)
  • conv=ascii – Convert EBCDIC to ASCII
  • conv=ebcdic – Convert ASCII to EBCDIC

For example, to copy a file and convert to all uppercase:

$ dd if=input.txt of=output.txt conv=ucase

This translates the file character set while copying the data. Useful for adapting data encodings.

Generating Files of Arbitrary Size

Need to generate some files to test your software or fill up a disk? dd offers an easy way to create files of any given size.

For example, to build a 4GB test file:

$ dd if=/dev/zero bs=1G count=4 of=test.img
4+0 records in
4+0 records out
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 5.49516 s, 782 MB/s

This creates test.img filled with 4GB of zeros from /dev/zero. Super handy for creating disposable test files!

Copying Disks and Cloning Systems

While tools like Clonezilla exist for disk cloning, dd can also copy full disks for system migration. For example, migrating a smaller system disk to a larger replacement.

To clone a system from one disk to another:

$ sudo dd if=/dev/sda of=/dev/sdb conv=sync,noerror bs=64K status=progress

This copies /dev/sda onto /dev/sdb while displaying progress. Useful options:

  • conv=noerror,sync – Don‘t abort on error; pad blocks
  • status=progress – Display progress bar
  • bs=64K – Uses 64KB block size for faster copies

With a large enough destination disk, this allows full system duplication. The resulting clone can then boot and operate the same as the original source.

Warning: Be EXTREMELY careful with the input (if=) and output (of=) devices when copying disks. You can easily overwrite your operating system or data drives if not properly specified!

While powerful, directly copying disks has limitations vs cloning tools:

  • The destination disk must be same size or larger
  • Doesn‘t resize existing partitions
  • Copies empty/unused space

So perform and validate carefully when cloning disks via raw dd operations.

Generating True Random Data

dd can leverage the kernel‘s random number generator to produce files filled with high-quality random bytes. This can be useful for encryption keys, security applications, or simulation data sets requiring unpredictable inputs.

To output 1GB of true randomness to a file:

$ dd if=/dev/urandom of=random.data bs=1M count=1024

This sources bytes from /dev/urandom which utilizes environmental noise from device drivers and other sources to generate entropy. The output file can now serve as a source of quality random numbers.

Conclusion

While a "simple" unix tool, dd enables incredibly versatile data copying and conversion operations. It empowers administrators and developers to:

  • Prototype and test storage systems
  • Backup disks and partitions
  • Benchmark disk performance
  • Adapt and transform data encodings
  • Build bootable USB drives
  • Copy virtual machine images
  • Generate random data sets
  • Migrate server systems to new disks

However, its low-level power also enables data loss if used without care. Always double check input and output specifications before executing copy operations.

Overall dd remains an indispensable tool for Linux power users to this day. No other tool provides such direct access for moving data around at the byte level. Used properly, it delivers a fast and lightweight option for everything from sysadmin tasks to developer prototyping.

Similar Posts