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 of Linux system

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

Share

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

This article mainly introduces "the use of the dd command of the Linux system". In the daily operation, I believe that many people have doubts about the use of the dd command of the Linux system. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about the use of the dd command of the Linux system. Next, please follow the editor to study!

I. basic usage

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:

If= file name: enter the file name, which defaults to standard input. That is, specify the source file.

< if=input file >

Of= file name: output file name, default to standard output. That is, specify the destination file.

< of=output file >

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.

Cbs=bytes: convert bytes bytes at a time, that is, specify the conversion 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.

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.

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

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.

II. Application examples of dd

1. Back up the local / dev/hdb entire disk to / dev/hdd

The code is as follows:

Heng@me: dd if=/dev/hdb of=/dev/hdd

two。 Back up the whole / dev/hdb data to the image file of the specified path

The code is as follows:

Heng@me: dd if=/dev/hdb of=/root/image

3. Restore backup files to the specified disk

The code is as follows:

Heng@me: dd if=/root/image of=/dev/hdb

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

The code is as follows:

Heng@me: dd if=/dev/hdb | gzip > / root/image.gz

5. Restore the compressed backup file to the specified disk

The code is as follows:

Heng@me: gzip-dc / root/image.gz | dd of=/dev/hdb

6. Backup and restore MBR

Back up 512 bytes of MBR information at the beginning of the disk to the specified file:

The code is as follows:

Heng@me: dd if=/dev/hda of=/root/image count=1 bs=512

Count=1 refers to copying only one block; bs=512 refers to a block size of 512 bytes.

Restore:

The code is as follows:

Heng@me: dd if=/root/image of=/dev/had

Write the backup MBR information to the beginning of the disk

7. Backup floppy disk

The code is as follows:

Heng@me: dd if=/dev/fd0 of=disk.img count=1 bs=1440k (that is, the block size is 1.44m)

8. Copy the memory contents to the hard disk

The code is as follows:

Heng@me: dd if=/dev/mem of=/root/mem.bin bs=1024 (specify a block size of 1k)

9. Copy the contents of the CD to the specified folder and save it as a cd.iso file

The code is as follows:

Heng@me: dd if=/dev/cdrom (hdc) of=/root/cd.iso

10. Increase swap partition file size

Step 1: create a file with a size of 256m:

The code is as follows:

Heng@me: dd if=/dev/zero of=/swapfile bs=1024 count=262144

Step 2: turn this file into a swap file:

The code is as follows:

Heng@me: mkswap / swapfile

Step 3: enable the swap file:

The code is as follows:

Heng@me: swapon / swapfile

Step 4: edit the / etc/fstab file so that the swap file is loaded automatically each time you boot:

The code is as follows:

/ swapfile swap swap default 0 0

11. Destroy disk data

The code is as follows:

Heng@me: dd if=/dev/urandom of=/dev/hda1

Note: use random data to fill the hard disk, which can be used to destroy data in some necessary situations.

twelve。 Test the read and write speed of the hard disk

The code is as follows:

Heng@me: dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file

Heng@me: dd if=/root/1Gb.file bs=64k | dd of=/dev/null

Through the command execution time outputted by the above two commands, the read and write speed of the hard disk can be calculated.

13. Determine the optimal block size for the hard drive:

The code is as follows:

Heng@me: dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file

Heng@me: dd if=/dev/zero bs=2048 count=500000 of=/root/1Gb.file

Heng@me: dd if=/dev/zero bs=4096 count=250000 of=/root/1Gb.file

Heng@me: dd if=/dev/zero bs=8192 count=125000 of=/root/1Gb.file

By comparing the command execution time shown in the above command output, you can determine the best block size for the system.

14. Repair the hard drive:

The code is as follows:

Heng@me: dd if=/dev/sda of=/dev/sda or dd if=/dev/hda of=/dev/hda

