How can I generate a new random wpa/wap2 key in the linux terminal? I tried pwgen but this is not generating hexadecimal values.

  • For WPA(2) I would use all regular characters to generate from rather than just hexadecimal ones. What is your reason to generate a password from such a selected set?
    – gertvdijk
    Jan 14 '13 at 13:39
  • @gertvdijk this is required by the router Jan 14 '13 at 14:22

For instance, if you want a password with the maximum character length, i.e. 63, you can enter either of the following 2 commands:

  1. makepasswd --chars=63

  2. openssl rand -base64 63


Here is a better command I've found for this purpose since I first wrote this answer:

dd if=/dev/urandom bs=48 count=1 status=none | base64 | tr +/ -_

Actually I use this zenity-powered script to generate such a password from time to time:

RNDPWD="$(dd if=/dev/urandom bs=48 count=1 status=none | base64 | tr +/ -_)"
echo $RNDPWD | tr -d '\n' | xclip -i -selection clipboard
zenity --question --title="Random Password" --text="<i>Password Copied to Clipboard:</i>\n\n$RNDPWD\n\n<b>Would you like to generate another one?</b>" --width=500 --height=150
if [ $? = 0 ]
  • thanks for your answer, but I need to generate a 64 characters long, hexadecimal password, which is required by my router. Jan 14 '13 at 14:38
  • then you need openssl rand -hex 32
    – Jean-Marie
    Jan 15 '13 at 14:58
  • 1
    @Sadi: "openssl rand -base64 63" generated 63 bytes of information, 84 base64 characters.
    – beroal
    Nov 6 '16 at 19:17
  • @beroal thanks for the feedback. I don't know what happened in nearly 4 years, but I've added another command - even a script - now which I hope will serve this purpose much reliably and much better ( and much longer ;-)
    – Sadi
    Nov 8 '16 at 8:54
  • Thanks. And why do you run "tr +/ -_"?
    – beroal
    Nov 23 '16 at 11:53

To generate the best possible password (with the highest entropy possible), one should use all of the ASCII printable characters - from 32 (space) to 126 (tilde, ~). This can be archieved with (for an example maximum password length of 63 characters):

< /dev/urandom tr -cd "[:print:]" | head -c 63; echo

To not include the space one can use [:graph:] character set and there are also others described in the tr's manpage.

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.