I want to set a user's password as MD5 hash value in Centos. I mean I don't know the password, but I have the hash MD5 value (md5sum). Is it possible?

Detailed explanation: New password I want to set: 123 MD5 value of '123' is: dak37yd2o9d8m2ype9n8283up1m2 I want to run a command like this:

echo dak37yd2o9d8m2ype9n8283up1m2 | passwd --stdin -md5 myuser

So myuser can login with password 123

  • The hash you have given is not a valid md5 hash. MD5 outputs 128 bits, or 32 hexadecimal chars. You have supplied 29. The hash of the string 123 is 202cb962ac59075b964b07152d234b70. – Cyclic3 Oct 2 '18 at 9:53
  • That was just a random value to explain my question, but thanks anyway :) – Hamid Reza Oct 3 '18 at 17:34
  • ah, I didn't realise – Cyclic3 Oct 3 '18 at 18:53

A word of warning: As I am sure you have heard many a time, md5 is a broken hash function. Storing passwords hashed with it is only slightly better than plaintext.

After much researching, including trying things out on my own system, I was unable to find a way of using an existing hash that has not been salted. If you still have access to the password, or access to someone who knows it, you can use chpasswd -e to generate a salted hash.

If you have a salted hash, add the user to your system normally, if you haven't done so already. Pick any password you like, as we will overwrite it later. Open the /etc/shadow file, and edit the line starting with the username of your user. Replace the second field (after the first colon, and before the second) with this:


Where $salt$ is your salt, and $hash$ is the md5 hash in crypt base-64 format (https://www.linuxquestions.org/questions/linux-security-4/how-can-i-convert-a-sha-512-etc-shadow-hash-to-base64-4175477045/).

If the hash is unsalted, and you cannot gain access to the password through normal means, using something like hashcat to break it for you could be a viable option.

  • 1
    You can use chpasswd -e to set a password hash without editing /etc/shadow yourself manually. Purely looking at man 3 crypt, empty salt should be allowed (0 is "up to 16"), so $1$$hash might work. If it doesn't, filing a bug against glibc will likely lead to a documentation clarification that salt can't be empty. – Ulrich Schwarz Sep 30 '18 at 11:03
  • @UlrichSchwarz I shall update my answer accordingly. I did try with an empty salt, but it gave me a different value that sha1sum, and I could not use it to log in. – Cyclic3 Sep 30 '18 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.