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

Detailed explanation of how to use dd under linux

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

Share

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

This article introduces the relevant knowledge of "detailed explanation of the use of dd under linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Brief introduction to Command

The main options for dd:

If the place of the specified number ends with the following characters multiplied by the corresponding number:

Baked 512, cased 1, kappa 1024, walled 2, xm=number m

If=file

Enter the file name, which defaults to standard input.

Of=file

Output file name, which defaults to standard output.

Ibs=bytes

Read bytes bytes at a time (that is, a block size is bytes bytes).

Obs=bytes

Write bytes bytes at a time (that is, a block size is bytes bytes).

Bs=bytes

At the same time, the size of the read and write block is set to bytes, which can replace ibs and obs.

Cbs=bytes

Convert bytes bytes at a time, that is, convert the buffer size.

Skip=blocks

Skip blocks blocks from the beginning of the input file before you start copying.

Seek=blocks

Skip blocks blocks from the beginning of the output file before you start copying. (usually valid only if the output file is a disk or tape).

Count=blocks

Only blocks blocks are copied, and the block size is equal to the number of bytes specified by ibs.

Conv=conversion [, conversion...]

Converts the file with the specified parameters.

Conversion parameters:

Ascii converts EBCDIC to ASCII.

Ebcdic converts ASCII to EBCDIC.

Ibm converts ASCII to alternate EBCDIC.

Block converts each line to a record of length cbs, with gaps filled in.

Unblock causes the length of each line to be cbs, with blanks to fill in the gaps.

Lcase converts uppercase characters to lowercase characters.

Ucase converts lowercase characters to uppercase characters.

Swab swaps each pair of bytes entered.

Noerror does not stop when there is an error.

Notrunc does not truncate the output file.

Sync fills each input block into ibs bytes, and the deficiency is filled with NUL characters.

two。 Case analysis

2.1. Data backup and recovery

2.1.1 full disk data backup and recovery

Backup:

Dd if=/dev/hdx of=/dev/hdy

Back up the local / dev/hdx entire disk to / dev/hdy

Dd if=/dev/hdx of=/path/to/image

Back up the whole / dev/hdx data to the image file of the specified path

Dd if=/dev/hdx | gzip > / path/to/image.gz

Backup / dev/hdx full data, and use gzip tool to compress and save to the specified path

Restore:

Dd if=/path/to/image of=/dev/hdx

Restore backup files to the specified disk

Gzip-dc / path/to/image.gz | dd of=/dev/hdx

Restore the compressed backup file to the specified disk

2.1.2. Remote backup with netcat

Dd if=/dev/hda bs=16065b | netcat

< targethost-IP >

1234

Execute this command on the source host to back up / dev/hda

Netcat-l-p 1234 | dd of=/dev/hdc bs=16065b

Execute this command on the destination host to receive data and write to / dev/hdc

Netcat-l-p 1234 | bzip2 > partition.img

Netcat-l-p 1234 | gzip > partition.img

The above two instructions are the changes of the destination host instructions respectively use bzip2 gzip to compress the data and save the backup files in the current directory.

2.1.3. Backup MBR

Backup:

Dd if=/dev/hdx of=/path/to/image count=1 bs=512

Backup the MBR information of the 512Byte size at the beginning of the disk to the specified file

Restore:

Dd if=/path/to/image of=/dev/hdx

Write the backup MBR information to the beginning of the disk

2.1.4. Backup floppy disk

Dd if=/dev/fd0 of=disk.img count=1 bs=1440k

Back up the floppy drive data to the disk.img file in the current directory

2.1.5. Copy memory data to hard disk

Dd if=/dev/mem of=/root/mem.bin bs=1024

Copy the data in memory to the mem.bin file in the root directory

2.1.6. Copy an iso image from a CD

Dd if=/dev/cdrom of=/root/cd.iso

Copy the CD data to the root folder and save it as a cd.iso file

2.2. Increase Swap partition file size

Dd if=/dev/zero of=/swapfile bs=1024 count=262144

Create a file large enough (256m in this case)

Mkswap / swapfile

Turn this file into a swap file

Swapon / swapfile

Enable this swap file

/ swapfile swap swap defaults 0 0

Automatically load the swap file every time you boot, you need to add a line to the / etc/fstab file

2.3. Destroy disk data

Dd if=/dev/urandom of=/dev/hda1

The hard disk is filled with random data, which can be used to destroy data in some necessary situations. "after this operation, / dev/hda1 cannot be mounted, and the create and copy operations cannot be performed."

2.4 disk Management

2.4.1. Get the most appropriate block size

Dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file

Dd if=/dev/zero bs=2048 count=500000 of=/root/1Gb.file

Dd if=/dev/zero bs=4096 count=250000 of=/root/1Gb.file

Dd if=/dev/zero bs=8192 count=125000 of=/root/1Gb.file

By comparing the command execution time shown in the dd instruction output, the optimal block size size of the system can be determined.

2.4.2 Test the read and write speed of the hard disk

Dd if=/root/1Gb.file bs=64k | dd of=/dev/null

Dd if=/dev/zero of=/root/1Gb.file bs=1024 count=1000000

The read / write speed of the test hard disk can be calculated from the execution time of the output of the last two commands.

2.4.3. Repair hard disk

Dd if=/dev/sda of=/dev/sda

When the hard disk is not in use for a long time (for example, 1 or 2 years), magnetic flux point will be generated on the disk. It is difficult for the head to read these areas and may result in an Icano error. When this situation affects the first sector of the hard disk, it may cause the hard disk to be scrapped. The above command has the potential to bring the data back to life. And the process is safe and efficient.

This is the end of the introduction of "detailed explanation of the use of dd under linux". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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