@ZEAL Blog·厉
WWW Zeal Blog
We stand alone,
TOGETHER.

dd command of Linux

Posted by zeal on 2005-09-16 18:07 , 6987 characters , Via |  + 0 - 0   English
转载请保留本行原始出处声明信息 : http://www.zeali.net/entry/175 MaDe1nZEAL
标签 ( 开发/理论 ):  ,  , 
Few linux newbies know much about the standard GNU untilities found in /bin. One of the most useful, and least understood of these commands is dd. Even people who have been using linux for years mostly don't know about the dd command. dd is found on every linux installation, and has been a standard since the 1970's in UNIX. There is a man page for dd, which gives a brief description, but no useful examples. Here are some things you need to know about dd.

The basic command is structured as follows:
dd if=<source> of=<target> bs=<byte size>(some power of 2, not less than 512 bytes(ie, 512, 1024, 2048, 4096, 8192, 16384) conv=<conversion>.
Source is the data being read. Target is where the data gets written. If you mess up, and accidently reverse the source and target, you can wipe out a lot of data.

Examples::

Copy one hard disk partition to another hard disk:

dd if=/dev/sda2 of=/dev/sdb2 bs=4096 conv=notrunc,noerror

sda2, sdb2 are partitions. You want to copy sda2 to sdb2. If sdb2 doesn't exist, dd will start at the beginning of the disk, and create it. Be careful with order of if and of. You can write a blank disk to a good disk if you get confused.

Make an iso image of a CD:

dd if=/dev/hdc of=/home/sam/mycd.iso bs=2048 conv=notrunc

CD sectors are 2048 bytes, so this copies sector for sector. The result will be a hard disk image file of the CD. You can use "chmod a+rwx mycd.iso" to make the image writable. You can mount the image with "mkdir /mnt/mycd", this line in fstab: "/home/sam/mycd.iso /mnt/mycd iso9660 rw,user,noauto 0 0", save fstab, "mount -o loop /mnt/mycd". Then the file system will be viewable as files and directories in the directory /mnt/mycd. You can edit the image as you wish, and the new file will be "/home/sam/mycd.iso" dd does not write to CD's. You need to use a burning utility, or the cdrdao command

Copy a floppy disk:

dd if=/dev/fd0 of=/home/sam/floppy.image bs=2x80x18b conv=notrunc

The 18b specifies 18 sectors of 512 bytes, the 2x multiplies the sector size by the number of heads, and the 80x is for the cylinders--a total of 1474560 bytes. This issues a single 1474560-byte read request to /dev/fd0 and a single 1474560 write request to /tmp/floppy.image. This makes a hard drive image of the floppy, with bootable info intact.

Copy a hard drive image of a floppy to a floppy:

dd if=/home/sam/floppy.image of=fd0 bs=2x80x18b conv=notrunc

Copy just the MBR and boot sector of a floppy to hard drive image:

dd if=/dev/fd0 of=/home/sam/MBRboot.image bs=512 count=2

This copies the first 2 sectors of the floppy

Cloning an entire hard disk:

dd if=/dev/sda of=/dev/sdb conv=notrunc,noerror

in this example, sda is the source. sdb is the target. Do not reverse the intended source and target. Surprisingly many people do. notrunc means to not truncate. noerror means to keep going if there is an error. Normally dd stops at any error. if you have a question about a hard drive on whether or not it works, you can try to use it as the source drive for the dd command. You should get an error if it is not working. target drives need to be really messed up to give an error in dd.

Copy MBR only of a hard drive:

dd if=/dev/sda of=/home/sam/MBR.image bs=446 count=1

this will copy the first 446 bytes of the hard drive to a file. If you haven't already guessed, reversing the objects of if and of, in the dd command line reverses the direction of the write.

Wipe a hard drive of all data (you would want to boot from a cd to do this)
http://www.efense.com/helix is a good boot cd

the helix boot environment contains the DoD version of dd called dcfldd. It works the same way, but is has a progress bar.

dd if=/dev/zero of=/dev/sda conv=notrunc

This is useful for getting rid of viruses, DRM trojans and the like.

It would be useful, at this time to interject a tip:

I have several machines, but on the one I use a lot I have two SATA hard drives. They are exactly the same. Before I do anything dangerous, I boot from the helix CD, run

dcfldd if=/dev/sda of=/dev/sdb bs=4096 conv=notrunc,noerror

and copy my present working sda drive system to the sdb drive. If I wreck the installation on sda, I just boot with the helix cd and

dcfldd if=/dev/sdb of=/dev/sda bs=4096 conv=notrunc,noerror

Please note: bs=4096 works fast for machines with at least 128 MB of ram. dd uses a lot of buffers. At bs=4096, on modern machines, the optimal transfer rate is reached for hard drives.

To make a file of 100 random bytes

dd if=/dev/urandom of=/home/sam/myrandom bs=1 count=100

Here, urandom is the linux random byte device. myrandom is a file. Byte size equals 1 and there are 100 of them. Gpg requires a random seed to generate keys. Generating a file of say 4096 random bytes, which can be passed to Gpg, will allow a truly random seed.

Write random data over a file before deleting it:
first do an ls -l to find filesize. In this case it is 3769

ls -l afile
-rw------- ... 3769 Nov 2 13:41 <filename>
dd if=/dev/urandom of=<filename> \ bs=3769 count=1 conv=notrunc

This will write random characters over the entire file.

Copy a disk partition to a file on a different partition. Do not copy a partition to the same partition.

dd if=/dev/sdb2 of=/home/sam/partition.image bs=4096 conv=notrunc,noerror

This will make a file that is an exact duplicate of the sdb2 partition. You can substitue hdb, sda, hda, or whatever the disk is called.

Restore a disk partition from an image file.

dd if=/home/sam/partition.image of=/dev/sdb2 bs=4096 conv=notrunc,noerror

This way you can get a bazonga hard drive and partition it so you can back up your root partition. If you mess up your root partition, you just boot from the helix cd and restore the image.

To covert a file to all uppercase:

dd if=filename of=filename conv=ucase

Copy ram memory to a file:

dd if=/dev/mem of=/home/sam/mem.bin bs=1024

The device /dev/mem is your system memory. You can actually copy any blobk or character device to a file with dd. Memory capture on a fast system, with bs=1024 takes about 60 seconds. Copying a 120 GB HDD takes about an hour. Copying a CD to hard drive takes about 10 minutes. Copying a floppy to a hard drive takes about 2 minutes. With dd, your floppy drive images will not change at all. If you have a bootable DOS diskette, and you save it to your HDD as an image file, when you restore that image to another floppy it will be bootable. dd is an excellent way to make an image of MS Windows XP install CD's. When you make a copy of such a cd, there is one sector that is of nonstandard length. It is the last sector. dd doesn't pad this sector, making the copy indistinguishable from the original. If you burn the CD using cdrdao, the resulting disk will be an absolutely exact copy of the original.

Last Modified on 2007-05-25 22:45
没有评论.
日志存档
2014年
2013年
2012年
2011年
2010年
2009年
2008年
2007年
2006年
本 Blog 原创内容
遵循以下授权:

Creative Commons
(创作共用) CC

署名-非商业性
-相同方式分享
ZEAL Blog 基于
Powered byPivot - 1.24.1: 'Arcee'
Pivot1.24.1
开发
系统部署于
Linode.com
Launched @
2005-04-27
Since 2005.04.27  梦想 就像鸡蛋 要么孵化 要么臭掉RSS Feed (Entries) | About me | Back To Home | @ZEAL | zbird.com | 沪ICP备05024379号