md5sum Command: Tutorial & Examples
Computing and comparing hash codes
md5sum command is a pre-installed utility in most Linux distributions that is used to compute
and check MD5 (Message-Digest algorithm 5) hashes. This command calculates an almost unique 128-bit signature for each
input of data. It is commonly used to verify the integrity of files. However, it is not considered to be secure anymore
for integrity checks, as it is vulnerable to hash collisions, where different data inputs can produce the same hash
output, making it feasible for an attacker to manipulate data without detection.
How Does md5sum Work?
md5sum command implements the MD5 cryptographic hash function. When
md5sum command is executed, it takes a file as input and produces a 32-character hexadecimal number. This number
is the file's MD5 hash or checksum. The primary purpose of this checksum is to compare it with other MD5 hashes. If the
hashes are the same, then the files are identical. However, if they are different, it means the files have differences,
whether they are minor or major.
Importance of md5sum
md5sum command is important for verifying file integrity. When you download a file, especially large ones, there's
a chance that they could become corrupted during the download process. By comparing the MD5 hash of the file you have
with the hash provided by the file's source, you can verify that your downloaded file has not been tampered with or
md5sum command is not recommended for security critical uses. It is vulnerable to hash collisions and
therefore not considered secure anymore in the cybersecurity world. Better hashing algorithms such
as SHA256 or SHA3 are recommended.
Using the md5sum Command
To use the
md5sum command, you simply need to type
md5sum followed by the file name. Here's an example:
This command will output the MD5 hash of
myfile.txt. The output will look something like this:
The string of numbers and letters before
myfile.txt is the file's MD5 hash.
Common Parameters of md5sum
md5sum command has several parameters that you can use to modify its behavior. Here are a few common ones:
--binary: Read in binary mode.
--check: Read MD5 sums from the FILEs and check them.
--text: Read in text mode (default).
--status: Exit with zero status when no check sums are missing.
Checking File Integrity with md5sum
To check the integrity of a file, you can use the
--check option. Here's an example:
echo '68b329da9893e34099c7d8ad5cb9c940 myfile.txt' | md5sum -c -
This command will output:
If the file's MD5 hash matches the one you provided. If they don't match, you'll see:
md5sum command is useful for basic file integrity checks, it's not considered secure for more critical uses
due to its vulnerability to hash collisions. For those cases, you should use more secure hashing algorithms such as
SHA256 or SHA3.