Utilities

How to Use dd Command on Linux

dd is a command line utility that is used to convert and copy files on Linux. dd has many usages in Linux. For example, you can use dd to make a bootable USB thumb drive of your favorite Linux distribution. This is a very common usage of the dd command. In this article, I will show you how you can use dd to do various convert and copy operations in Linux. So, let’s get started.

Making an ISO File of CD/DVDs:

If you want to make an ISO file of your CD or DVD disk. You can easily do that with the dd command.

Let’s say, you’ve inserted a DVD of a movie into the CD/DVD reader of your computer. Now, you want to create a ISO file from that DVD.

First, run the following command to find the device name of your CD/DVD reader.

$ sudo lsblk | grep rom

As you can see, the device name is sr0 in my case. So, I can access it as /dev/sr0

Now, you can make an ISO file of the CD/DVD disk with the following command:

$ dd if=/dev/sr0 of=path/filename.isobs=1M

Here, /path/filename.iso is the path and filename of your ISO file. In my case, I will save it to ~/Downloads/ubuntu.iso

As you can see, the ISO file is created. The disk write speed is about 29.4 MB/s and about 851 MB of data is written in total.

You should be able to find the ISO file in the directory where you saved it.

Making a Bootable USB Thumb Drive of Your Favorite Linux Distribution:

You can use dd to create a bootable USB thumb drive of your favorite Linux distribution.

To create a bootable USB thumb drive, you need a USB thumb drive of about 4GB or more in size and an ISO image of your preferred Linux distribution.

Let’s say, you want to make a bootable USB thumb drive of Ubuntu Server 18.04.1 LTS. You’ve downloaded the ISO file of Ubuntu Server 18.04.1 LTS and it’s in your ~/Downloads directory.

First, insert the USB thumb drive which you want to make bootable.

Now, run the following command to find the device name of your USB thumb drive.

$ sudo lsblk | grep disk

As you can see, my 32GB USB thumb drive is listed here. The device name is sdb. So, I can access it as /dev/sdb

Now, run the following command to make a bootable USB thumb drive from the ISO image of your preferred Linux distribution.

$ sudo dd if=~/Downloads/ubuntu-18.04.1-live-server-amd64.iso of=/dev/sdb bs=1M

Your USB thumb drive now can be used to install your desired Linux distribution.

Creating Virtual File Systems:

The dd command can be used to create file based virtual file systems. You can format, mount, store files etc from there.

Let’s say, you want to create a 512MB virtual file system.

To do that, run the following command:

$ dd if=/dev/zero of=disk1.raw bs=1M count=512

NOTE: bs=1M means the block size is 1 MB and count=512 means the disk1.raw file will contain 512 blocks. 512 * 1MB = 512 MB. You can also set bs=1G to change the block size to 1 GB.

The 512MB raw file disk1.raw should be created.

As you can see, a new file disk1.raw is in my current working directory.

Now, you can format the file disk1.raw file as any filesystem you want. I will format it as EXT4 filesystem.

To format the disk1.raw file as EXT4 filesystem, run the following command:

$ mkfs.ext4 -L datastore1 disk1.raw

NOTE: Here, datastore1 is the label of the virtual disk. You may change it if you want.

A virtual EXT4 filesystem should be created.

Now, make a new directory where you can mount the virtual filesystem with the following command:

$ mkdir datastore1

Now, mount the virtual filesystem to the newly created directory with the following command:

$ sudo mount disk1.raw datastore1/

As you can see, the virtual filesystem is mounted correctly.

This is great for testing.

Wiping The Whole Disk:

You can use the dd command to wipe the partition table off of your disk or USB thumb drives.

Let’s say, you want to wipe off the partition table off of the disk /dev/sdb. To do that, run the following command:

$ sudo dd if=/dev/zero of=/dev/sdb bs=1M count=1

The whole partition table of your disk should be removed. This is a very destructive operation. You will not be able to recover your partitions anymore. If you want to use this disk again, you will have to create a new partition table.

If you want to sell your hard drive or SSD to someone else, then it is always safe to completely wipe out all your personal data. Complete wipe out means replacing the contents of the whole disk with either zeros or random values. So, the new owner of the disk won’t be able to recover any of your personal data.

If you want to replace the contents of the whole disk with zeros, then you can use dd as follows:

$ sudo dd if=/dev/zero of=/dev/sdb

This will take a long time to complete.

If you want to replace the contents of the whole disk with random values, then you can use dd as follows:

$ sudo dd if=/dev/urandom of=/dev/sdb

This will take a long time to complete as well.

Where to Go Next?

The dd command has a lot of options that may come in handy. You may checkout the dd manpage for more information on all the supported command line options and usages of the dd command.

To access the manpage of dd, run the following command:

$ man dd

So, that’s how you use dd command on Linux to do various tasks. Thanks for reading this article.

About the author

Shahriar Shovon

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.