Quite often, system administrators need to copy various binary data. For example, sometimes you may need to make a backup hard drive, create an empty file filled with zeros to organize swap space or other virtual file system.

To solve all these problems, the dd linux utility is used, which simply copies data from one place to another at the binary level. It can copy a CD/DVD disc, a section on a disc, or even an entire HDD. In this article we will look at what the linux dd command is, its main options and parameters, and how to use it.

First you need to understand how the dd command works and what it does. In fact, this is an analogue of the utility only for block data. The utility simply transfers one block of data of the specified size from one place to another. Since everything, including devices, is considered a file in Linux, you can transfer devices to files and vice versa.

Using various utility options, you can influence the block size, and this, in turn, already affects the speed of the program. Next we will look at the main options of the utility and its capabilities.

dd command

The syntax of the utility is quite unusual, but at the same time very simple, once you remember and get used to it:

$dd if= copy_source of= destination options

Using the if parameter, you need to specify the source from which the blocks will be copied, this can be a device, for example, /dev/sda or a file - disk.img. Next, using the of parameter, you need to specify the destination device or file. Other parameters have the same syntax as if and of.

Now let's look at the additional options:

  • bs- indicates how many bytes to read and write at a time;
  • CBS- how many bytes need to be written at a time;
  • count- copy the specified number of blocks, the size of one block is indicated in the bs parameter;
  • conv- apply filters to the data stream;
  • ibs- read the specified number of bytes at a time;
  • obs- write the specified number of bytes at a time;
  • seek- skip the specified number of bytes at the beginning of the reading device;
  • skip- skip the specified number of bytes at the beginning of the output device;
  • status- indicates how detailed the conclusion should be;
  • iflag, oflag- allows you to set additional operation flags for the input and output device, the main ones: nocache, nofollow.

These were all the basic options you might need. Now let's move closer to practice and look at several examples of how to use the dd linux utility.

How to use dd?

Regular users use the dd command most often to create images DVDs or CD. For example, to save a disk image to a file, you can use the following command:

sudo dd if=/dev/sr0 of=~/CD.iso bs=2048 conv=noerror

The noerror filter allows you to disable response to errors. Next, you can create an image of the hard drive or partition on it and save this image to disk. Just be careful not to save to the same hard drive or partition, so as not to cause recursion:

dd if=/dev/sda of=~/disk.img

A file named disk1.img will be created in your home folder, which in the future can be deployed and restored to the damaged system. To write an image to a hard drive or partition, just swap the device addresses:

dd if=~/disk.img of=/dev/sda

A very important and useful option is bs. It allows you to greatly influence the speed of the utility. This parameter allows you to set the size of one block when transferring data. Here you need to specify a digital value with one of these format modifiers:

  • With- one character;
  • b- 512 bytes;
  • kB- 1000 bytes;
  • K- 1024 bytes;
  • M.B.- 1000 kilobytes;
  • M- 1024 kilobytes;
  • G.B.- 1000 megabytes;
  • G- 1024 megabytes.

The dd linux command uses just such a system, it is complex, but there is no escape from it. It will have to be understood and remembered. For example, 2b is 1 kilobyte, and 1k is also 1 kilobyte, 1M is 1 megabyte. By default, the utility uses a block size of 512 bytes. For example, to speed up disk copying, you can take blocks of 5 megabytes in size. To do this, use the following command:

dd if=/dev/sda of=~/disk.img bs=5M

The next parameter is count. Using it you can specify how many blocks need to be copied. For example, we can create a 512 megabyte file by filling it with zeros from /dev/zero or random numbers from /dev/random:

sudo dd if=/dev/zero of=file.img bs=1M count=512

Please note that this parameter does not indicate the size in megabytes, but only the number of blocks. Therefore, if you specify a block size of 1b, then you only need to take two blocks to create a 1KB file. This option can also be used to backup the MBR partition table. To do this, copy the first 512 bytes of the hard drive to a file:

sudo dd if=/dev/sda of=mbr.img bs=1b count=1

To restore, use the usual command to deploy the image to disk.

If the disk image is too large, you can redirect all output to the non-standard output stream of the gzip utility:

dd if =/dev/sda2 | bzip2 disk.img.bz2

You can also use the dd linux utility to copy files, although this is not its intended purpose:

dd if=/home/sergiy/test.txt of=/home/sergiy/test1.txt

As you know, the linux dd command writes data to disk directly in binary form, which means that zeros and ones are written. They override what was previously placed on the recording device. Therefore, to erase a disk, you can simply fill it with zeros from /dev/zero.

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

Using dd this way results in the entire disk being completely erased.

conclusions

In this article we looked at how to use dd linux, what this utility can be used for and how useful it can be. It's an almost indispensable tool. system administrator, because with it you can do backups the whole system. And now you know how. If you have any questions, ask in the comments!

Using the dd utility, we will create an image of a flash drive with archiving of free space. Backup image will come in handy if the original suddenly stops working with important information. For example, a flash drive with private keys electronic signatures management of the organization. So, we have a 4GB flash drive /dev/sdd, the information on which takes up about 90MB.
du - sh / run / media / aleksey / Transcend

