Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Hashes and file metadata

After this lesson: I can use a hash to prove a file is unchanged and read its metadata.

55 min lesson9 commandsIncident response and forensics lab: investigate a compromised server in your browser

Is this copy the same as the original?

A hash is a short fingerprint worked out from the content of a file. If even one character of the file changes, the fingerprint changes completely.

So a hash lets you prove that a copy is the same as the original, and that nobody has edited your evidence.

By the end of this lesson

Note

I can use a hash to prove a file is unchanged and read its metadata.

Key words

WordWhat it meansExample
Hasha fixed-length fingerprint worked out from a file's content
Baselinea list of hashes taken at the start, to compare with later
Metadatadata about a file, such as its owner and times
Modified timethe time the content last changed
Ownerthe account that controls a file
Modethe permissions of a file644

The cells students fill in are left blank in this preview.

A hash is a fingerprint of content

A hash function reads all the bytes of a file and gives back a number written in hexadecimal. SHA-256 always gives 64 characters, whether the file is one letter or one gigabyte.

The same content always gives the same hash. A different content gives a different hash, even if it differs by one character.

Hash a short text

Command
printf 'invoice 100' | sha256sum

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Change one character and hash again

Command
printf 'invoice 101' | sha256sum

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Worked example: one character changes everything

TextSHA-256 hash
invoice 100dcd749973453e21279782312cd726c03028ed69267b481285a1affc360350c8e
invoice 10172b424430947b5cce4cf9488575455eeed11c1877741c2f1502e416619daa9e1

The cells students fill in are left blank in this preview.

Compare hashes to prove a file is unchanged

At the start you write down the hash of each file. This list is your baseline. Later you hash the files again and compare.

If a hash is different, the file changed. The tool sha256sum -c does the comparing for you and prints OK or FAILED for each file.

Check a file against its own hash

Command
sha256sum case/etc/passwd | sha256sum -c -

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Check against a wrong hash

Command
printf '%s  case/etc/passwd\n' 0000000000000000000000000000000000000000000000000000000000000000 | sha256sum -c -

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Compare a file with an edited copy

Command
sed 's/root/ROOT/' case/etc/passwd | cmp -s - case/etc/passwd; echo $?

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think a file's modified time proves when it was last changed. Anyone who owns a file can set its time to any date they like. Treat a time as a clue. Use a hash as proof of the content.

Metadata tells you about a file

Metadata is the information the system keeps about a file, apart from its content: its owner, its group, its mode, its size and three times.

The command stat prints it. Metadata helps you decide where to look, but unlike a hash it can be changed.

The three times on a file

TimeShort nameIt changes when
ModifiedmtimeThe content changes
AccessedatimeThe file is read
ChangedctimeThe metadata changes, such as its owner or mode

The cells students fill in are left blank in this preview.

Owner, group and mode

Command
stat -c "%U %G %a" case/etc/passwd

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Turn a number of seconds into a date

Command
date -u -d @86400 +"%Y-%m-%d %H:%M:%S"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The columns of the lab's time list

Command
head -n 1 case/mtimes.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

How many files share one day

Command
grep -c "2026-09-02" case/mtimes.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Going further

Run sha256sum case/etc/passwd in your own work folder and in the case. Copy the file with cp without any option, then hash the copy. Compare the three hashes, then compare the modified times with stat -c %y. Write down which one changed and which one did not.

About this lab and its source: Incident response and forensics lab: investigate a compromised server in your browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.