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 dd command

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

Share

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

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, default to standard input.

Of=file # output file name, which defaults to standard output

Ibs=bytes # reads bytes bytes at a time (that is, a block size is bytes bytes).

Obs=bytes # writes bytes bytes at a time (that is, a block size is bytes bytes).

Bs=bytes # also sets the size of the read-write block to bytes, which can replace ibs and obs.

Cbs=bytes # converts bytes bytes at a time, that is, the conversion buffer size.

Skip=blocks # skips blocks blocks from the beginning of the input file before starting to copy.

Seek=blocks # skips blocks blocks from the beginning of the output file before starting to copy. (usually valid only if the output file is a disk or tape).

Count=blocks # copies only blocks blocks with a block size 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 # backup the local / dev/hdx disk to / dev/hdy

Dd if=/dev/hdx of=/path/to/image # back up / dev/hdx full data to the image file in the specified path

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

# backup / dev/hdx full data, compress it with gzip tool, and save it 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 files 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 loads 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.

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