Linux commands
Browse topics

Verifying files with sha256sum

Version
unversioned
Review
Draft

Summary#

sha256sum prints a SHA-256 digest for each file and can check files against a saved list of digests.

Syntax#

sh
sha256sum release.tar.gz
sha256sum release.tar.gz > SHA256SUMS
sha256sum -c SHA256SUMS
sha256sum -c --ignore-missing SHA256SUMS
echo "$digest  release.tar.gz" | sha256sum -c

A list line holds the digest, two spaces and the file name. -c reads such lines and reports each file. The exit status is nonzero when any check fails.

Example#

sh
(
  set -eu
  dir=$(mktemp -d)
  trap 'rm -rf -- "$dir"' EXIT
  cd "$dir"
  printf 'hello\n' > release.txt

  sha256sum release.txt | tee SHA256SUMS
  sha256sum -c SHA256SUMS
  printf 'changed\n' > release.txt
  sha256sum -c SHA256SUMS 2>/dev/null || echo "status $?"
)

Expected output#

Text
5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03  release.txt
release.txt: OK
release.txt: FAILED
status 1

Common mistakes#

A warning that a computed checksum did NOT match means the file differs from the list: download it again and do not run it. A matching checksum proves only that the file equals what the list describes. If an attacker can replace both, it proves nothing, so get the digest over a trusted channel or verify a signature. Do not use md5sum or sha1sum to detect deliberate tampering. File names in the list are relative to the current directory.

Version applicability#

This entry describes sha256sum from GNU Coreutils. macOS offers shasum -a 256 instead.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0