Linux / Unix: Generate A MD5 String

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
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Time5m
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.

ADVERTISEMENTS

Create a md5 string using md5sum command

Use the following syntax:

echo -n "Your-String-Here" | md5sum

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"
Recommended readings
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 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 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
11 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')

Leave a Reply

Your email address will not be published.

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