How to encrypt user password

Use encrypted passwords to automatically setup users using configuration management tools like Puppet or Ansible.

Install whois package.

$ sudo apt-get install whois

Encrypt password using DES algorithm with random salt.
It uses only the first 8 characters of the password.

$ printf "badpassword" | mkpasswd --stdin --method=des
Nw8.4ABfIKZKM

Encrypt password using DES algorithm with defined salt.
It uses only the first 8 characters of the password.

$ printf "badpassword" | mkpasswd --stdin --method=des --salt="AA"
AAEZq.zBgmGCM

Encrypt password using MD5 algorithm with random salt.

$ printf "badpassword" | mkpasswd --stdin --method=md5
$1$RcMgFRVJ$Pq3G.LQ0iwuP5M2dWNLzo1

Encrypt password using MD5 algorithm with defined salt.

$ printf "badpassword" | mkpasswd --stdin --method=md5 --salt="UeUn9t3A"
$1$UeUn9t3A$oEb0prxivWfmh41.YpLJu.

Encrypt password using SHA256 algorithm with random salt.

$ printf "badpassword" | mkpasswd --stdin --method=sha-256
$5$2inx/2oOXZ/hMl$3oTqVmBtiFJ/2je9MM.JplWfLjnf.RaVeg9e7eQbCKC

Encrypt password using SHA256 algorithm with defined salt.

$ printf "badpassword" | mkpasswd --stdin --method=sha-256 --salt="U7ReiUGcnY9yt3A1"
$5$U7ReiUGcnY9yt3A1$3Bfm1gV9RMLMjZs2ATRX3DtxKCYOdN3ykVSKWEpQwE5

Encrypt password using SHA512 algorithm with random salt.

$ printf "badpassword" | mkpasswd --stdin --method=sha-512
$6$YfQbudiFYdPl$8Byyl7mpwmDJRTChzRa2cCXqFKbZ23bgUuoGqgqFTAMsdJkiqreLUwiRaVOmIjpM5X0qUP83W9m1xq9EwALKZ.

Encrypt password using SHA512 algorithm with defined salt.

$ printf "badpassword" | mkpasswd --stdin --method=sha-512 --salt="g3RYi6b0nk9y43Rl"
$6$g3RYi6b0nk9y43Rl$k89OM4kjLkqGcbJi/G2y8AO3CooJQgH5j2M8azymZzliVIaELbhixDPSSf.OlepYN6TVJxKgDLX4dsJtNdJ7R/

Read how to determine encryption algorithm used to store password for more information.

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.