89M /run/media/aleksey/Transcend

All commands are executed on behalf of the user root. Or in the corresponding distributions add before the commands sudo.
fdisk - l /dev/sdd

Disk /dev/sdd: 3.7 GiB, 3904897024 bytes, 7626752 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xc653eaa4 Device Boot Start End Sectors Size Id Type /dev/sdd1 2048 7628543 7626496 3.7G b W95 FAT32

Disk /dev/sdd: 3.7 GiB, 3904897024 bytes, 7626752 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disklabel type: dos

Disk identifier: 0xc653eaa4

Device Boot Start End Sectors Size Id Type

/dev/sdd1 2048 7628543 7626496 3.7G b W95 FAT32

By creating a simple image with the command
dd if = /dev/sdd of = sdd . iso bs = 4M conv = noerror,
we condemn ourselves to storing a 4GB file. What if the flash drive had a capacity of 64GB? And not alone? A regular archiver will help us solve this problem, let’s take a standard one gzip.
dd if=/dev/sdd bs=4M conv=noerror | gzip - c > sdd . iso. zip
where is the key -c allows you to work with standard output.
After completing the work, let's look at the resulting file. ls - al sdd*

The resulting file is approximately 25MB in size. Real savings on disk space even compared to a 4GB file!
To restore a flash drive from an image, use reverse order commands
gunzip - c sdd . iso. zip | dd of = /dev/sdd conv = noerror bs = 4M

You can also archive images hard drives, where the volumes are an order of magnitude larger.

By the way! To make the process clearer, due to dd not having its own progress bar, I suggest using a small utility progress- Coreutils Progress Viewer. Installing it on Fedora is not difficult.
dnf install progress
For other distributions, the required repository can be found at https://pkgs.org/download/progress.
By installing and running the utility with the command watch progress in the second terminal (in the first we have an archiver and dd running) on ​​behalf of the same user, we will see something like this.

man progress will show you various useful keys of this utility.

    Rufus- free software with open source code for formatting removable USB storage media and creating bootable disks with various operating systems. The program is easy to use, high speed work and supports a multilingual interface.

You can download the program on the developer's website. The page contains links to download the standard version Rufus, and portable Rufus portable, which do not differ in anything except the name of the executable file and the location where the settings are stored. The program settings include the language used and settings for checking for updates. The standard version stores these settings in the registry, while the portable version stores these settings in a file rufus.ini program directory. Rufus does not require installation on the system - just download the executable file and run it. The program interface is very simple:

Overall, the program Rufus is not something unique in the field of tools for creating bootable media and its main advantage is ease of use. In order to create a bootable flash drive with its help, it is enough to have the original image of the bootable system and be able to click on the “Start” button. All selected parameters and settings, by default, are already designed for using the program to work on a standard computer.

Easiest and most convenient to use Rufus to create a bootable flash drive (bootable USB drive) from installation ISO images Windows drives or Linux, as well as disks disaster recovery systems and diagnostic tools.

When creating a bootable Windows flash drive, you just need to select the device to be written to and the bootable ISO image file. The program will set other parameters itself.

If you do not have an ISO image file, you can create one from a physical CD (or from a set of distribution files) using CD/DVD burning programs such as the well-known Nero, Alcohol, or the free CDBurnerXP or ImgBurn.

