Generate passwords from terminal

I often need a random password for yet another account on some website I will probably visit only once. In the past I used different online and/or offline tools or browser plugins to generate these random passwords but that required me to launch other applications and selecting various options over and over again to fill in my needs. Then I discovered an easy way to do exactly that through Terminal and immediately copy it to the clipboard as well. This has saved me a lot of time so you should add this line to your dotfiles:

# Random 14 character password generator using base64

alias passgen='openssl rand -base64 14 | pbcopy'

You can replace the number '14' in there by any number of characters you want the password to contain. The password will contain numbers, special characters and both uppercased and lowercased characters so when using this, you'll have a strong and pretty secure password.

Comments (6)


Personally, I prefer something like "cat /dev/urandom | tr -cd [:alnum:] | head -c 14" which doesn't require OpenSSL.



julesj wrote

Your alias caused a "unable to write 'random state'" error for some reason. I changed it to the following to ignore those errors:

alias passgen='openssl rand -base64 14 2>/dev/null | pbcopy'


Gerben wrote

Just as a note; the only special characters I get are / and + and the = that is always at the end.

Also the 14 is before converting it to base64, so it will result in 19 characters plus an = for padding.

Real cool, though not the most strong password you can get.


Passwords were never a problem for me. I am one of those guys, who goes criyptc with passwords.I use combination of words, and letter substitution and my own defined set of mutation functions to get the password. :)


I am having the same issue as julesj and his change in code also isn't helping me out. :-/


joren wrote

on mac: brew install pwgen

then you can just type 'pwgen' in your terminal to get a bunch of random password. You can add a couple of parameters:

'pwgen 10 5' will generate 5 random passwords each 10 characters long.

But you snippet is handy because of the pbcopy

Leave a comment