In this video, we'll talk about another useful system administration command called dd. It is 1
a command line utility 2
which purpose is to convert and copy files. Compared to the cp command, which is used to copy regular 3
files and directories 4
the dd command can be used to read and write from special device files. 5
Remember that hard disks are represented in Linux as special device files. 6
For example, in my case, the partition where the root directory is mounted is called sda5 7
and is represented as a special file named sda5 in the /dev directory. So dd can work directly 8
with these device files and is used for tasks such as such as backing up the boot sector of a hard drive, cloning 9
a disk or a partition to another one or creating a bootable usb stick. 10
Let's get started with some practical examples. 11
Note that sometimes dd humorously called this "Disk Destroyer", due to its drive-erasing capabilities. 12
So be careful and make a backup of your data before continuing with the examples in this lecture. 13
They could destroy all your data. 14
The command-line syntax of dd is slightly different from other Linux commands as it uses the syntax 15
option equals value for its command line options. 16
In the first example, I'm going to back up or clone an entire partition to a file on another partition. 17
I'm going to insert and use a USB stick for these examples. 18
Remember that if you run Linux in virtual box, you need to install virtual box extension back on your 19
Guest OS, which is probably Windows to have support for USB devices. Then go to devices USB and 20
select the USB flash disk. 21
OK, and it was automatically mounted. All the following commands must be run as root, so I'm 22
temporarily becoming root running 23
sudo su 24
Let’s see the device file which represents the usb drive.I'm running fdisk -l and we see that 25
the file is /dev/sdc Note that it could overwrite on your system. 26
There is also another command named lsblk that lists block devices 27
I’m running the dd command: so dd if 28
= the name of the USB stick file 29
dev/sdc of=/home/student 30
let's say /backup-usb.img status=progress 31
I'm hitting enter. 32
if means input file and of means output file. You need to use the gnu dd command from coreutils version 33
8.24 or above to use the status=progress option. 34
What we notice is that even though the USB stick is empty, look, it's empty, 35
this is its contents, 36
the command is copying data and you wonder what's copying. The dd 37
command works with blocks and is in fact cloning the device by copying everything both empty and occupied 38
space. 39
So if you have a partition of 10 gigabytes and the 9 are free, the dd command will copy 10 gigabytes 40
to the destination. 41
This is one of the main differences from the cp command. 42
I'm interrupting the command by pressing control 43
C 44
And when you want to restore the disc using the image file, you just use the image file as the input 45
and the disk or 46
partition as the destination. 47
So dd 48
if=/home/student/backup-usb.img of= 49
/dev and I'll use the same USB disk. 50
So sdb status= progress. 51
OK, it's wrking. You can also add the sync option to copy everything using synchronized input-output. 52
And I'm writing conv =sync 53
it's using synchronized IO. 54
I'm interrupting it by pressing control +C. Let's see another example! This time I'm cloning a 55
partition to another one. With dd command and you can clone the entire hard disk or just a partition. 56
Of course, the destination partition should be at least the same size as the source partition 57
for this to work. I'm going to clone the partition where the root file system is mounted to the USB 58
stick. 59
So fdisk-l to see the name of the partition. 60
And it's sda5; this is the source partition and the /dev sdb will be the destination. 61
I'm running the dd command. 62
dd if=/dev/sda5 of=/dev/sdb and status equals progress 63
OK, it's working, but it takes some time, so I'm stopping it. 64
In the next example, I'm going to back up and then restore the master boot record of the disk; a master 65
boot record or MBR, it's a special type of boot sector 66
at the very beginning of the disk. The master boot record holds the information on how the logical 67
partition, containing file systems, are organized on that medium. 68
It also contains executable code, which is usually referred to as a boot loader. 69
So the MBR represents the first sector of the disk with the size of 512 bytes. 70
By default, the dd command reads, one block of input processes it and writes it to an output file. 71
You can specify the block size using the bs option. 72
So dd if=/dev/sda this is the name of the disk not the name 73
of a partition. 74
And of= for example / root ~/mbr.dat from date and bs=512 75
This is the block size and count=1 76
It will copy only one block; and the block was copied. 77
This file, in the root home directory, is a backup up of the master boot record. Now in case something 78
bad happens, then you can restore the MBR r from the file like this. 79
dd if=/root mbr.dat of and the name of the disk. 80
/dev/sda bs=512 and count=1 81
And I'm hitting enter. 82
I've just restored the master boot record from a file. 83
In the last example, I'm going to show you how to create a bootable USB stick using dd. I have already 84
downloaded this iso file. 85
It's Linux Mint 20. 86
The first step is to find out the name of the device file for the USB drive, and I'm running ls 87
blk 88
It's sdb as we already know from the previous example.To make the USB drive bootable, it should be formatted 89
with a file system. On most Linux distributions 90
the USB flash drives are automatically mounted when inserted; unmount the disk if it's mounted using 91
the umount command and run the command that will format the drive. 92
So umant/ media/ student and the name of the drive. 93
It will be unmounted. 94
And I'm formatting the disc, using a shell command: mkfs. and the file system. 95
Let's say vfat 96
And the path to the device file, so /dev /sdb 97
It's formating the drive. 98
It takes some seconds, maybe more than 10, but it will be done soon. 99
The next step is to run the dd command as follows. 100
dd if=and the path to the ISO file 101
so home /student/ downloads and the name of the iso file of the output file will be the name of the USB stick. 102
So dev /sdb bs=4 megabytes and status= progress. 103
And I'm hitting enter. 104
That's all! After a few minutes, you'll have a bootable Linux Mint on your USB stick.