As many of you know, the >>
operator appends the output of a command to whatever you put after it.
I know that it can be used to overwrite or add data onto files, but I also know that it can be used in a malicious way: If a drive or partition is used as the output, the command's output gets written directly to the drive, which can damage that drive or partition.
For example, from my understanding, appending >> /dev/sda
to a command would append the output of the command directly onto the existing data in /dev/sda
.
Out of curiosity, I set up a Ubuntu VM amd tried this for fun. This machine's virtual drive is /dev/vda
, has /dev/vda1
as its boot partition, has /dev/vda2
as its root partition, and does not have a separate partition for /home
.
Now here comes the fun part:
- When I ran
echo "Hello world" >> /dev/vda
(as root) in the VM and then restarted it, nothing seemed to have been damaged. It booted up just fine on restart and everything seemed to work afterwards. - However, when I ran
echo "Hello world" >> /dev/vda1
(also as root) in the same VM and then restarted it, it wouldn't boot.
So I already know why writing the output to vda1
was bad (it damaged the boot partition). My question is: Why does appending the output to /dev/vda
not seem to do anything, but appending the output to /dev/vda1
harm it? Is it because /dev/vda2
is located "after" /dev/vda1
and so passing vda
results in the command output being written to vda2
?
446
(bytes) for the first one. The "protective" MBR on a GPT disk will have that layout in the first sector, again so it looks like a full (and optionally bootable) disk to legacy BIOSes and formatting tools. – Peter Cordes Oct 27 '23 at 04:03