linux,

Generate SHA512 password hash with a salt from the CLI

Apr 26, 2022 · 1 min read · Post a comment

Personally, the only time I’ve been generating a SHA512 password hashes was when I was working with ansible.builtin.user module, simply because I needed to commit a password hash for a new Linux user. Although I wouldn’t consider it as an optimal solution (not even close), let’s see how to create one.

Prerequisites

  • Shell environment
  • sudo privileges

Solutions

Solution 1. Use mkpasswd

Step 1. Install mkpasswd.

sudo apt install whois

Step 2. Generate SHA512 password hash with a salt.

mkpasswd --method sha-512 <SOME_PASSWORD_HERE>

Note(s):

  • If you don’t add parameter -S, --salt=<SOME_STRING_HERE>, it will use a random salt value.
  • If you want to keep it OS agnostic (since mkpasswd it’s not available to macOS and Windows, although you could install it via gem or npm), use Docker. For instance:
    docker run -it --rm alpine mkpasswd --method=sha-512 <SOME_PASSWORD_HERE>
    

Solution 2. Use openssl

Step 1. Open Terminal and run the following command:

openssl passwd -6 -salt <SOME_SALT_HERE>

Note. The SALT string could be any random string, just make sure not to be a short one.

Conclusion

If you can think of any other alternative solution, please do let me know in the comment section below. On a side note, follow our official channel on Telegram.