How to work with file checksums on Linux

This article describes how to generate MD5 and SHA file checksums and verify file integrity on Linux.

About checksums

A checksum (also sometimes referred to as a hash) is an alphanumeric value that uniquely represents the contents of a file. Checksums are often used to verify the integrity of files downloaded from an external source, such as an installation file. You can also use checksums to verify the integrity of your own files. For example, you can generate checksums for your backup files, and then verify they have not become corrupted or altered at a later date.

The two most commonly used checksum algorithms are MD5 and SHA. When verifying checksums, you must make sure you use the same algorithm that was used to generate the checksum. For example, a file's MD5 checksum value is completely different from its SHA-256 checksum value.

“SHA” is an umbrella term for an entire family of hashing algorithms. The two most commonly used SHA algorithms are:

  • SHA-1
  • SHA-2: This family includes several hashing functions, whose names indicate the hash value bit-length (for example, SHA-224, SHA-256, SHA-384, and SHA-512).

Generating checksums on Linux

All A2 Hosting servers run Linux, and therefore include command line programs for generating MD5 and SHA checksums.

To generate a file checksum and store the value in a file, follow these steps:

  1. Log in to your account using SSH.
  2. At the command prompt, type one of the following commands, replacing filename with the name of the file for which you want to generate a checksum:
    • To generate an MD5 checksum, type:
      md5sum filename > md5sums.txt
    • To generate an SHA checksum, type the name of the command for the hashing algorithm you want to use. For example, to generate a SHA-256 checksum, use the sha256sum command. To generate a SHA-512 checksum, you would type the following command:

      sha512sum filename > sha512sums.txt

    The md5sums.txt (or sha512sums.txt) file now contains a file listing and associated checksums.

    You can also generate multiple checksums at once. For example, to generate MD5 checksums for all of the .zip files in the current directory, type the following command:

    md5sum *.zip > md5sums.txt

    Similarly, to generate MD5 checksums for all of the files in the current directory and all directories beneath it, type the following command:

    find . -type f -exec md5sum {} > md5sums.txt \;

    To generate SHA checksums instead, replace md5sum with the appropriate SHA command.

Verifying checksums on Linux

All A2 Hosting servers run Linux, and therefore include command line programs for verifying MD5 and SHA checksums.

To verify file checksums, follow these steps:

  1. Log in to your account using SSH.
  2. At the command prompt, type one of the following commands based on the algorithm (MD5 or SHA) that was used to generate the checksums. These examples use the filenames md5sums.txt and sha512sums.txt. If you stored the checksums in a different file, use that filename instead.
    • To verify MD5 checksums, type:
      md5sum -c md5sums.txt
    • To verify SHA checksums, type the name of the command for the hashing algorithm you want to use. For example, to verify a SHA-256 checksum, use the sha256sum command. To verify a SHA-512 checksum, you would type the following command:

      sha512sum -c sha512sums.txt

    Each matching checksum displays OK, while a mismatched checksum displays FAILED.

    If you are checking a lot of files, mismatched results can get lost amongst all of the scrolling results. To display only mismatched checksums, use the —quiet option. For example:

    sha512sum --quiet -c sha512sums.txt

Other operating systems

You can generate and verify checksums on other operating systems:

  • For a computer running Microsoft Windows, you can install one of the many programs available for download that generates and verifies checksums.
  • For a computer running Apple macOS, you can use the md5 and shasum programs at the command line.

More Information

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

 

 

Loading