Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the dd command correctly in Linux

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to correctly use the dd command in Linux, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Interpretation of dd command

Dd: copies a file with a block of the specified size and performs the specified conversion at the same time.

Note: if the specified number ends with the following characters, it will be multiplied by the corresponding number: bouncy 512

Parameter comments:

1. If= file name: enter the file name. Default is standard input. That is, specify the source file.

< if=input file >

2. Of= file name: output file name, default is 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: output bytes bytes at a time, specifying a block size of bytes bytes.

Bs=bytes: set the read / output block size to bytes bytes at the same time.

4. Cbs=bytes: convert bytes bytes at a time, that is, specify the conversion buffer size.

5. Skip=blocks: skip blocks blocks from the beginning of the input file and then start copying.

6. Seek=blocks: skip blocks blocks from the beginning of the output file and then start copying.

Note: it is usually only valid when the output file is a disk or tape, that is, when backing up to a disk or tape.

7. Count=blocks: only blocks blocks are copied, and the block size is equal to the number of bytes specified by ibs.

8. Conv=conversion: converts the file with the specified parameters.

Ascii: converting ebcdic to ascii

Ebcdic: converting ascii to ebcdic

Ibm: converting ascii to alternate ebcdic

Block: convert each line to a length of cbs, and fill in the gaps with blanks

Unblock: make the length of each line cbs, and fill the insufficient parts with blanks

Lcase: convert uppercase characters to lowercase characters

Ucase: convert lowercase characters to uppercase characters

Swab: swap each pair of bytes of input

Noerror: don't stop when something goes wrong

Notrunc: output files are not truncated

Sync: populate each input block into ibs bytes, and fill in the shortfalls with NUL characters.

Make perfect copies of drives and partitions

If you go deep enough, you can use dd to perform a variety of tasks, but its best feature is to allow you to play around with partitions. Of course, you can use tar or even scp to copy the entire file system by copying files from one computer and pasting them intact into the newly installed Linux on another. However, since those file system archives are not complete mirrors, they need to run the host operating system on both sides as the basis.

On the other hand, you can use dd to create a perfect byte-by-byte mirror of almost any digital content. But before you start copying partitions from one place to another, it's worth mentioning that there is some truth in the old saying that "dd stands for disk destroyer" among Unix administrators. Even if you type a single character wrong in the dd command, it will immediately and permanently erase the valuable data from the entire drive. Yes, it is important to make sure that the input is correct.

Remember: be sure to think carefully before pressing enter to call dd!

Basic operation of dd

We have given you the necessary warning, starting with a simple point of view. Suppose you want to create an exact mirror of the data on the entire disk specified as / dev/sda. You have inserted an empty drive (ideally the same capacity as the / dev/sda system). The syntax is simple: if = define the source drive, of = define the file or location where the data is saved:

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

The next example creates an .img archive of the / dev/sda drive and saves it to the home directory of the user account:

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

Those commands create a mirror 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 copied at a time (4096 bytes in this case). Adjusting the BS value may affect the overall speed of dd operations, but the ideal setting will depend on your hardware profile and other considerations.

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

The recovery is simple: actually just reverse the value of if and the value of of. In this article, if= corresponds to the image you want to restore, and of= corresponds to the target drive you want to write to the image:

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

You can also perform both create and copy operations in a single command. For example, this example will use SSH to create a compressed image of a remote drive and save the resulting 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 available. If it is the boot drive you created, plug it into your computer and see if it starts properly. If it is a normal data partition, mount it to ensure that the file exists and can be accessed properly.

Erase the disk with dd

Many years ago, a friend of mine was in charge of the security of overseas embassies for his government. He once told me that every embassy he supervised was equipped with a hammer issued by the government. Why? In case there is any danger at the embassy, you can use this hammer to smash all the hard drives.

Then why not delete the data? Are you kidding me? It is well known that deleting files that contain sensitive data from a storage device does not actually delete data. If there is enough time and strong motivation, almost any data can be recovered from almost any digital medium, except those that have been smashed to pieces.

However, you can use dd to make it extremely difficult for criminals to get access to your old data. This command will take some time to create millions of zeros in each 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 write to disk using random characters:

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

Monitor dd operation

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 (execute 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/sda14,14MB 0:00:05 [98kB/s] [] is how to correctly use the dd command in Linux. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report