The procedure for creating a bootable USB flash drive with Windows is as follows:

  • select the flash drive on which the image will be written. Unlike many similar programs, Rufus displays the volume label, drive letter and size, so if you have several removable drives on the system, it is easy to select the one to write to.

  • select the partition scheme and system interface type. Rufus allows you to create flash drives for booting in a regular BIOS interface and for booting in a UEFI environment, create boot records for MBR volumes and GPT volumes. The default mode is "MBR for computers with BIOS or UEFI" - the most common mode for bootable flash drives today.

  • select the file system that will be used on the bootable flash drive to be created. By default, bootable Windows flash drives use the file system FAT32, but if necessary, you can choose NTFS, if you need to use files larger than 4 GB.

  • set the cluster size. The cluster size is selected by the program based on the image data and file system type, but if necessary, it can be changed.

  • specify the volume label that will be specified for the created flash drive.

  • set formatting options. It is best to leave these parameters as default and just select the file ISO image. For images created by the program dd on Linux, you need to select the option DD image.

    After pressing the button Start the program will format the flash drive, set the sign of the active partition, write down the main boot entry and partition boot record, as well as data bootable media from ISO image. After completion of work Rufus You can boot using the resulting bootable flash drive.

    Using virtualization technology to test bootable flash drives. Links to download free and convenient programs to simplify the process of creating, debugging and testing created bootable media.

    dd is a simple utility that is included in most Unix-like operating systems - Linux, FreeBSD, Solaris, etc.
    Its purpose is to read data from one device or file and write to another.

    dd can be effectively used to create an image of a hard drive, without using commercial utilities like Acronis True Image or Norton Ghost.

    Let's assume we have two disks:

    • /dev/sda - the disk whose image needs to be created;
    • /dev/sdb - the disk on which the image will be written.

    If necessary, you need to substitute your own values.

    The first step is to boot from any available Live-CD disk that has the dd utility and enter command line as a superuser. Create a mount point for backup.

    mkdir /mnt/backup

    We mount the hard drive on which you want to save the image.

    Creating a hard drive image

    dd if=/dev/sda of=/mnt/backup/sda.img bs=8M conv=sync,noerror

    • if=/dev/sda - copy all hard disk sda;
    • of=/mnt/backup/sda.img - copy to /mnt/backup/sda.img;
    • bs=8M — set the size of the hard drive cache to speed up the copying procedure (otherwise the data will be reset in small portions of 512 bytes);
    • conv=sync,noerror - we indicate to dd the need for bit-for-bit copying and ignoring read errors.

    To reduce the size of a hard disk image, you can compress it with any archiver.

    dd if=/dev/sda bs=8M conv=sync,noerror | gzip -c > /mnt/backup/sda.img

    Recovering a hard drive image

    To restore a hard disk image, you must follow the reverse procedure to the procedure for creating this image.

    dd if=/mnt/backup/sda.img of=/dev/sda bs=8M conv=sync,noerror

    When using compression, you must unzip the image in parallel.

    gunzip -c /mnt/backup/sda.img | dd of=/dev/sda conv=sync,noerror bs=8M

    Migrating the system to another hard drive

    To migrate the entire system to another hard drive, you must set the location of the new drive as the destination.

    dd if=/dev/sda of=/dev/sdb bs=8M conv=sync,noerror

    Then, if necessary, install the boot from this tough disk. Provided that new hard the disk is larger than the old one, there will be an unallocated area on it. It should be marked up and formatted according to existing requirements.

    Copy statistics in dd

    The main disadvantage of dd is the lack of a visual representation of the statistics of the copying procedure. However, this disadvantage can be easily circumvented. All you need to do is connect to another terminal.

    Determine the process number under which dd is running.

    Send periodically this process command kill -USR1 process_number_dd .

    watch -n 5 kill -USR1 process_number_dd

    • watch -n 5 - execute the command every 5 seconds;
    • kill -USR1 process_number_dd — show copy statistics.

    Cons of using dd to create disk images

    Everything has pros and cons. dd is a free and very flexible tool, but it can only do full copy volumes Special programs They can only copy data that is stored on disk.

    Thus, the volume of a disk image created using dd will be equal to the volume of the disk itself - regardless of how much data is on the disk.

    The other day I decided to create an image of my workers, bootable flash drives with different operating systems. How to create these multi-boot flash drives, I already in one of his articles. An old and time-tested program will help us in creating images. dd. As far as I know, the dd utility should be installed on the system by default. To create an image of your flash drive, run the following command in the Terminal:

    sudo dd if=/dev/sdc of=image.iso

    /dev/sdc- this is your flash drive,

    image.iso is an image with a name image and expansion .iso, which will appear in your home folder.

    To find out how your flash drive is designated in the system, you can, for example, run the Disks utility or the GParted program and look in them, or if through the Terminal, the command will help:

    sudo fdisk -l

    If you want to see the process of creating a flash drive, then there are several ways. I won’t tell you the first way, because I didn’t like it, but the alternative solution to this problem looks much better. To do this, you will need to install an improved version of dd, which is called dcfldd.

    sudo apt-get install dcfldd

    The staff of the DoD Computer Forensics Laboratory (DCFL) made some changes to the dd command, thereby improving it and using it in their research work. As a result, the dcfldd command was born, which ensures that the copied data is hashed at certain intervals to authenticate it. Moreover, dcfldd is much faster than dd. Now, if you want to see the progress of copying or creating an image, you need to run the command:

    sudo dcfldd if=/dev/sdc of=image.iso

    Now that the image is ready, you can create a new one, bootable USB flash drive. Let's insert a new, empty one instead of a flash drive with systems. I think that it will be detected by the system in the same way as the first one - sdc, but it’s better to double-check. Now the command will be like this:

    sudo dd if=image.iso of=/dev/sdc

    Well, if you have an empty flash drive that is the same size as the media with the data you need, then you can simply copy the entire contents of the first flash drive directly to the second, bypassing the creation of an image on the hard drive. In this case the command will be as follows:

    sudo dd if=/dev/sdb of=/dev/sdc

    Using this scheme, you can copy and create images not only of flash drives, but also of entire hard drives, or their partitions, CD/DVD drives, etc. But more on that in the next article.

    The choice of flash drives is up to you. You can burn the boot image onto regular flash drives from well-known brands: Transcend, Kingston, Apacer, Silicon Power and other manufacturers, or you can be a little different and choose jewelry flash drives with rhinestones, diamonds and other decorations. Although this, of course, will be the choice of the fair half of humanity. Although the work, the recorded image, appearance The device has absolutely no effect.