When the hard drive is not in use for a long time (more than one year), magnetic flux point will be generated on the disk, and it will be difficult for the head to read these areas, which may lead to an Ibind O error. When this situation affects the first sector of the hard disk, it may cause the hard disk to be scrapped. The above command may bring these numbers back to life. And the process is safe and efficient.

15. Remote backup with netcat

The code is as follows:

Heng@me: dd if=/dev/hda bs=16065b | netcat

< targethost-IP >

1234

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

The code is as follows:

Heng@me: 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

The code is as follows:

Heng@me: netcat-l-p 1234 | bzip2 > partition.img

Heng@me: netcat-l-p 1234 | gzip > partition.img

The above two instructions are the changes of the destination host instructions. Bzip2 and gzip are used to compress the data, and the backup files are saved in the current directory.

16. Change the value of the first byte in a large video file to 0x41 (that is, the ASCII value of the capital A)

The code is as follows:

Echo A | dd of=bigfile seek=$i bs=1 count=1 conv=notrunc

3. The difference between / dev/null and / dev/zero

/ dev/null, nicknamed bottomless pit, you can output any data to it, it takes all, and it won't hold up!

/ dev/zero, is an input device that you can use to initialize files. The device provides an endless supply of zeros, and you can use any number you need-the device provides a lot more. It can be used to write the string 0 to a device or file.

/ dev/null-- it is an empty device, also known as a bit bucket. Any output written to it will be discarded. If you do not want the message to be displayed as standard output or written to a file, you can redirect the message to the bucket.

The code is as follows:

Heng@me:if=/dev/zero of=./test.txt bs=1k count=1

Heng@me:ls-l

Total 4

-rw-r--r-- 1 oracle dba 1024 Jul 15 16:56 test.txt

Heng@me:find /-name access_log 2 > / dev/null

1 use / dev/null

Think of / dev/null as a "black hole", which is equivalent to a write-only file, and everything written to it will be lost forever, while trying to read from it will read nothing. However, / dev/null is very useful for both the command line and scripts

Prohibit standard output

The code is as follows:

The contents of the heng@me:cat $filename > / dev/null # file are missing and are not output to standard output.

Standard error prohibited

The code is as follows:

Heng@me:rm $badname 2 > / dev/null # so the error message [standard error] is thrown into the Pacific Ocean

Prohibit the output of standard output and standard error

The code is as follows:

Heng@me:cat $filename 2 > / dev/null > / dev/null

If "$filename" does not exist, there will be no error message; if "$filename" exists, the contents of the file will not be printed to standard output. Therefore, the above code does not output any information at all. This is useful when you only want to test the exit code of a command without any output.

2 use / dev/zero

Like / dev/null, / dev/zero is a pseudo file, but it actually produces a continuous stream of null (binary zero stream, not ASCII). The output written to it is lost, and it is difficult to read a series of null from / dev/zero, although this can also be done through od or a hexadecimal editor.

The main use of / dev/zero is to create an empty file of a specified length for initialization, just like a temporary swap file.

Create an exchange temporary file with / dev/zero

The code is as follows:

#! / bin/bash

# create an exchange file.

The $UID for a ROOT_UID=0 # Root user is 0.

E_WRONG_USER=65 # is not root?

FILE=/swap

BLOCKSIZE=1024

MINBLOCKS=40

SUCCESS=0

This script must be run with root.

If ["$UID"-ne "$ROOT_UID"]

Then

Echo; echo "You must be root to run this script."; echo

Exit $E_WRONG_USER

Fi

Blocks=$ {1:-$MINBLOCKS} # if the command line does not specify

# + is set to the default of 40 yuan.

# the above sentence is equivalent to:

#

# if [- n "$1"]

# then

# blocks=$1

# else

# blocks=$MINBLOCKS

# fi

#

If ["$blocks"-lt $MINBLOCKS]

Then

Blocks=$MINBLOCKS # must be at least 40 blocks long.

Fi

Echo "Creating swap file of size $blocks blocks (KB)."

