The dd command is used in Linux without destroying the disk

  • 2020-12-05 17:32:26
  • OfStack

Whether you're trying to salvage data from a memory drive that's about to fail, backing up an archive to remote storage, or making a perfect copy of an active partition somewhere else, know how to copy the drive and file system safely and reliably. Fortunately, dd is a simple and powerful mirror replication tool, and it has a long history. There is no better tool for this.

Explanation of the dd command

dd: Copies 1 file with a block of the specified size and performs the specified conversion while copying.

Note: If the specified number ends with the following characters, multiply it by the corresponding number: b=512; c = 1; k = 1024; w=2

Parameter notes:

1. if= file name: Enter the file name, default to standard input. That is, specify the source file. < if=input file >

2. of= file name: output file name, default to standard output. That is, specify the destination file. < of=output file >

3. ibs=bytes: Read bytes bytes at a time, that is, specify a block size of bytes bytes.

obs=bytes: Outputs bytes bytes at a time, specifying a block size of bytes bytes.

bs=bytes: Also sets the read/output block size to bytes bytes.

4. cbs=bytes: convert bytes bytes once, that is, specify the size of the conversion buffer.

5. skip=blocks: Skip blocks blocks from the beginning of the input file before copying.

6. seek=blocks: Skip blocks blocks from the beginning of the output file before copying.

Note: this is usually only valid if the output file is a disk or tape, that is, if it is backed up to disk or tape.

7. count=blocks: copies only blocks blocks, with block size equal to the number of bytes specified by ibs.

8. conv=conversion: Converts the file with the specified parameter.

ascii: Convert ebcdic to ascii

ebcdic: Convert ascii to ebcdic

ibm: Convert ascii to alternate ebcdic

block: Converts each line to a length of cbs, filling the gaps with Spaces

unblock: Make each line length cbs, filling in the gaps with Spaces

lcase: Converts uppercase characters to lowercase characters

ucase: Converts lowercase characters to uppercase characters

swab: Exchange each pair of bytes of input

noerror: Do not stop when an error occurs

notrunc: Untruncated output file

sync: Each input block is filled to ibs bytes, and the remainder is filled with empty (NUL) characters.

Make perfect copies of drives and partitions

You can use dd for a variety of tasks if you dig deep enough, but its best feature is that it lets you play around with partitions. Of course, you can use tar or even scp to copy an entire file system by copying files from one computer and pasting them intact onto a newly installed Linux on another computer. However, because those file system archives are not complete images, they need to run the host operating system on both ends as a basis.

On the other hand, dd makes it possible to create a perfect image of almost any digital content that corresponds to a byte-by-byte. But before you start copying partitions from one place to another, it's worth mentioning that the old adage among Unix administrators that "dd stands for disk breaker" has a point. Mistyping even a single character in the dd command immediately and permanently clears the entire drive of valuable data. Yes, it's important to make sure you have the correct input.

Remember: Think carefully before hitting enter to call dd!

Basic operation of dd

We've given you the necessary warning, starting with the easy stuff. Suppose you want to create an exact image of the data for the entire disk specified as /dev/sda. You have inserted an empty drive (ideally as large as /dev/sda system 1). The syntax is simple: if = define the source drive, of = define the file or location where the data will be held:

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

The next example will create a.img archive of the /dev/sda drive and save it to the home directory of the user account:

# dd if=/dev/sda of=/home/username/sdadisk.img

Those commands create a mirror image of the entire drive. You can also focus on a single partition in the drive. The next example does this and also uses bs to set the number of bytes per copy (4096 bytes in this case). Adjusting the bs value may affect the overall speed of the dd operation, but the ideal setting will depend on your hardware configuration file and other considerations.

# dd if=/dev/sda2 of=/home/username/partition2.img bs=4096

It's easy to recover: you actually just reverse the value of if and of. In this article, if= corresponds to the mirror you want to restore and of= corresponds to the target drive you want to write the mirror to:

# dd if=sdadisk.img of=/dev/sdb

You can also perform both create and copy operations in one command. For example, this example would use SSH to create a compressed image of a remote drive and save the generated archive to the local computer:

# ssh username@54.98.132.10 "dd if=/dev/sda | gzip -1 -" | dd of=backup.gz

You should always test the archives to make sure they are working properly. If it is the boot drive you created, insert it into the computer and see if it starts properly. If it is a normal data partition, mount it to ensure that the file exists and is normally accessible.

Erase the disk with dd

Many years ago, a friend of mine was responsible for the security of his government's embassies abroad. He once told me that every embassy he oversees has a government-issued hammer. Why is that? In any danger to the 11000 embassy, we can use this hammer to smash all the hard drives.

So why not delete the data? Are you kidding? It is well known that deleting files containing sensitive data from storage devices does not actually remove data. Given enough time and motivation, almost any data can be retrieved from almost any digital medium, except those that have been smashed to pieces.

However, you can use dd to make it extremely difficult for unscrupulous criminals to get hold of your old data. This command will take 1 minute to create millions of zeros on every corner of the /dev/sda1 partition:

# dd if=/dev/zero of=/dev/sda1

But it can get better. Using the /dev/urandom file as the source, you can use random characters to write to disk:

# dd if=/dev/urandom of=/dev/sda1

Monitor dd operations

Since disk or partition archiving can take a long time, you may need to add a progress monitoring tool to the command. Install Pipe Viewer (run sudo apt install pv on Ubuntu) and insert it into dd. With pv, the last command looks like this:


# dd if=/dev/urandom | pv | dd of=/dev/sda1
4,14MB 0:00:05 [ 98kB/s] [   <=>         ]

Tired of backup and disk management? With dd, you don't have many excuses. It's not that hard to use, but be careful. Good luck!

conclusion


Related articles: