The Wayback Machine - https://web.archive.org/web/20250305222125/https://www.geeksforgeeks.org/md5sum-linux-command/
Open In App

md5sum Command in Linux with Examples

Last Updated : 19 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The md5sum is designed to verify data integrity using

MD5

(Message Digest Algorithm 5). MD5 is 128-bit cryptographic hash and if used properly it can be used to verify file authenticity and integrity. Example :

Input : md5sum /home/mandeep/test/test.cpp
Output : c6779ec2960296ed9a04f08d67f64422 /home/mandeep/test/test.cpp

Importance :

Suppose, anyone wants to install an operating system , so to verify if it’s correct CD, it’s always a good idea to verify .iso file using MD5 checksum, so that you don’t end up installing wrong software (some sort of virus which can corrupt your filesystem).

Syntax :

md5sum [OPTION]... [FILE]...

It will print or check MD5(128-bit) checksum. It computes MD5 checksum for file “test.cpp” Output :

c6779ec2960296ed9a04f08d67f64422  /home/mandeep/test/test.cpp

Options :

-b :

read in binary mode

-c :

read MD5 from files and check them

–tag :

create a BSD-style checksum

-t :

read in text mode(it’s by default)

The options which are useful when verifying checksum :

–ignore-missing :

don’t report status for missing files

–quiet :

don’t print OK for each successfully verified file

–status :

don’t output anything, status code shows success

–strict :

exit non-zero for improperly formatted checksum files

-w :

warn about improperly formatted checksum files

Command usage examples with options :

Example 1: Store the MD5 checksum in file and then verify it.

# md5sum /home/mandeep/test/test.cpp > checkmd5.md5

It will store the MD5 checksum for test.cpp in file checkmd5.md5

# md5sum -c checkmd5.md5

It will verify the contents of file Output :

/home/mandeep/test/test.cpp: OK

After changing the contents of file checkmd5.md5, the output will be :

/home/mandeep/test/test.cpp: FAILED
md5sum: WARNING: 1 computed checksum did NOT match

Example 2: create a BSD-style checksum with –tag option

# md5sum --tag /home/mandeep/test/test.cpp

Output :

MD5 (/home/mandeep/test/test.cpp) = c6779ec2960296ed9a04f08d67f64422

Example 3: –quiet option, can be used when verifying checksum, don’t print OK when verification is successful.

#  md5sum -c --quiet  checkmd5.md5 

Don’t produce any output, means it’s successful. But if checksum don’t match, it produces warning.

# md5sum -c --quiet  checkmd5.md5
/home/mandeep/test/test.cpp: FAILED
md5sum: WARNING: 1 computed checksum did NOT match

Example 4: –warn option, it can be used for generating a warning for improperly formatted checksum files. content of file checkmd5.md5:

c6779ec2960296ed9a04f08d67f64422 /home/mandeep/test/test.cpp

Now, execute command with –warn option

# md5sum -c --warn  checkmd5.md5
/home/mandeep/test/test.cpp: OK

It don’t produce any warning. Now, do some formatting in file checkmd5.md5

c6779ec2960296ed9a04f08d67f64422 
/home/mandeep/test/test.cpp

Now, execute the command

# md5sum -c --warn  checkmd5.md5

Output :

md5sum: checkmd5.md5: 1: improperly formatted MD5 checksum line
md5sum: checkmd5.md5: 2: improperly formatted MD5 checksum line
md5sum: checkmd5.md5: no properly formatted MD5 checksum lines found

and if –warn is replaced with –strict option, it will exit non-zero for improperly formatted checksum lines

# md5sum -c --strict  checkmd5.md5
md5sum: checkmd5.md5: no properly formatted MD5 checksum lines found

md5sum Command in Linux with Examples – FAQs

What is the md5sum command used for in Linux?

The md5sum command in Linux is primarily used to compute and verify the MD5 (Message-Digest Algorithm 5) hashes of files. MD5 is a widely used cryptographic hash function that produces a 128-bit (16-byte) hash value. It’s typically expressed as a 32-digit hexadecimal number. The md5sum tool is commonly used for checking data integrity. Its main purpose is to ensure that files have not been altered, whether by corruption, file transfer errors, or malicious intent.

How to generate an MD5 checksum for a file using md5sum?

To generate an MD5 checksum for a file, you can use the md5sum command followed by the filename. For example, to calculate the MD5 checksum of a file named example.txt, you would use the following command:

md5sum example.txt

This command will output the MD5 checksum followed by the filename. The output might look something like this:

d41d8cd98f00b204e9800998ecf8427e  example.txt

How to verify a file’s integrity with md5sum?

To verify a file’s integrity using md5sum, you need a previously generated MD5 checksum for comparison. Here’s how you can verify the integrity:

  1. First, save the expected MD5 checksum in a file. For instance, if you have an MD5 checksum d41d8cd98f00b204e9800998ecf8427e for example.txt, you would save this in a file (often called md5sum.txt) like this:
  2. Use md5sum with the -c (check) option to verify the file against the checksum in md5sum.txt:This command reads the MD5 checksum from md5sum.txt and compares it against the checksum computed for the current state of example.txt. The output will indicate whether the file is OK or FAILED.
d41d8cd98f00b204e9800998ecf8427e  example.txt
md5sum -c md5sum.txt

Can md5sum check multiple files at once?

Yes, md5sum can check multiple files at once. To do this, generate MD5 checksums for all the files you want to check and save them in a single checksum file. Here’s an example of how to generate the checksums for all files in a directory and save them:

md5sum * > md5sums.txt

You can then verify all files at once using:

md5sum -c md5sums.txt

This process will check each file listed in md5sums.txt and report on their integrity individually.

What are the limitations of using md5sum?

While md5sum is useful for checking data integrity, it has several limitations:

  • Security Vulnerabilities: MD5 is considered cryptographically broken and unsuitable for further use in security-sensitive applications. It is vulnerable to hash collisions, where two different inputs produce the same output hash, which can be exploited in various cryptographic attacks.
  • Performance: For very large files, generating an MD5 hash can be computationally intensive and time-consuming.
  • Not Foolproof for Security: Because of the vulnerabilities to collisions, MD5 checksums are not recommended for security verification purposes, like ensuring that a file has not been tampered with maliciously.
  • Limited to File Contents: md5sum only checks file contents and does not consider metadata changes, such as changes to file names, timestamps, or permissions.


Next Article

Similar Reads

three90RightbarBannerImg