{"id":7908,"date":"2024-10-09T01:00:00","date_gmt":"2024-10-09T05:00:00","guid":{"rendered":"https:\/\/www.both.org\/?p=7908"},"modified":"2024-10-09T06:23:54","modified_gmt":"2024-10-09T10:23:54","slug":"the-dd-command-the-disk-destroyer","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=7908","title":{"rendered":"The dd command &#8212; the Disk Destroyer"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"7908\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>Yes &#8212; <strong><code>dd<\/code><\/strong> &#8212; the disk destroyer. The command that strikes fear into the hearts of SysAdmins everywhere. It did for me, too, until I discovered how much power is contained in those twin letters.<\/p>\n\n\n\n<p>Officially known as \u201cdisk dump,\u201d many sysadmins call it \u201cdisk destroyer\u201d for good reason. Many of us have inadvertently destroyed the contents of an entire hard drive or partition using the <code><strong>dd<\/strong><\/code> command. That is why we will hang out in the <strong>\/tmp\/test<\/strong> directory to perform some of the experiments in this article.<\/p>\n\n\n\n<p>Despite its reputation, <code><strong>dd<\/strong><\/code> can be quite useful in exploring various types of storage media, hard drives, and partitions. We can also use it as a tool to explore other aspects of Linux.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">About dd<\/h2>\n\n\n\n<p>The <code><strong>dd<\/strong><\/code> command is part of the <a href=\"https:\/\/www.both.org\/?p=4575\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=4575\" target=\"_blank\" rel=\"noreferrer noopener\">GNU Core Utilities<\/a>, so is part of every Linux distribution. The <strong><code>dd<\/code><\/strong> man page has a disastrously understated description of its function. &#8220;Copy a file, converting and formatting according to the operands.&#8221;<\/p>\n\n\n\n<p>A better description of this tool is quite elusive but I&#8217;ll try. &#8220;The dd command is a tool that can copy <a href=\"https:\/\/www.both.org\/?p=3101\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=3101\" target=\"_blank\" rel=\"noreferrer noopener\">data streams<\/a> from any device to any other device.&#8221; That data stream can be what we normally think of as a <a href=\"https:\/\/www.both.org\/?p=3105\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=3105\" target=\"_blank\" rel=\"noreferrer noopener\">file<\/a>, a data stream stored on a disk, with a beginning and an end, and an entry in a directory that points to the inode that contains the metadata for the file including the locations of its data on the disk.<\/p>\n\n\n\n<p>But in Linux, <a href=\"https:\/\/www.both.org\/?p=6843\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=6843\" target=\"_blank\" rel=\"noreferrer noopener\">everything is a file<\/a>. So that opens up some interesting and powerful, yet potentially dangerous possibilities. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The bootable USB thumb drive<\/h2>\n\n\n\n<p>On of the most common things I do when a new version of Fedora is released is to download the ISO image to my hard drive and then copy it to a couple USB flash drives. This creates what&#8217;s typically called a &#8220;Live USB.&#8221; Let&#8217;s look at how that works.<\/p>\n\n\n\n<p>Most Linux distributions provide a bootable <a href=\"https:\/\/en.wikipedia.org\/wiki\/Optical_disc_image\" data-type=\"link\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Optical_disc_image\" target=\"_blank\" rel=\"noreferrer noopener\">ISO image<\/a> of each release. An ISO image is a file that conforms to the ISO 9660 standard for an optical disk image. Intended for use with CDs and DVDs, they contain all the information necessary to create a bootable disk. Such an image can also be copied to a USB device.<\/p>\n\n\n\n<p>I use Fedora and the images I use are typically 1.5GB to a little over 2GB. It&#8217;s easy to copy these ISO images to a USB device but the standard <strong><code>cp<\/code><\/strong> (copy) command can&#8217;t do that. The <strong><code>cp<\/code><\/strong> command can only copy a file to a directory on a formatted partition on the device. The ISO image needs to be copied starting at the very first sector of this storage device in order to be <a href=\"https:\/\/www.both.org\/?p=4597\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=4597\" target=\"_blank\" rel=\"noreferrer noopener\">bootable<\/a>. The image is a linear copy of the boot record, GRUB2 and the entire boot loader as well as the kernel and the rest of the operating system. Only the <strong><code>dd<\/code><\/strong> command can perform this task.<\/p>\n\n\n\n<p>For this example, the ISO image is contained in the file, ~\/Fedora-Xfce-Live-x86_64-40-1.14.iso, and the device on which I want to install it is located at \/dev\/sdb. The command to install the ISO image on that device looks like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># <strong>dd if=~\/Fedora-Xfce-Live-x86_64-40-1.14.iso of=\/dev\/sdb<\/strong><\/code><\/pre>\n\n\n\n<p>Simple and easy. It typically takes about 8 minutes to copy the image on my primary workstation but that will vary depending on the speed of the USB device, and your computer.<\/p>\n\n\n\n<p>The thing I like about using <strong><code>dd<\/code><\/strong> for this, is that I don&#8217;t need any other special software. <\/p>\n\n\n\n<p>Linux also provides a tool to create an ISO image file from the files located in a directory. You&#8217;ll probably need to install the genisoimage package like I did with Fedora.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># <strong>dnf -y install genisoimage<\/strong><\/code><\/pre>\n\n\n\n<p>Creating an ISO image is outside the scope of this article, but I think it is important that you know that it&#8217;s there and where to start.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Exploring the hard drive<\/h2>\n\n\n\n<p>It is now time to do a little exploring. Let\u2019s start with something simple. Assuming that the bootable device on your Linux host is \/dev\/sda, let&#8217;s see what we can find.<\/p>\n\n\n\n<p>As root in the terminal session, I use the <strong><code>dd<\/code><\/strong> command to view the boot record of the hard drive, assuming it is assigned to the <strong>\/dev\/sda<\/strong> device. The <code>bs=<\/code> argument is not what you might think; it simply specifies the block size, and the <code>count=<\/code> argument specifies the number of blocks to dump to STDIO. The <code>if=<\/code> argument specifies the source of the data stream, in this case, the <strong>\/dev\/sda<\/strong> device. Notice that we are not looking at the first blocks of the partition, we are looking at the very first blocks of the storage device.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong># dd if=\/dev\/sda bs=2048 count=25 | less<\/strong><\/code><\/pre>\n\n\n\n<p>The first page of the data stream isn&#8217;t very interesting. I scrolled down a page or two, how far depends on the size of the terminal session, and I saw the more interesting stuff.  Figure 1, shows the beginning of the logical volume metastructure that defines the layout of the logical volumes configured on this device. Be sure to click on the image to enlarge it.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><a href=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/10\/dd-2024-10-07_08-01-15.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1375\" height=\"1341\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/10\/dd-2024-10-07_08-01-15.png\" alt=\"\" class=\"wp-image-7935\" style=\"width:689px;height:auto\"\/><\/a><figcaption class=\"wp-element-caption\">Figure 1: Viewing the LVM metadata on my primary workstation&#8217;s hard drive. Click to enlarge.<\/figcaption><\/figure>\n\n\n\n<p>Because this LVM (Logical Volume Management) data was only a short distance into the data stream, and the LVM 2 label starting at about Hex 0010000, this is a non-bootable LVM device. I didn&#8217;t create a partition on it before creating a physical volume (PV) of the entire device. <\/p>\n\n\n\n<p>The bootable device on my workstation is \/dev\/nvme0n1 so let&#8217;s take a look at that one.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># <strong>dd if=\/dev\/nvme0n1 bs=2048 count=10 | less<\/strong><\/code><\/pre>\n\n\n\n<p>The results of this command on my bootable device are significantly different. Figure 2 shows the first page of the data stream and you can see the string, &#8220;EFI PART,&#8221; indicating that this device boots from an EFI partition. <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><a href=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/10\/dd-2024-10-07_08-28-49.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1034\" height=\"949\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/10\/dd-2024-10-07_08-28-49.png\" alt=\"\" class=\"wp-image-7936\" style=\"width:682px;height:auto\"\/><\/a><figcaption class=\"wp-element-caption\">Figure 2: The data stream of my bootable device. It uses EFI boot. Click to enlarge.<\/figcaption><\/figure>\n\n\n\n<p>This device is a standard <a href=\"https:\/\/www.both.org\/?p=4597\" target=\"_blank\" rel=\"noreferrer noopener\">Linux GPT<\/a> partitioning scheme, with \/boot and \/boot\/efi partitions with EXT4 filesystems. The rest of the device is a partition with LVM, one logical volume formatted with an EXT4 filesystem. Your system could look like one of these examples, or it may be something entirely different, like a BTRFS partition. <\/p>\n\n\n\n<p>I&#8217;ve been looking at the device itself and not the partitions contained on the device. That&#8217;s easy to do with another simple command. Of course, you&#8217;d use the device ID for your own host.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># <strong>dd if=\/dev\/sda1 bs=2048 count=100 | less<\/strong><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Saving data<\/h2>\n\n\n\n<p>I&#8217;ve had instances where a storage device has failed. Sometimes one of mine, and sometimes one belonging to a friend. One of the best uses I&#8217;ve found for <strong><code>dd<\/code><\/strong> is that of salvaging as much data as possible from a defective drive. Of course the error must be on the storage medium rather than in a complete electronic or head-seek or read failure. <\/p>\n\n\n\n<p>I&#8217;ve managed to save most of the data on a bad storage device by using dd to copy the entire partition or device to another device. Sometimes I&#8217;ve simply copied the image of the defective data stream to an image file and use the Linux loopback devices to mount that file as if it were a physical device. Either way I can copy most of the data off the image off the image to another device. This works because <strong><code>dd<\/code><\/strong> ignores bad data. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Turning it around<\/h2>\n\n\n\n<p>Reading data from the disk can provide us with some important information and help us to understand the structure of the metadata on the storage device. With some patience we could also locate and view &#8212; or copy &#8212; files and the data they contain, although this would normally seem to be a hard way to do that. <\/p>\n\n\n\n<p>But we can also write data to storage devices which opens some interesting but potentially dangerous possibilities.<\/p>\n\n\n\n<p>One useful application of <strong><code>dd<\/code><\/strong> is to clone a storage device. That&#8217;s pretty much what we did to create the Live USB device that can be used to try out Linux and to install it. <\/p>\n\n\n\n<p>This use case is to copy the entire content of a storage device directly from one to another. I have a dual-slot SATA docking station that I can plug two SATA devices into. Yet another simple command can be used to clone one device to the other. Just ensure that the target drive is equal to or larger than the size of the source device. <\/p>\n\n\n\n<p>In this example, I assume that the source device is \/dev\/sdc and the target is \/dev\/sdd. That gives us this command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># <strong>dd if=\/dev\/sdc of=\/dev\/sdd<\/strong><\/code><\/pre>\n\n\n\n<p>This command will run until it&#8217;s read all the data from the source device or until the target device is full, whichever comes first. You can do the same with a partition.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Copy a file<\/h2>\n\n\n\n<p>Yes, the <strong><code>dd<\/code><\/strong> command can copy a single file but we need to be careful with the syntax. Unlike the <strong><code>cp<\/code><\/strong> command, <strong><code>dd<\/code><\/strong> won&#8217;t automatically copy the file into a directory if we specify only the directory name as the outfile argument. Fortunately, it tells us we&#8217;re making a mistake and that it can&#8217;t open the directory as its not a file. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <strong>dd if=testfile.txt of=Test\/<\/strong>\ndd: failed to open 'Test\/': Is a directory<\/code><\/pre>\n\n\n\n<p>The <strong><code>of=<\/code><\/strong> argument must contain the full name of the file as well as the directory. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <strong>dd if=testfile.txt of=Test\/Testfile.txt<\/strong>\n31+1 records in\n31+1 records out\n16200 bytes (16 kB, 16 KiB) copied, 0.000250787 s, 64.6 MB\/s<\/code><\/pre>\n\n\n\n<p>We can also copy a file to the same directory with a different filename. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <strong>dd if=testfile.txt of=Testfile2.txt<\/strong>\n31+1 records in\n31+1 records out\n16200 bytes (16 kB, 16 KiB) copied, 0.000153622 s, 105 MB\/s<\/code><\/pre>\n\n\n\n<p>I don&#8217;t think there are any advantages to using <strong><code>dd<\/code><\/strong> to copy a file rather than using the <strong><code>cp<\/code><\/strong> command unless you have a need for one of the conversions. Read the manual page for more information on those conversions.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conversions<\/h2>\n\n\n\n<p>The <strong><code>dd<\/code><\/strong> manual page says that it can make some conversions. Most of the ones it discusses are esoteric and most SysAdmins will never need them. But I did find a couple interesting ones that can convert IBM Mainframe EBCDIC to ASCII and back. Others can convert uppercase to lowercase and vice-versa. Another can swap every pair of input Bytes, thus <a href=\"https:\/\/en.wikipedia.org\/wiki\/Endianness\" data-type=\"link\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Endianness\" target=\"_blank\" rel=\"noreferrer noopener\">little-endian for big-endian<\/a> and back. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Destructive uses<\/h2>\n\n\n\n<p>You can also do some destructive things. Sometimes I do this on purpose. I just want to delete all the data from a device or a partition. The <strong><code>dd<\/code><\/strong> command makes that easy, especially when combined with device special files like \/dev\/zero, \/dev\/random and \/dev\/urandom. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parting thoughts<\/h2>\n\n\n\n<p>You can see that the <code>dd<\/code> command can be very useful for exploring the structures of various types of filesystems, locating data on a defective storage device, and much more. It also produces a stream of data on which we can pipe through standard Linux utilities in order to modify or view.<\/p>\n\n\n\n<p>The real point here is that <code><strong>dd<\/strong><\/code>, like so many Linux commands, produces a stream of data as its output. That data stream can be searched and manipulated in many ways using other tools. It can even be used for ghost-like backups or disk duplication. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yes &mdash; dd &mdash; the disk destroyer. The command that strikes fear into the hearts of SysAdmins everywhere.<\/p>\n","protected":false},"author":2,"featured_media":4837,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[100,90,5],"tags":[594],"class_list":["post-7908","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-line","category-in-depth","category-linux","tag-dd-command"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/7908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7908"}],"version-history":[{"count":21,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/7908\/revisions"}],"predecessor-version":[{"id":7959,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/7908\/revisions\/7959"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/4837"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}