- How to Verify SHA256 Checksum of File in Linux
- What is a SHA256 Checksum?
- Verifying SHA256 Checksum of a File in Linux
- What is Checksum? How to Check if a File was Modified Using the cksum Command in Linux
- How to Check if a File Was Modified by Checking Modification Time
- How to Check if a File Was Modified by Checking File Size
- What is Checksum in Linux?
- How to Find the Checksum in Linux using cksum
- Syntax of cksum
- How to Use cksum
- Conclusion
- How to Verify Checksum on Linux [Beginner Guide]
- How is a Checksum generated?
- Installing GtkHash on Ubuntu
- Using GtkHash
- Verify checksums via Linux command line
- Generating and Verifying SHA256 Checksum with sha256sum
- How accurately does this work?
- Checksum — File Integrity Check on Linux Command Line
- Conclusion
- Resource and Links
How to Verify SHA256 Checksum of File in Linux
Internet security is one of the most important aspects when it comes to the world wide web. There has been constant research and development to improve the security of applications and files on the Internet and thus to prevent malicious use.
Downloadable files over the Internet are often the target of attacks on the Internet. As thousands and thousands of people download these files regularly, it becomes especially important to protect such files.
In this article, we will learn about checksums and how they can be used to authenticate a downloaded file from the Internet.
What is a SHA256 Checksum?
A Checksum is a cryptographic signature of a file. It is basically a string that uniquely represents the file; thus if a file is ‘tampered‘ with or ‘modified‘, its checksum value will change and the user will know that the file has been changed.
There are a number of mathematical algorithms for generating Checksum for a file in Linux. One such algorithm which is very popularly used is SHA256, which stands for ‘Secure Hash Algorithm 256‘ and was developed by the United States National Security Agency.
This algorithm breaks down the data of the file into small-sized parts and creates and combines the hash values for each part to create the checksum value. The SHA256 Checksum is usually provided in a text file or directly as a string along with the main file, in its download section on the Internet.
Verifying SHA256 Checksum of a File in Linux
Let’s take an example of Ubuntu Groovy (20.10) ISO file download and try to verify its checksum. Note that along with the ISO files, the text file ‘SHA256SUMS‘ is provided which contains the checksum values.
Download the ISO file for Ubuntu 20.10 desktop and the file SHA256SUMS in the same folder and go to the folder where they are downloaded.
$ wget http://releases.ubuntu.com/groovy/ubuntu-20.10-desktop-amd64.iso $ wget http://releases.ubuntu.com/groovy/SHA256SUMS $ cd ~/Downloads
To generate a checksum of the ISO file, run the following:
$ sha256sum ubuntu-20.10-desktop-amd64.iso
To compare the checksum to the value in the file SHA256SUMS, run the command with the ‘-c’ flag. This will take all the checksums in the file, compare them with the corresponding filename, and print the filename that matches the checksum.
As seen above, the ISO file matches the original checksum, and hence we can be sure that the file was not modified or tampered with in any way during the download.
The other output is regarding the 2nd checksum in the file: for the live server ISO file which is not downloaded and hence the command prints those errors.
Conclusion
Today we learned how to verify the sha256 checksum of a file in Linux. Make sure you check out the manual page of sha256sum (by running ‘man sha256sum‘) to learn more in-depth about the command.
Thanks for reading and let us know your thoughts or questions in the comments below!
What is Checksum? How to Check if a File was Modified Using the cksum Command in Linux
Zaira Hira
When you are working with files on the command line, you might need to check their modification time and content integrity.
Linux has a powerful command line which allows you to explore multiple aspects of files and filesystems.
In case you need to check if file was modified, you can follow these two approaches:
How to Check if a File Was Modified by Checking Modification Time
When a file is edited, its timestamp changes to match the modification time.
We can view the last modified time of a file using long listing( ls -l ).
In the output below, we can see that the file was modified on Jul 19 13:22 .
zaira@Zaira:~$ ls -lrt | grep calculator.py -rw-r--r-- 1 zaira zaira 263 Jul 19 13:22 calculator.py
How to Check if a File Was Modified by Checking File Size
If we know the previous size of the file, we can compare it with the current file size to see if was changed.
We can view the file size using long listing( ls -l ). The 5th column shows the size of the file in bytes.
zaira@Zaira:~$ ls -lrt | grep calculator.py -rw-r--r-- 1 zaira zaira 263 Jul 19 13:22 calculator.py
The methods mentioned above usually get the task done, but there is an advanced method to check file integrity using a hash. The method is called ‘checksum’ and the corresponding command for that in Linux is cksum .
What is Checksum in Linux?
Sometimes the data gets corrupted during transmission or storage. To ensure that the data remains consistent, we can use checksum.
Checksum is the result of an algorithm called a cryptographic hash function. It’s applied to blocks of the data in the file.
In networking, you can use checksum to compare the hash value at sender and receiver ends. If the hash value is same, it implies that your copy of the file is genuine and error free.
Some commonly used cryptographic hash functions include MD5 and SHA-1.
Next we will see how we can calculate the hash in Linux.
How to Find the Checksum in Linux using cksum
cksum is a command found in *nix-like operating systems that generates a checksum value for a file or stream of data.
According to the man page of cksum , the command prints CRC (cyclic redundancy check) checksum and byte counts of each FILE.
To learn more about the CRC algorithm, refer to this page.
Syntax of cksum
The cksum command takes the filename as an argument and generates its hash value. The basic syntax is as follows:
How to Use cksum
Let’s suppose we have a file named calculator.py . We can calculate its checksum like this:
zaira@Zaira:~$ cksum calculator.py 1991291549 262 calculator.py
In the output, we get three columns:
- The first column is the hash value.
- The second value is the amount of data in bytes for the given file.
- The third column is the file name.
Even a slight modification changes the hash value. Let’s see how that looks with an example.
Let’s modify our original file calculator.py by adding an extra line at the end:
zaira@Zaira:~$ echo >> "this file is now changed" >> calculator.py
Let’s calculate the checksum again and see if the hash value has changed:
zaira@Zaira:~$ cksum calculator.py 331872555 263 calculator.py
The first column is the hash value and it has changed since we appended the text.
Now we know that the file has changed as the checksum hash values are no longer the same.
We can use the same method to compare files with the same name, size, and modification time across different machines to ensure that both files are the same.
Conclusion
There are cases when you need to compare the files across systems, specially when they are transferred from one location to the other. We can use a combination of the three methods to verify if our file is intact:
- Viewing the file modification time.
- Verifying the file size.
- Generating and comparing the hash value using cksum .
I hope you found this tutorial helpful. Thank you for reading till the end.
What’s your favorite thing you learned from this tutorial? Let me know on Twitter!
You can also read my other posts here.
How to Verify Checksum on Linux [Beginner Guide]
A checksum is a small-sized datum from a block of digital data for the purpose of detecting errors which may have been introduced during its transmission or storage.
So a checksum is a long string of data containing various letters and numbers. You’ll generally find them when downloading files from the web, e.g. Linux distribution images, software packages, etc.
The most common use of checksums is for checking if a downloaded file is corrupted.
For instance, the Ubuntu MATE download page includes an SHA-256 checksum for every image it makes available. So after you’ve downloaded an image, you can generate an SHA-256 checksum for it and verify that the checksum value matches the one listed on the site.
If it doesn’t, that means your downloaded image’s integrity is compromised (maybe it was corrupted during the download process). We will use an Ubuntu MATE “ubuntu-mate-16.10-desktop-amd64.iso” image file for this guide.
How is a Checksum generated?
Each checksum is generated by a checksum algorithm. Without going into the technical details let’s just say it takes a file as input and outputs the checksum value of that file. There are various algorithms for generating checksums. The most popular checksum algorithms are:
Let’s see how to verify a checksum on Linux.
Installing GtkHash on Ubuntu
To install GtkHash on your Ubuntu system, simply run the following command:
That’s it. Then select the checksum algorithms to use:
- Go to Edit >Preferences in the menu.
- Select the ones you’d like to use.
- Hit the Close button.
By default, MD5, SHA-1 and SHA256 are selected.
Using GtkHash
Using it is quite straight-forward.
- Select the file you want to check.
- Get the Checksum value from the website and put it in the Check box.
- Click the Hash button.
- This will generate the checksum values with the algorithms you selected.
- If any one of them matches with the Check box, it will show a small tick sign beside it.
Here’s an example showing GtkHash generating a checksum for the Ubuntu MATE iso image (ubuntu-mate-16.10-desktop-amd64.iso):
Verify checksums via Linux command line
Every Linux distribution comes with tools for various checksum algorithms. You can generate and verify checksums with them. The command-line checksum tools are the following:
- MD5 checksum tool is called md5sum
- SHA-1 checksum tool is called sha1sum
- SHA-256 checksum tool is called sha256sum
There are some more available, e.g. sha224sum, sha384sum, etc. All of them use similar command formats. Let’s see an example using sha256sum. We’ll use the same “ubuntu-mate-16.10-desktop-amd64.iso” image file that we used before.
Generating and Verifying SHA256 Checksum with sha256sum
First go to the directory where the .iso image is stored:
Now, to generate the SHA-256 checksum, enter the following command:
sha256sum ubuntu-mate-16.10-desktop-amd64.iso
You’ll see the SHA-256 checksum in your terminal window! Easy, isn’t it?
If the generated checksum matches the one provided on the Ubuntu MATE download page, that will mean no data was changed while you downloaded the file – in other words, your downloaded file is not corrupted.
The other tools mentioned work similarly.
How accurately does this work?
If you’re wondering how accurately these checksums detect corrupted files – if you delete or change even one character from any one of the text files inside the iso image, the checksum algorithm will generate a totally different value for that changed image. And that will definitely not match the checksum provided on the download page.
Do you checksum?
One of the suggested steps while installing Linux is to verify the checksum of your Linux ISO. Do you always follow this step or do you do it only when something goes wrong with the installation?
Was this guide helpful? If you have any questions, let us know! And if you need a similar guide for something else, reach out to us, we’re here to help.
Checksum — File Integrity Check on Linux Command Line
A checksum is a string of characters and numbers generated by running a cryptographic hash function against a file. You can use this output, or checksum, to verify that a file is genuine, error free and has not been changed from it’s original source.
A checksum is a small-sized datum derived from a block of digital data for the purpose of detecting errors that may have been introduced during its transmission or storage.
-Wikipedia
Once you have the file downloaded, you check check the integrity of the file by running sha256sum and comparing the results with the hash on the website.
$ sha256sum kali-linux-2019.2-amd64.iso 67574ee0039eaf4043a237e7c4b0eb432ca07ebf9c7b2dd0667e83bc3900b2cf kali-linux-2019.2-amd64.iso
If the strings match, you file has downloaded successfully and has not been altered since the folks at Kali created the checksum.
Conclusion
Checking the integrity of a file is an important step in ensuring a secure system. Specifically when downloading files from the internet. In this article we discussed how to generate a checksum and how to use it for a file integrity check. Now that you know how to use them, you should read up on hashing algorithms.