Linux хэш сумма файла

How to check the checksum through commandline?

I dont want to write a separate script text page just to check this.
This above thing is showing syntax error. But there must be a slick way to get around this? How to do this?

12 Answers 12

shasum httpd-2.4.7.tar.bz2 | awk '$1=="19asdasdasd56462e44d61a093ea57e964cf0af05c0e"' 

So normally you get this output from shasum

19asdasdasd56462e44d61a093ea57e964cf0af05c0e *httpd-2.4.7.tar.bz2

What my command does it is takes the first field $1 , and compares it against your string. If the strings match, then awk prints «good to go».

Note that for anything other than sha-1 , you need to specify your algorithm. For example, for sha 256 , you can do:

shasum -a256 httpd-2.4.7.tar.bz2 

The -a flag specifies the algorithm.

Maybe obvious for most, but, for sha256, use sha256sum . Also, for those wondering, like me: awk and awk.

Also maybe not obvious to everyone, shashum -a256 and sha256sum are not different names for the same command. They are different utilities that may or may not already be on your system depending on your platform.

echo "19asdasdasd56462e44d61a093ea57e964cf0af05c0e httpd-2.4.7.tar.bz2" \ | shasum -c 

Simply using grep seems to be the best approach:

> shasum httpd-2.4.7.tar.bz2 | grep 19ed9ee56462e44d61a093ea57e964cf0af05c0e 

The checksum will be highlighted when it is correct:

Command execution snapshot

And when checksum is incorrect, nothing shows.

  • Check $? in bash scripting. grep exits with code 0 while something is found, and code 1 while nothing found.
  • If you are given an upper-case checksum, use grep -i .
  • This approach is typically used in situations when you can ensure that checksum doesn’t exist in the file name, like, curl -o or wget -O then check.

I use the exit code of the previous/last command:

If the checksum is valid the exit code of the last executed command is 0 :

> echo "$ prometheus-$.linux-arm64.tar.gz" | sha256sum -c > echo $? 0 

If the checksum is not correct, then the exit code is different than 0 :

> export PROMETHEUS_CHECKSUM='some garbage' > echo "$ prometheus-$.linux-arm64.tar.gz" | sha256sum -c prometheus-2.0.0.linux-arm64.tar.gz: FAILED sha256sum: WARNING: 1 computed checksum did NOT match > echo $? 1 

And here is the whole example with an if statement:

#!/bin/bash . echo "$ prometheus-$.linux-arm64.tar.gz" | sha256sum -c if [ $? != 0 ]; then echo 'Prometheus checksum is not valid' exit 1 fi 
shasum httpd-2.4.7.tar.bz2 | while read -r sum _ ; do [[ $sum == 19asdasdasd56462e44d61a093ea57e964cf0af05c0e ]] && echo "good" || echo "bad" done 

The test operator is enclosed in [ .. ] and the proper syntax is if; then; fi but you can use && and || operators to simulate it.

Test:

$ touch httpd-2.4.7.tar.bz2 $ shasum httpd-2.4.7.tar.bz2 | while read -r sum _ ; do [[ $sum == 19asdasdasd56462e44d61a093ea57e964cf0af05c0e ]] && echo "good" || echo "bad"; done bad $ shasum httpd-2.4.7.tar.bz2 | while read -r sum _ ; do [[ $sum != 19asdasdasd56462e44d61a093ea57e964cf0af05c0e ]] && echo "good" || echo "bad"; done good $ bash --version GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13) Copyright (C) 2007 Free Software Foundation, Inc. 

Источник

Читайте также:  Полностью удалить postgresql linux centos

How to Use the md5sum Command in Linux

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.

How to Use the md5sum Command in Linux

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:

Checking the hash value of a file

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 ):

Using the -b option to read checksum in binary mode

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:

Using the -t option to read checksum in text mode

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:

Using the --tag option to output checksum in a BSD format

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]

Validating a file using a file containing its hash value, no failures detected