Dd if=/dev/zero of=$FILE bs=$BLOCKSIZE count=$blocks # writes zeros to the file.

Mkswap $FILE $blocks # creates this file as a swap file (or swap partition).

Swapon $FILE # activates the exchange file.

Echo "Swap file created and activated."

Exit $SUCCESS

Another application of / dev/zero is to populate a file of a specified size with zeros for a specific purpose, such as mounting a file system to a loopback device (loopback device) or "safely" deleting a file.

Example to create ramdisk

The code is as follows:

#! / bin/bash

# ramdisk.sh

# "ramdisk" is a segment of system RAM memory

# + it can be operated as a file system.

Its advantage is that the access speed is very fast (including reading and writing).

# disadvantages: volatile, data will be lost when the computer is restarted or shut down.

# + will reduce the RAM available to the system.

# 10 # so what is the purpose of ramdisk?

# Save a large dataset in ramdisk, such as a table or dictionary

# + this speeds up data queries, because searching in memory is much faster than searching on disk.

E_NON_ROOT_USER=70 # must be run with root.

ROOTUSER_NAME=root

MOUNTPT=/mnt/ramdisk

SIZE=2000 # 2K blocks (can be modified appropriately)

BLOCKSIZE=1024 # each has a size of 1K (1024 byte)

DEVICE=/dev/ram0 # first ram device

Username= `id-nu`

If ["$username"! = "$ROOTUSER_NAME"]

Then

Echo "Must be root to run" `basename $0` "."

Exit $E_NON_ROOT_USER

Fi

If [!-d "$MOUNTPT"] # Test whether the mount point already exists

Then # + if this script has been run several times, this directory will not be built again

Mkdir $MOUNTPT # + because it has already been established.

Fi

Dd if=/dev/zero of=$DEVICE count=$SIZE bs=$BLOCKSIZE

# populate the contents of the RAM device with zeros.

# Why do you need to do this?

Mke2fs $DEVICE # creates an ext2 file system on the RAM device.

Mount $DEVICE $MOUNTPT # Mount the device.

Chmod 777$ MOUNTPT # enables ordinary users to access this ramdisk.

However, it can only be loaded by root.

Echo "$MOUNTPT" now available for use. "

# now ramdisk can be used to access files even by ordinary users.

# Note that ramdisk is volatile, so the content in ramdisk disappears when the computer system is rebooted or shut down.

# copy all the files you want to save to a regular disk directory.

After restarting, run this script to create a ramdisk again.

# only reloading / mnt/ramdisk without other steps will not work correctly.

# if improved, the script can be placed in / etc/rc.d/rc.local

# + so that a ramdisk can be set up automatically when the system starts.

# this is very suitable for database servers with high speed requirements.

Exit 0

4. Check the copy progress of dd

The DD command in linux makes it easy to make binary copies, such as hard disk cloning.

The general usage is as follows:

The code is as follows:

Dd if=/src/device/name of=/dst/device/name

... And then wait for it to finish, and the process exits.

But when the things we need for copy are huge, the process is painful.

Many people check iostat to estimate how long it will take to complete, which is unintuitive and unreliable.

If we dd-- help

You will see that there is actually a way to check the progress of copy. The example in the help file is as follows:

The code is as follows:

$dd if=/dev/zero of=/dev/null& pidymagram! Run the dd test in the background and save the process number to pid

Actually, we can open a new terminal when we use it.

The code is as follows:

Ps-ef | grep dd

Find the PID of dd, then send USR1 (user-defined signal) to check the progress of copy

On some occasions, we may use the current terminal, so we can transfer the currently running dd to the background: ctrl+z, when the dd process is stop, use bg% 1 to keep it running in the background and number 1. If you want it to run in the foreground, just type fg% 1.

Then we type kill-USR1 PID, where PID is the process number seen earlier with ps.

At this point you can see the current copy information as well as the average copy speed

At this point, the study of "how to use the dd command of the Linux system" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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