Would
dd if=/dev/zero of=somepartition bs=512
also wipe partitions after somepartition
or stop at the end of somepartition
?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communitydd
dd
is a very powerful but also dangerous tool. It does what you tell it to do without questions. So if you tell it to wipe the family pictures, ... and it is a minor typing error away.
But if you check and double-check, you can use it.
dd if=/dev/zero of=somepartition bs=512
or I would suggest
dd if=/dev/zero of=/dev/sdxn bs=4096
where x
is the drive letter and n
is the partition number and block size 4096 bytes makes the write process faster.
It is important that you write to a partition in this case. If you write to the whole drive (the drive head end) /dev/sdx
the whole drive will be overwritten. But writing to the partition will be interrupted at the end of the partition and partitions behind it will be preserved. (I tested now on a USB pendrive in Lubuntu 16.04 LTS, so I know that it works like that.)
There is an exception for an extended partition (which is a container for logical partitions, in order to have more than four partitions in an MSDOS partition table). This is described in the following link,
Can I make image of 'extended' partition using dd?
But there is another problem too. I tested your command in a test environment, and dd read only one kibibyte (1024 bytes) when I wanted it to make an image of an extended partition.
I tested also this now on a USB pendrive in Lubuntu 16.04 LTS, and this applies to writing (as well as to reading). Only the first kibibyte is overwritten.
So to summarize, overwriting primary partitions and logical partitions work according to the main description in this answer. But do not use this method to overwrite an extended partition because only the first kibibyte will be overwritten. The extended partition's logical partitions will no longer be found via the partition table, but the data stored in them are still there.
dd
and that it is risky, so that you are very careful when using it. Good luck :-) In general, you should always have a backup of all files, that you cannot afford to lose.
I think your question is based on a fundamental misunderstanding about how dd
(and in fact Unix-like operating systems in general) work:
dd
cannot overwrite adjacent partitions, simply because dd
cannot overwrite partitions, period.
dd
simply writes to files. That's it.
Now, if you pass dd
a file which represents multiple partitions, then dd
will overwrite that file. But in that case, it's not dd
writing past the end of the partition. dd
will still write until the end of the file, and only until the end of the file.
But, if you pass dd
a file which only represents one partition, then dd
will not write past the end of this partition. Again, this has nothing to do with dd
. dd
simply writes to the file you tell it to write to. The fact that this file represents a single partition is (in this case) ensured by the block device driver in the kernel. dd
has nothing to do with that.
So, in short: dd
writes to files. What those files represent, is none of dd
's concern. dd
knows nothing about partitions.
dd
for writing to raw partitions. You can only write to files. You can of course write to a block device file which represents a partition (such as /dev/sda1
), but you cannot write to a raw partition. And since you can only write to a file that represents a partition, you cannot write past the end of the partition, because the file only represents the partition, and not the partition and a bit past the end.
Oct 26, 2017 at 23:33
/dev/sda
in your text editor and change the data, dd
is not different from any other program in terms of being able to access partitions. Very good answer!
Writes to a partition device won't write outside of that partition, with dd or anything else. You would need to use a wholedisk device to have any effect outside of a single partition.
(Caveat: unless your disk has a partition table with overlapping partitions, which should never happen.)
There is a dangerous, but rare special scenario in which this could happen even with non-buggy block device drivers:
You have to be careful with dd as if you make a mistake you can overwrite more than you bargained for and it depends on what you are using dd for (the OP was vague in his or her use of dd and the exact syntax of the dd command).
If you specify a partition it will write to that partition until the partition is full.
If you make a mistake and put the entire drive, for example of=/dev/sda , dd will write to the entire drive start to finish ignoring (and overwriting) your partition table.
You can also us dd on a partition to overwrite deleted files (deleted files may remain on the partition and discovered by various recovery tools with various degrees of success until they are over written. In this case you can use dd to fill the free space by writing to a file.
dd if=/dev/zero of=/path/to/mount_point/zero_file bs=4096
rm -f /path/to/mount_point/zero_file
Depending on what you are doing, however, you may wish to use an alternate tool to securely delete files. See How to delete file(s) in secure manner? for options.
dd
is not that low level. In fact most of the things people do using dd
could have been done just the same with cat
. In fact cat
is slightly faster because it uses larger blocks than dd
does by default. All of the low level code is in the kernel. And the driver does not really care if you are using dd
or cat
.
dd if=/dev/zero of=/dev/sda bs=512 count=1
or dd if=/dev/zero of=/dev/sda bs=512
are examples. With the second command it will keep on writing and fill the entire disk "ignoring" the partition table. If you specify a partition dd if=/dev/zero of=/dev/sda1
it will not go beyond the partition. Sort of depends on what you want to do with dd.
of=somepartition
, so the case where you write to the whole disk device doesn't apply. dd
can't get around the restrictions of the device driver.
/dev/sda1
, are created by the kernel in such a way that it is impossible to access adjacent storage through them (assuming the partition table wasn't altered after the kernel read it the last time). If you could access adjacent storage in that way, it would be considered a huge bug.