Linux / Unix: Generate A MD5 String or Hash

I need to generate a md5 hash for given string. It will be used by shell script to generate keys for remote web service or cryptographic application. How do I generate a md5 hash based on any input string under Linux or Unix like operating systems?

Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements md5sum command
Time 5m
You can use md5sum command to compute and check MD5 message digest. This is a default tool on most modern Linux distributions. It generate a md5 hash for given string or words or filenames.

Creating a md5 string using md5sum command

Use the following syntax:

VAR="some_value"
echo -n 'Your-String-Here' | md5sum
echo -n "${VAR}" | md5sum
echo -n 'some-value' | md5sum [options]

In this example create a md5 hash for wpblog string that can be used by memcached server

echo -n 'wpblog' | md5sum

Sample outputs:

6afedb7a8348eb4ebdbe0c77ef92db4c -

You can store the same in a bash shell variable called hash as follows:

md5="set-string-here"
hash="$(echo -n "$md5" | md5sum )"
echo "$hash"

The -n option passed to echo command to avoid appending a newline. Want to show a BSD-style MD5 checksum on GNU/Linux? Try the following syntax:
echo -n 'word1-word2-foo' | md5sum --tag

If you have openssl installed, try:

echo -n 'string-here' | openssl md5
echo -n "${VAR}" | openssl md5

WARNING: MD5 has been deprecated for some time. Using salted md5 for passwords is a flawed idea. Please do not use it. Using MD5 for file integrity check is also not recommended anymore. This page exists for historical reasons. If possible, use SHA-256 or above.

Checking md5 checksums

The syntax is as follows to read checksums from a file named input.file.md5:
md5sum -c --ignore-missing input.file.md5
md5sum --check --ignore-missing input.file.md5

Please note that the MD5 sums are computed as described in RFC 1321. When checking, the input should be a former output of this program. The default mode is to print a line with checksum, a space, a character indicating input mode (‘*’ for binary,
‘ ‘ for text or where binary is insignificant), and name for each FILE.

Other options to test or generate a MD5 string

Don’t want to print OK for each successfully verified file? Try passing the --quiet option:
md5sum --quiet -c --ignore-missing input_md5_sum_file
Same way don’t output anything, status code shows success by passing the --status option to the md5sum command:
md5sum --status -c input_file

Getting help

Type the following command:
md5sum --help
We will see:

Usage: md5sum [OPTION]... [FILE]...
Print or check MD5 (128-bit) checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read MD5 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode (default)
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping

The following five options are useful only when verifying checksums:
      --ignore-missing  don't fail or 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 lines
  -w, --warn           warn about improperly formatted checksum lines

      --help     display this help and exit
      --version  output version information and exit
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/md5sum>
or available locally via: info '(coreutils) md5sum invocation'

Recommended readings and conclusion

We learned how to compute and check MD5 message digest on a Linux or Unix-like system using the command-line option. For more info see the following resources:

  • See md5sum man page for more information.
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
13 comments… add one
  • anton bors Jun 6, 2012 @ 8:07

    Reading from standard input:
    $ md5sum -

    a little script:

    #!/bin/bash
    echo $1
    echo $1 | md5sum 

    $ ./md5 hallo
    hallo
    aee97cb3ad288ef0add6c6b5b5fae48a –

    The “-” declares, that this message comes from standard input
    greets

    • AtesComp Feb 21, 2014 @ 1:45

      Beware this script! You should use the “-n” option for echo as shown in the main article. Otherwise, a newline is appended to the output of the echo command and your hash will not be as advertised (unless you really want the newline).

      #!/bin/bash
      echo $1
      echo -n $1 | md5sum

      $ ./md5 hallo
      hallo
      598d4c200461b81522a3328565c25f7c –

      Not “aee97cb3ad288ef0add6c6b5b5fae48a” as shown above.

      • Dorell James Galang Feb 17, 2016 @ 4:03

        If your hash contains a “–” at the end. Well, seriously this is not correct. I suggest you use this command instead to get rid of that character.

        echo -n "samplestring" | md5sum | cut -d"-" -f1 -

  • Roy Bellingan Jun 12, 2012 @ 9:32

    Remember that md5 should only used for file hash now, in recent days linked_in had a major password leakage for using md5…
    md5 and password leaked.

    • Drew H Dec 13, 2012 @ 3:21

      You’re an idiot mate

      • Roy Bellingan Dec 13, 2012 @ 9:15

        Thanks, i’ll write this on my tombstone!

  • Guy Merritt Aug 23, 2013 @ 17:50

    I love this site – it’s got more simple, helpful tips for *Nix amateurs (like me!) than I’ve ever seen elsewhere. But, for the life of me, I can’t find a site search anywhere. I was looking for this page, because I’d forgotten the syntax to generate an encrypted password. What’s up with no site search…? If I wanted to search Google, I’d search Google. Anyway, thanks for all of the help. Great site.

  • Nick Gar Jul 12, 2016 @ 3:24

    Unfortunately, this command creates completely wrong MD5 hashes under certain circumstances, for example if the string to be hashed contains both uppercase and lowercase letters or symbols.

    Let’s take the string “8FdBd35E” as an example. Its correct MD5 hash is
    2a6b2d4c5fbd87fd896177a8fd12d4d1

    and PHP indeed creates the correct MD5 hash:
    root@corp6 [~]# php -r "echo md5('8FdBd35E');" | xargs echo
    2a6b2d4c5fbd87fd896177a8fd12d4d1

    However, the “md5sum” shell command creates a completely different MD5 hash:

    root@corp6 [~]# echo "8FdBd35E" | md5sum
    bf84cb9630c7748d0f337c98c2644051 –

    Also, checked it with thousands other strings and md5sum creates an incorrect MD5 hash, so the command is not to be trusted.

    • Thibault Jun 2, 2017 @ 6:54

      again, the add -n to remove the newline

      echo -n "8FdBd35E" | md5sum => 2a6b2d4c => 2a6b2d4c5fbd87fd896177a8fd12d4d1
      php -r "echo md5('8FdBd35E');" | xargs echo => 2a6b2d4c5fbd87fd896177a8fd12d4d1

  • Erich Oct 21, 2016 @ 10:03

    That’s because ‘echo’ appends a newline. I tried your command with the ‘-n’ option and it worked as expected.

  • Pedro Dec 19, 2016 @ 23:28

    username=user
    password=passwd

    hash_user="$(echo -n "$username" | md5sum )"
    hash_pass="$(echo -n "$password" | md5sum )"

    as me funciono gracias a tu post.
    hash_username=$(echo $hash_user | sed 's/ //g' | sed 's/-//g')
    hash_username=$(echo $hash_pass | sed 's/ //g' | sed 's/-//g')

  • Katakis Nov 6, 2020 @ 16:15

    WARNING! Using double quotes “…” in
    echo -n "Your-String-Here" | md5sum
    is a potential source of error.
    If you have a dollar sign $ in your password, it will be interpreted as a variable, and your resulting MD5 hash will be different from what you expected.

    Use single quotes ‘…’
    echo -n 'Your-String-Here' | md5sum
    instead.

    @Webmaster: Maybe you could change the several occurrences of the password enclosure in this article from double quotes to single quotes, and then delete my comment. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.