A checksum value is a short data block derived from a larger one. MD5 (Message Digest 5) checksum values are used to verify the integrity of files in Linux.
MD5 checksums are 128-bit strings derived by running the MD5 algorithm on a file. Besides the checksum, the MD5 algorithm is also known for its hash function. The function generates a 128-bit message or “hash value.”
The value is always the same for a file, regardless of which machine generates it or how many times it’s generated.
Two distinct files rarely have the same hash value, so the MD5 checksum is used to check the integrity of a file.
It helps when you’ve downloaded a file from the internet and want to ensure it’s a perfect copy and data wasn’t lost or damaged during the download.
When damaged files are installed, they can corrupt your machine. Therefore, it is considered best practice to generate a software or operating system ISO file’s MD5 hash and compare it with the original hash, which is typically found on the site of the tool/OS.
Linux distros typically feature the GNU Core Utilities package, so there’s a good chance the md5sum program is already installed on your Linux machine.
Here’s how you can use it to check the MD5 hash values of a file:
md5sum [OPTION] [FILE PATH] |
Let’s say you want the hash value of a “test.cpp” file. You would run:
md5sum /home/TestLinux/test/test.cpp |
The output would look something like this:
5f6681de7bb943a5eeeb1cf81f6bbef9 /home/TestLinux/test/test.cpp |
The command can be paired with the following flags:
Option | What It Does |
-b | Read in binary mode |
-c | Generate MD5 for files and compare them |
–tag | Generate a BSD-style checksum |
Some flags you might find helpful when verifying a file’s checksum include:
Option | What It Does |
–ignore-missing | Stops printing report status for missing files |
–quiet | Stops printing OK when files are verified |
–status | Stops supplying output, status code shows success |
–strict | Exits non-zero for improperly formatted checksum files |
-w | Warns about improperly formatted checksum files |
Using the md5sum Command
To generate an MD5 checksum and store it in a file, you can run:
# md5sum /home/TestLinux/test/test.cpp > exampleMD5.md5 |
This command stores the generated MD5 in the “exampleMD5.md5” file:
To verify the contents of the MD5 stored in this file, you can run:
# md5sum -c exampleMD5.md5 |
Its output should look like this:
/home/TextLinux/test/test.cpp: OK |
Try changing the contents of the exampleMD5 file. When you verify the file again, you will see:
/home/TestLinux/test/test.cpp: FAILED
md5sum: WARNING: 1 computed checksum did NOT match |
Now, let’s look at how the –quiet option works. If you use the option when verifying the checksum, the command won’t print “OK” if the MD5 value is correct.
So, let’s revert to the original exampleMD5 file – the one that has the correct MD5 checksum – and run the command:
# md5sum -c –quiet exampleMD5.md5 |
You won’t see any output, which means this file has the correct MD5.
But if it doesn’t, you will see a warning like this:
# md5sum -c –quiet exampleMD5.md5
/home/TestLinux/test/test.cpp: FAILED md5sum: WARNING: 1 computed checksum did NOT match |