Introduction
When you download a file from the internet, it is a good safety practice to check whether you received the original version. Comparing checksums you received from the file creator with the ones you obtain by checking the file yourself is a reliable way to confirm your download’s integrity.
The md5sum
command in Linux helps create, read, and check file checksums.
In this tutorial, you will learn how to use the md5sum
command to validate the files you receive.
Prerequisites
- A system running Linux
- Access to the command line
The md5sum Command with Examples
When used on a file without any options, the md5sum
command displays the file’s hash value alongside the filename. The syntax is:
md5sum [filename]
After obtaining the hash value, compare it with the MD5 value provided by the file creator.
Note: While md5sum
is a reliable method for testing whether the file you received has been compromised, it is useful only if you know that the website you downloaded it from is secure. If hackers gain access to the website, they can change both the file and its checksum, making it appear as if the file you are downloading is safe.
Read in Binary Mode
To read the file in binary mode, use the -b
option (--binary
):
md5sum -b [filename]
The *
character before the file name means that md5sum
read it in binary mode.
Read in Text Mode
Use the -t
option (--text
) to read the file in text mode:
md5sum -t [filename]
Text mode is the default mode for reading files with md5sum
.
Create a BSD-Style Checksum
Using the --tag
option outputs the hash value in the BSD-style format:
md5sum --tag [filename]
Validate md5 Checksum with a File
To check a file by comparing its hash value with the value provided in a hash file, use the -c
option.
1. As an example, create a hash file containing the md5sum
output:
md5sum [filename] > [file-containing-hashes]
2. Use the following syntax to compare the hash value from the file you created against the current hash value of the .txt
file:
md5sum -c [file-containing-hashes]
3. If you change the contents of the file and repeat the check, a warning message is displayed:
Validate Multiple Files
Use the same md5sum -c
procedure to check the integrity of multiple files:
md5sum [filename1] [filename2] [filename3] > [file-containing-hashes]
In the following example, the contents of example2.txt
have changed, resulting in a warning message from md5sum
:
Display Only Modified Files
The --quiet
option displays only the files whose hash value has changed. It skips the output of validated files.
md5sum --quiet -c [file-containing-hashes]
Generate Status Only
The md5sum
command with the --status
option does not produce any output but returns 0
if there are no changes and 1
if it detects changes. This argument is useful for scripting, where there is no need for standard output.
The example script below illustrates the use of the --status
option:
#!/bin/bash
md5sum --status -c hashfile
Status=$?
echo "File check status is: $Status"
exit $Status
When the script executes, it shows status 1
, meaning that md5sum
detected the change made earlier in example2.txt
.
Check Improperly Formatted Checksum Lines
Add the --strict
option to exit non-zero for improperly formatted hash values:
md5sum --strict -c [file-containing-hashes]
The example shows the output of md5sum --strict
when you put invalid characters in the first line of the file containing hashes:
To display which line has an invalid hash, use -w
(--warn
):
md5sum -w -c [file-containing-hashes]
The example above shows the -w
option displaying that the improperly formatted MD5 checksum line is line 1 of the file.
Skip Reporting Status for Missing Files
By default, md5sum
shows warnings about the files it cannot find on the system. To override this behavior, use the --ignore-missing
option:
md5sum --ignore-missing -c [file-containing-hashes]
In the example below, example1.txt
was deleted before running the md5sum
command. The output ignores the deleted file:
Show Help and Version Information
To get the official help for the md5sum
command, type:
md5sum --help
To check md5sum version, type:
md5sum --version
Note: You should also check out our overview of the diff command to learn how to compare two files line by line.
Conclusion
After completing this tutorial, you should know how to properly use the md5sum
command to create, print, or check MD5 checksums.
原创文章,作者:bd101bd101,如若转载,请注明出处:https://blog.ytso.com/224312.html