DD Command Usage



* Example use of dd command to create an ISO disk image from a CD-ROM:
dd if=/dev/cdrom of=/home/sam/myCD.iso bs=2048 conv=sync

*Using dd to wipe an entire disk with random data:
dd if=/dev/urandom of=/dev/hda

*Using dd to clone a hard disk to another hard disk:
dd if=/dev/ad0 of=/dev/ad1 bs=1M conv=noerror

*Duplicate a disk partition as a disk image file on a remote machine over a secure ssh connection:
dd if=/dev/sdb2 | ssh user@host “dd of=/home/user/partition.image”

*Overwrite the first 512 bytes of a file with null bytes:
dd if=/dev/zero of=path/to/file bs=512 count=1 conv=notrunc

*To duplicate a disk partition as a disk image file on a different partition:
dd if=/dev/sdb2 of=/home/sam/partition.image bs=4096 conv=noerror

*Create a 1 GiB file containing only zeros (bs=blocksize, count=number of blocks):
dd if=/dev/zero of=file1G.tmp bs=1M count=1024

*To zero out a drive:
dd if=/dev/zero of=/dev/sda

*To make sure that the drive is really zeroed out:
dd if=/dev/sda | hexdump -C | head

*To duplicate the first 2 sectors of the floppy:
dd if=/dev/fd0 of=/home/sam/MBRboot.image bs=512 count=2

*To create an image of the entire master boot record (including the partition table):
dd if=/dev/sda of=/home/sam/MBR.image bs=512 count=1

*To create an image of only the boot code of the master boot record (without the partition table):
dd if=/dev/sda of=/home/sam/MBR_boot.image bs=446 count=1

*To make drive benchmark test and analyze read and write performance:
dd if=/dev/zero bs=1024 count=1000000 of=/home/sam/1Gb.file
dd if=/home/sam/1Gb.file bs=64k | dd of=/dev/null

*To make a file of 100 random bytes:
dd if=/dev/urandom of=/home/sam/myrandom bs=100 count=1

*To convert a file to uppercase:
dd if=filename of=filename conv=ucase

*To search the system memory:
dd if=/dev/mem | hexdump -C | grep ‘some-string-of-words-in-the-file-you-forgot-to-save-before-you-hit-the-close-button’

*Image a partition to another machine:
On source machine: dd if=/dev/hda bs=16065b | netcat 1234
On target machine: netcat -l -p 1234 | dd of=/dev/hdc bs=16065b

*Create a 1 GiB sparse file or resize an existing file to 1 GiB without overwriting:
dd if=/dev/zero of=mytestfile.out bs=1 count=0 seek=1G

*To copy MBR 
MBRTotal Size
446 + 64 + 2 = 512
*Where
446 bytes – Bootstrap.
64 bytes – Partition table.
2 bytes – Signature(magic no)
*Type dd command as follows:
dd if=/dev/sda of=/dev/sdb bs=512 count=1

*dd command for two discs with different size partitions
# dd if=/dev/sda of=/tmp/mbrsda.bak bs=512 count=1

*Now to restore the image to any sdb:
# dd if=/tmp/mbrsda.bak of=/dev/sdb bs=446 count=1

*Linux sfdisk Command Example
Linux sfdisk command can make a backup of the primary and extended partition table as follows.
It creates a file that can be read in a text editor, or this file can be used by sfdisk to restore the primary/extended partition table.
To back up the partition table /dev/sda, enter:
# sfdisk -d /dev/sda > /tmp/sda.bak

*To restore, enter:
# sfdisk /dev/sda /tmp/backup-sda.sfdisk

*Task: Restore MBR and Extended Partitions Schema
To restore the MBR and the extended partitions copy backup files from backup media and enter:
# dd if=backup-sda.mbr of=/dev/sda
# sfdisk /dev/sda < backup-sda.sfdisk

*Example 1. Backup Entire Harddisk
To backup an entire copy of a hard disk to another hard disk connected to the same system, execute the dd command as shown below.
In this dd command example, the UNIX device name of the source hard disk is /dev/hda, and device name of the target hard disk is /dev/hdb.
# dd if=/dev/sda of=/dev/sdb
“if” represents inputfile, and “of” represents output file. So the exact copy of /dev/sda will be available in /dev/sdb.
If there are any errors, the above command will fail. If you give the parameter “conv=noerror” then it will continue to copy if there are read errors.
Input file and output file should be mentioned very carefully, if you mention source device in the target and vice versa, you might loss all your data.
In the copy of hard drive to hard drive using dd command given below, sync option allows you to copy everything using synchronized I/O.
# dd if=/dev/sda of=/dev/sdb conv=noerror,sync

*Example 2. Create an Image of a Hard Disk
Instead of taking a backup of the hard disk, you can create an image file of the hard disk and save it in other storage devices.
There are many advantages to backing up your data to a disk image, one being the ease of use.
This method is typically faster than other types of backups, enabling you to quickly restore data following an unexpected catastrophe.
# dd if=/dev/hda of=~/hdadisk.img
The above creates the image of a harddisk /dev/hda. Refer our earlier article How to view initrd.image for more details.

*Example 3. Restore using Hard Disk Image
To restore a hard disk with the image file of an another hard disk, use the following dd command example.
# dd if=hdadisk.img of=/dev/hdb
The image file hdadisk.img file, is the image of a /dev/hda, so the above command will restore the image of /dev/hda to /dev/hdb.

*Example 4. Creating a Floppy Image
Using dd command, you can create a copy of the floppy image very quickly. In input file, give the floppy device location, and in the output file, give the name of your floppy image file as shown below.
# dd if=/dev/fd0 of=myfloppy.img
Example 5. Backup a Partition
You can use the device name of a partition in the input file, and in the output either you can specify your target path or image file as shown in the dd command example below.
# dd if=/dev/hda1 of=~/partition1.img

*Example 6. CDROM Backup
dd command allows you to create an iso file from a source file. So we can insert the CD and enter dd command to create an iso file of a CD content.
# dd if=/dev/cdrom of=tgsservice.iso bs=2048
dd command reads one block of input and process it and writes it into an output file. You can specify the block size for input and output file. In the above dd command example, the parameter “bs” specifies the block size for the both the input and output file. So dd uses 2048bytes as a block size in the above command.
Note: If CD is auto mounted, before creating an iso image using dd command, its always good if you unmount the CD device to avoid any unnecessary access to the CD ROM.

Comments

Popular posts from this blog

grep: unknown device method

How to find outgoing IP in Linux ?

Uploading files to FTP/SFTP using CURL