Background
As an experiment, I was adding new users (as the root user) by manually creating entries into
/etc/passwd
. To hash the passwords I was usingopenssl
, in particularOpenSSL 1.1.1g
but the result should be the same across versions.I noticed each time I hashed the string "
testing
" I would get a different result.
# openssl passwd testing YY9E0oGqqamCM # openssl passwd testing csL9dpD2Iy3H2
I then added both hashes to the
/etc/passwd
file like so:
# echo "root2:YY9E0oGqqamCM:0:0:root:/root:/bin/bash" >> /etc/passwd # echo "root3:csL9dpD2Iy3H2:0:0:root:/root:/bin/bash" >> /etc/passwd
Finally, I switched to each user successfully, despite their hashes being different:
$ whoami reguser
$ su root2 Password: testing
# whoami root # exit
$ whoami reguser
$ su root3 Password: testing
# whoami root
Question
- How is it that I am able to use the same password (i.e.
testing
) even though the actual hashes within the/etc/passwd
file are different?- It would be great if someone could explain what is going on behind the scenes that allows this to work.
- I assume that the string "testing" is being given a different salt each time it is hashed, but how does linux know what that corresponding salt is when my password is entered to switch users?
- It would be great if someone could explain what is going on behind the scenes that allows this to work.