Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | md5sum command |
Time | 5m |
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.
🐧 14 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Reading from standard input:
$ md5sum -
a little script:
$ ./md5 hallo
hallo
aee97cb3ad288ef0add6c6b5b5fae48a –
The “-” declares, that this message comes from standard input
greets
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).
$ ./md5 hallo
hallo
598d4c200461b81522a3328565c25f7c –
Not “aee97cb3ad288ef0add6c6b5b5fae48a” as shown above.
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 -
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.
You’re an idiot mate
Thanks, i’ll write this on my tombstone!
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.
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.
again, the add -n to remove the newline
echo -n "8FdBd35E" | md5sum => 2a6b2d4c => 2a6b2d4c5fbd87fd896177a8fd12d4d1
php -r "echo md5('8FdBd35E');" | xargs echo => 2a6b2d4c5fbd87fd896177a8fd12d4d1
That’s because ‘echo’ appends a newline. I tried your command with the ‘-n’ option and it worked as expected.
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')
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.
The page has been updated. Thanks!
I find md5sum as an interesting idea for making passwords on the command line
$ echo "someword" | md5sum
$ echo "someword-$(date)-$(hostname)" | md5sum
Do you think it is a good idea? the password looks good to me.