3. If you change the contents of the file and repeat the check, a warning message is displayed:

Validating a file using a file containing its hash value, failure detected

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 :

Validating multiple files using a file containing their hash value, failure detected

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]

Validating multiple files using a file containing their hash value, skipping the output of the validated files

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 .

Читайте также:  Load sound driver linux

Executing a script illustrating md5sum

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:

Using the --strict option to exit non-zero for improperly formatted hash values

To display which line has an invalid hash, use -w ( —warn ):

md5sum -w -c [file-containing-hashes]

Using the -w option to display which line has an invalid hash

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:

Using the --ignore-missing option to ignore files that are missing from the system

Show Help and Version Information

To get the official help for the md5sum command, type:

To check md5sum version, type:

Note: You should also check out our overview of the diff command to learn how to compare two files line by line.

After completing this tutorial, you should know how to properly use the md5sum command to create, print, or check MD5 checksums.

Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.

The echo command prints out a text string you provide as the output message. This tutorial covers the echo.

The ls command (short for list) lists information about directories and any type of files in the working.

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or.

Creating a file in Linux might seem straightforward, but there are some surprising and clever techniques. In.

Источник

How to Get md5 Hash Of A File

Message-Digest algorithm, commonly known as md5 hash, is a type of cryptographic hash function mainly used to verify the integrity of files. Md5 is a 128-bit message digest produced after running the MD5 function against a file.

Md5 has its flaws and is therefore not a very good choice for certain encryption methods, but it is very well suited for file verification. It works by creating a checksum of a file and comparing the result to the original. That means if there are changes to a file, there is no way it can produce a digest value similar to the original. The value stays constant no matter where generated or how many times as long as the file remains unchanged.

For this guide, we shall look at ways to generate an md5 hash value of a file. That will allow you to verify the integrity of files either from remote locations or on your local machine.

Читайте также:  Linux восстановление удаленных данных

Install md5sum

In Linux and almost major Unix and Unix-Like systems, they come pre-installed with an md5 tool. The most common one is md5sum. By default, you should find it available in your system.

If you do not have the tool installed, you can use the package manager of your system.

Debian/Ubuntu
On Ubuntu and other Debian based distributions, use apt as:

REHL/CentOS
On REHL and CentOS, use yum as:

Arch/Manjaro
If you are on Manjaro or other arch based distributions, use Pacman using the command:

Fedora
Finally, on Fedora systems, use the dnf command as:

Generate Md5sum of a File

With the tool installed, we can proceed and generate a md5sum for a file. You can use any basic file available in your system. In my example, I am using the /etc/hosts available in Linux systems.

To generate the md5sum of a file, simply use the md5sum command followed by the filename, which you can see in the command below:

The above command should generate a hash value of the file as shown in the output below:

Once the contents of the file change, the md5sum value becomes completely different. For example, add a value to the/etc/hosts file.

Add the following entry to the file (feel free to change to any way you see fit).

If you try to calculate the md5 value of the file with the new contents as:

The hash value is different as shown in the output below:

If you revert the file to its original contents, the md5sum value is similar to the original, allowing you to know when a file has changed.

NOTE: The md5 value will be similar to the original even if the file gets renamed. This is because md5 is calculated based on file contents and not filename.

Verify Online Files

Suppose you want to verify the integrity of a file and ensure it is tamper-proof. To do this, all you need is the original md5 value. In my example, I am using a simple deb package of MySQL from the resource below:

Download the file with wget using the command as:

Once the file has downloaded:

Let us now verify the md5 value using a command:

If the file has not been modified in any way, you should get a similar value as the original as shown:

Conclusion

This tutorial looked at a simple method to verify the md5 checksum of files and verify their modification state.

Here is a quick exercise for you.

Exercise

Create a simple bash script that checks if a file md5 value has any recorded modification every 5 minutes. If the file has changed, delete the file and shut down the system.

That should be a fun exercise!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

Источник

Оцените статью
Adblock
detector