md5sum and sha256sum: Verifying File Integrity
What you'll be able to do
- Compute a file's fingerprint with `sha256sum`
- Verify recorded hashes against the current files with `-c`
- Explain how hashing differs from encryption and encoding
Prerequisites (read these first)
What You'll Learn
- Why checksums (hashes) exist and what problem they solve
- How to compute a file's fingerprint with
sha256sum - How to use
-cto check whether a file is corrupted or swapped out - How to tell hashing, encryption, and encoding apart
Words used here (sorted out first)
- Checksum / hash value / fingerprint: three names for nearly the same thing. This article says "fingerprint" when explaining the idea.
- Hashing: producing a short value from the contents. You cannot get the file back from it. It answers "same or different?"
- Encryption: making data readable only to whoever holds the key. Its goal is hiding contents, which is a different goal from hashing.
- Encoding: only changing how data is written. Anyone reverses it without a key.
base64is this.
Hashing cannot be reversed. Encryption is reversible with the key. Encoding is reversible by anyone. Keep the three apart.
Quick Summary
- Just want a fingerprint →
sha256sum file - Check it matches the official value →
sha256sum -c SHA256SUMS - For tamper protection use SHA-256. MD5 / SHA-1 are only for accidental corruption
Environment
- OS: Ubuntu / typical Linux
md5sum/sha256sumship with GNU coreutils and are preinstalled (no install needed)- Relatives include
sha1sum/sha512sum/b2sum
1. What Is a Checksum?
Conclusion: A checksum is a "fingerprint" computed from a file's contents; changing even one bit changes it drastically.
What checksums let you do
- Detect whether a downloaded file got corrupted in transit (corruption detection)
- Confirm the contents match the source (detecting swaps / tampering)
- Compare whether two files are byte-for-byte identical
A fingerprint cannot rebuild the contents
Hashing is a one-way street. You cannot restore the original file from the fingerprint. That is why it hides nothing. Use encryption when you need to hide something.
2. Computing a Fingerprint with sha256sum
Conclusion:
sha256sum filecomputes the hash; output is "hash + two spaces + filename".
2-1. Basics: Fingerprint a Single File
$ sha256sum ubuntu.iso
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 ubuntu.iso
How to read the output
e3b0c4...b855 ubuntu.iso
└── hash ────┘└┘└ filename
two spaces- The first space is a separator
- The second character marks the input mode (
= text /*= binary) - On GNU systems both modes are identical (kept for historical compatibility)
2-2. md5sum Works the Same Way
$ md5sum ubuntu.iso
d41d8cd98f00b204e9800998ecf8427e ubuntu.iso
md5sum computes MD5 and sha256sum computes SHA-256. That is the only difference; the usage is identical. The same goes for sha1sum / sha512sum.
3. Multiple Files and Saving a List
Conclusion: You can hash many files at once and save the list with
>to reuse it later with-c.
3-1. Hash Several Files at Once
$ sha256sum *.iso
e3b0c4...b855 ubuntu.iso 9f86d0...0a08 debian.iso
3-2. Save the List to a File
$ sha256sum *.iso > SHA256SUMS
The name SHA256SUMS is a common convention on distribution sites. The file is just plain text with "hash + filename" on each line.
Where it lands, and the overwrite risk
SHA256SUMSis created in the directoryA container that organizes files. Same idea as a "folder" on Windows or macOS. you are in right now. Check it withpwd>wipes the contents and overwrites if that name already exists. Nothing asks for confirmation (doubled up as>>, it appends to the end instead)- To avoid losing an important list, run
ls SHA256SUMSfirst to see whether it exists sha256sumdoes not run in this site's virtual terminalAn interactive program that reads the commands you type and runs them.. Run it in your own terminal- Practice inside the empty directory you create in "8. Mini Exercises". Nothing you already had can break there
-c, you can verify in bulk whether the saved fingerprints still match the current files. It's handy for periodic checks that a backup hasn't rotted.4. Verifying with -c (the Main Event)
Conclusion:
sha256sum -c listfilecompares recorded hashes against the current files:OKif they match,FAILEDif not.
4-1. Check Against a List
$ sha256sum -c SHA256SUMS
ubuntu.iso: OK debian.iso: OK
If a file's contents changed, you get:
ubuntu.iso: OK debian.iso: FAILED sha256sum: WARNING: 1 computed checksum did NOT match
FAILED, does that mean the file is broken?4-2. Verify Against a Single Official Value
Sometimes a download page lists only one line, like "SHA256: official value". Turn that one line into a list file and verify against it.
# Write the official value and filename on one line (two spaces) $ echo "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 ubuntu.iso" > check.txt $ sha256sum -c check.txt
ubuntu.iso: OK
Don't retype the separator
Keep the gap between the "hash" and the "filename" exactly as sha256sum printed it: two spaces. With three or more, the extra blank becomes part of the filename and you get No such file or directory. When copying an official value, don't add stray whitespace.
4-3. Useful Options for Verifying
# Hide OK lines, show only failures $ sha256sum -c --quiet SHA256SUMS # Ignore files not in the list (when checking only some) $ sha256sum -c --ignore-missing SHA256SUMS # Print nothing, judge by exit code (for scripts) $ sha256sum -c --status SHA256SUMS && echo "all match"
--status prints nothing. It decides solely by exit code (0 success / 1 failure). The exit code is the number that says whether the previous command succeeded; check it with echo $?. It's ideal for automated checks in shell scripts.
5. Corruption Detection Is Not Tamper Detection
Conclusion: Any hash can catch accidental corruption, but defending against malicious tampering requires SHA-256.
Two goals
| Goal | What it prevents | Usable hashes |
|---|---|---|
| Corruption check | Accidental transit/disk corruption | MD5 / SHA-1 are fine |
| Tamper check | Deliberate swaps by an attacker | SHA-256 or stronger |
6. Why MD5 Is Discouraged Today
Conclusion: MD5 and SHA-1 allow practical collision attacks ("different contents, same fingerprint"), so they're discouraged for security.
When NOT to use MD5 / SHA-1
- Verifying the authenticity of a downloaded file (is it genuine?)
- Hashing passwords
- Anything security-related such as signatures or certificates
For these, use SHA-256 or stronger (sha256sum / sha512sum).
7. Common Beginner Pitfalls
Conclusion: Misreading upper/lowercase, the number of spaces, and stray line endings are the typical causes of failed verification.
7-1. Comparing by Eye and Missing It
-c do the comparison. It never misses a single character.# Bad: compare by eye (you'll miss something) # Good: make a list and verify with -c $ sha256sum -c SHA256SUMS
7-2. Extra Whitespace in the Separator
Put three or more spaces in the separator and you get this.
sha256sum: ' ubuntu.iso': No such file or directory ubuntu.iso: FAILED open or read sha256sum: WARNING: 1 listed file could not be read
FAILED open or read — rather than plain FAILED — does not mean the contents differ. It means the filename could not be read. A list made with sha256sum file > SHA256SUMS never takes this shape.
7-3. A Windows-Made List Won't Verify
Files from Windows use \r\n (CRLF) line endings. The trailing \r can cause trouble.
# Convert CRLF to LF before verifying $ tr -d '\r' < SHA256SUMS.txt | sha256sum -c -
The trailing - means "read what comes through the pipe instead of a file." Think of it as putting - where the filename goes in sha256sum -c SHA256SUMS.
7-4. Hashes Match but the Files "Look Different"
If only the filename differs but the contents are identical, the hash matches. A hash looks only at contents — not the filename or timestamp.
8. Mini Exercises: Learn by Doing
Conclusion: Create your own file and walk through computing, saving, verifying, and detecting corruption to make it stick.
Make an empty practice directory and work inside it. That way you never overwrite a file you already had.
# Prepare a practice directory and move into it $ mkdir -p ~/checksum-practice && cd ~/checksum-practice
Exercise 1: Create a file with echo hello > a.txt and print its SHA-256 hash.
Show Hint 1 (Direction)
Pass the filename to the command that computes the fingerprint. No options needed.
Show Hint 2 (Command name)
Use sha256sum.
Show Answer
$ echo hello > a.txt $ sha256sum a.txt
5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 a.txt
The 64-digit fingerprint comes first, then the filename, with two spaces between.
Exercise 2: Save a.txt's hash list to SUMS and verify it to see OK.
Show Hint 1 (Direction)
Write the computed result into a file. Then have the command read that list back and check it.
Show Hint 2 (Command name)
The command is sha256sum. Save with the > redirection and verify with -c.
Show Answer
$ sha256sum a.txt > SUMS $ sha256sum -c SUMS
a.txt: OK
OK means the saved fingerprint and the current contents are the same.
Exercise 3: Modify a.txt, then verify again and confirm you get FAILED.
Show Hint 1 (Direction)
Append one line to the file. Then run the same verification as Exercise 2.
Show Hint 2 (Command name)
Append with >> (> would overwrite). The verification is the same command as Exercise 2.
Show Answer
$ echo world >> a.txt $ sha256sum -c SUMS
a.txt: FAILED sha256sum: WARNING: 1 computed checksum did NOT match
When the contents change, the fingerprint changes and verification turns to FAILED.
9. Copy-Paste Templates
Conclusion: The standard forms for computing, saving a list, verifying, and matching a single official value are ready to copy.
Checksum Templates
# Fingerprint a single file sha256sum file # Hash several files at once sha256sum *.iso # Save a list (overwrites an existing name) sha256sum *.iso > SHA256SUMS # Verify against a saved list sha256sum -c SHA256SUMS # Show only failures sha256sum -c --quiet SHA256SUMS # For scripts (judge by exit code) sha256sum -c --status SHA256SUMS && echo OK # Verify against a single official value (trailing - reads the pipe) echo "<official hash> file.iso" | sha256sum -c - # When you want a stronger hash sha512sum file
What not to do
- Use MD5 / SHA-1 for tamper protection
- Compare 64 digits by eye (verify mechanically with
-c) - Retype the separator by hand (copy
sha256sum's own output instead)
Looking Back
Conclusion: A fingerprint cannot be reversed, and
-ccompares it, not your eyes.
-c decide. That was exactly my mistake.Three-Line Recap
Conclusion: Compute with sha256sum, verify with -c, and pick SHA-256 against tampering.
sha256sum filecomputes a file's fingerprint. A hash cannot be reversed- Don't compare by eye. Let
sha256sum -c listfilemake the call - Use SHA-256 or stronger against tampering. Keep MD5 / SHA-1 for accidental corruption only