How to check if the ISO was written to my USB stick without errors?

You can use cmp for checking if everything was copied fine:

$ cmp -n `stat -c '%s' debian-X-netinst.iso` debian-X-netinst.iso /dev/sdX

This solution does not explicitly compute the checksum of your /dev/sdX - but you don't need to do that because you have already done this for the source of the comparison (i.e. debian-X-netinst.iso).

Doing just a dd if=/dev/sdX | sha1sum may yield a mis-matching checksum just because you get trailing blocks (/dev/sdX is most likely larger than the iso-file).

Via cmp -n you make sure that no trailing bytes on your /dev/sdX are compared.

If you are paranoid about the quality of your USB mass storage device you call sync, eject it, re-insert it and then do the comparison - else all or some blocks may just come from the kernels VM (cache) - when in reality perhaps bits on the hardware are screwed up.


Just using dd and md5sum / sha1sum is enough, but as previously said, be carefull, your device is not of the same size than your file, so sums will differ.

Here how you still can do it

First you'll need to know the size of the file:

$ stat -c '%s' debian-live-8.2.0-amd64-lxde-desktop.iso
1003487232

Then, to be cool with your syscalls, you better get this as a multiple of a nice power of two like 4096, the multiplication of the two HAVE TO yield exactly the size of the file, in other ways, you'll check too few or too many bytes, yielding a wrong checksum.

$ bc
bc 1.06.95
scale = 9
1003487232 / 4096
244992.000000000

I'm happy, 4096 × 244992 = 1003487232 so 4096 is good for me, (and will for you, probably) so I can use a block size of 4096 (typical) and a bloc count of 244992.

Don't forget to write the file on the USB key...

$ dd if=debian-live-8.2.0-amd64-lxde-desktop.iso of=/dev/sd? && sync

And know, using the known block size and the block count, you can read the exact number of bytes from the key and check them:

$ dd if=/dev/sdb bs=4096 count=244992 | sha1sum
b0dbe4ca8f526d1e43555459c538607d4a987184

(Yes md5sum is way faster than sha1sum but that's clearly not your bottleneck here, the bottleneck is the USB thoughput, thanks for noticing).

Or, in short:

dd if=/dev/sdb bs=4096 count=$(($(stat -c '%s' the.iso) / 4096)) | sha1sum

Julien's answer does the job but there is a simpler and faster way to do this:

sudo head -c <image size> /dev/sdX | sha1sum