MD5 Checksum Linux: Generate & Verify Files
In this article
To generate and verify MD5 checksums on Linux, use the md5sum command, which is included in all mainstream distributions. Run md5sum filename to generate a checksum, and md5sum -c checksumfile to verify a file against a stored checksum.
What Is an MD5 Checksum?
An MD5 checksum is a 32-character hexadecimal string produced by the MD5 (Message-Digest Algorithm 5) hash function. It acts as a digital fingerprint of a file's contents: any change to the file, even a single bit, results in a completely different checksum. This makes it useful for verifying file integrity after downloads or transfers, and for detecting accidental corruption. However, MD5 is cryptographically broken—attackers can deliberately craft two different files with the same hash—so it should not be used for security purposes like password storage or verifying signed software.
Generating MD5 Checksums
Open a terminal and navigate to the directory containing the file, or provide the full path. The basic syntax is:
md5sum /path/to/fileFor example, to check a file named ubuntu.iso in the current directory:
md5sum ubuntu.isoOutput:
5d41402abc4b2a76b9719d911017c592 ubuntu.isoThe checksum is printed first, followed by two spaces and the filename. To generate checksums for multiple files at once, list them all:
md5sum file1.txt file2.txt file3.txtFor all files in a directory (not recursive by default), use a wildcard:
md5sum *To include subdirectories recursively, use the find command:
find . -type f -exec md5sum {} +Storing Checksums in a File
It is common practice to save checksums to a text file, typically named MD5SUMS or checksums.md5. Redirect the output to create such a file:
md5sum file1.txt file2.txt > MD5SUMSThe file will contain lines in the same format as the terminal output. This file can be distributed alongside the original files so recipients can verify integrity. To append checksums to an existing file, use >> instead.
Verifying MD5 Checksums
To verify files against a checksum file, use the -c (check) option:
md5sum -c MD5SUMSFor each file listed in MD5SUMS, md5sum will compute the current checksum, compare it to the stored value, and print either OK or FAILED. For example:
file1.txt: OK
file2.txt: FAILED
md5sum: WARNING: 1 computed checksum did NOT matchIf a file is missing, you'll see a message like md5sum: file3.txt: No such file or directory and a warning at the end. To verify a single file against a known checksum string, use the --check option with a here-string or process substitution:
echo '5d41402abc4b2a76b9719d911017c592 file.txt' | md5sum -c -Or, more simply, compute the current checksum and compare it manually with the expected value.
Common Problems and Misconceptions
Line endings and whitespace: The checksum depends on the exact bytes of the file. If a file is transferred between Windows and Linux and line endings are converted (CRLF vs LF), the checksum will change. Always transfer files in binary mode or use tools that preserve bytes exactly.
Filename with spaces: When using a checksum file, filenames with spaces are handled correctly by md5sum because it uses the first 32 characters as the hash, then two spaces, then the filename. However, if you manually copy-paste a checksum, ensure you include the correct spacing.
MD5 is not secure: Many people mistakenly believe MD5 provides security. It does not. It is only suitable for checking accidental corruption, not for verifying that a file hasn't been maliciously altered. For security-sensitive verification, use SHA-256 (sha256sum) or SHA-512 (sha512sum).
Large files: md5sum reads the entire file, which can take time for large files. There is no built-in progress bar, but you can use pv to show progress:
pv file.iso | md5sumThis pipes the file through pv (pipe viewer) to display progress, then into md5sum.
Alternatives to MD5
Linux provides several other checksum tools. The most common are:
sha1sum– produces a 40-character SHA-1 hash. SHA-1 is also considered weak for security, but still used for integrity.sha256sum– produces a 64-character SHA-256 hash. Recommended for security-sensitive applications.sha512sum– produces a 128-character SHA-512 hash, even stronger.b2sum– implements BLAKE2, a fast and secure hash function.
All these commands work exactly like md5sum (generate, store, verify). For example, to generate and verify SHA-256:
sha256sum file.iso > SHA256SUMS
sha256sum -c SHA256SUMSChoosing between them depends on your need: MD5 is fastest but insecure; SHA-256 is a good balance of speed and security; BLAKE2 is even faster and secure.
Frequently Asked Questions
How do I get the MD5 checksum of a file in Linux?
Run md5sum filename in the terminal. The command outputs the 32-character hash followed by the filename. You can also pipe a file's content directly: cat file | md5sum.
What does “md5sum -c” do?
The -c (or --check) option reads a file containing checksums and verifies that the listed files match those checksums. It prints OK for each match and FAILED for mismatches.
Why does my MD5 checksum change after downloading?
The file may have been corrupted during transfer, or the transfer tool might have altered line endings (especially if you're on Windows). Always download in binary mode and compare checksums from the official source.
Is MD5 checksum safe for verifying downloads?
For accidental corruption, yes. For security against tampering, no. An attacker can create a malicious file with the same MD5 hash. Use SHA-256 or SHA-512 for security-sensitive verification.
Can I verify a file without a checksum file?
Yes. Compute the current checksum with md5sum file and manually compare it to the expected hash string. Some websites provide the hash in the download page; you can copy and paste it into a text editor for comparison.
How do I generate MD5 checksums for all files in a directory recursively?
Use the find command: find /path/to/dir -type f -exec md5sum {} +. This will output checksums for every file in the directory and its subdirectories. Redirect the output to a file if needed.