brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: openssl passwd -6 makes a SHA-512 password hash for Linux

Openssl
~/tips/tip-openssl-passwd-sha512

openssl passwd -6

Generate a crypt style password hash for cloud-init, Ansible or a user account.

Cloud-init, Ansible and usermod -p want a password hash, not the password itself. openssl passwd -6 makes the same SHA-512 crypt format that /etc/shadow uses.

Run it without the password on the command line, so it prompts you:

openssl passwd -6

The result looks like $6$salt$long-hash. The $6$ marks SHA-512 crypt, followed by a random salt and the hash. Every run uses a new salt, so the same password gives a different string each time, and both work.

In a script, read the password from standard input rather than an argument:

printf '%s\n' "$PW" | openssl passwd -6 -stdin

Then use it, for example with usermod -p '$6$...' alice, or in the passwd field of cloud-init. Keep the single quotes, since the dollar signs would otherwise be expanded by your shell.

-5 gives SHA-256 crypt. Avoid -1, which is MD5 crypt, and note that OpenSSL 3 also falls back to MD5 when you give no flag at all. Recent Debian and Fedora releases default to yescrypt ($y$) in /etc/shadow, which OpenSSL cannot make, but they still accept $6$ hashes. I would still treat a hash like a secret: it can be attacked offline if it leaks.

Further reading: OpenSSL: openssl-passwd, crypt(5) man page

More Openssl tips openssl s_client -connect 203.0.113.10:443 -servername example.com </dev/null | openssl x509 -noout -subject openssl x509 -in cert.pem -days 365 -signkey key.pem -out cert-new.pem printf %s hello | openssl dgst -sha256 -hmac secret openssl pkcs12 -legacy -in old.pfx -nokeys echo 'aGVsbG8gd29ybGQ=' | openssl base64 -d All Openssl tips (34) All topics

Link