How to Generate Random Passwords and Keys in the Terminal?
As with any development-related lethargy, I always find figuring out a strong password or a secret key as an arduous task that requires more than the internal will and motivation to go visit random.org.
Nevertheless, for development environments, you really don’t need to venture into true randomness. Along the way, I’ve picked up some short utilities and commands to generate a pseudo-random string right within your Linux terminal (without any external packages).
Automatic Password Generator (APG)
If you are on the latest Ubuntu (at the time of updating this article, it’s Ubuntu 20), you might already have this package installed in your system. Fire up a terminal and type
apg to get your random passwords.
This one actually spits out special characters in the mix and the command is just
apg, easiest to remember when you compare to the ones below. If you don’t have it, use
sudo apt install apg.
GNU Privacy Guard (GPG)
GPG comes in handy if you don’t have APG around. Just for the sake of ambiguity, GPG and APG have nothing in common. GPG is a cryptographic utility software.
gpg --gen-random --armor 1 18.
This might be a bit hard to keep at the top of your head, but after exactly thirty-seven random string generations in your terminal, you will finally be able to recite this in sleep.
Just hash something
Yeah. Just hash it. Think of a random word, animal, or that one weird game character you still have a crush on and pipe it to a hashing algorithm like
sha256. Or, just hash the date.
This won’t generate any special characters, but the checksum is good enough to generate dev keys.
Get random with OpenSSL
If you are in a hurry for an alphanumeric key, use
openssl rand -base64 16. Do you really need a screenshot for this? After three images on how random strings would look like?
Mash the keyboard
The ultimate random string generator. Mash the keys! Mash your forehead on the keyboard! Or … you could just type out some random special characters in the string.
cURL random.org for passwords
If you are still reluctant on mashing the keyboard or switching to the browser tab. You can
curl this random.org URL
curl "https://www.random.org/strings/?num=10&len=8&digits=on&upperalpha=on&loweralpha=on&unique=on&format=plain&rnd=new" will respond with random passwords in your terminal. Modify the parameters as per your requirement. Don’t worry about forgetting the URL as you can usually retrieve it from the terminal history. Oh, and you can check out your weather from your terminal too.
If you know any other ways to generate random strings in your terminal, let me know in the comments.
This post was first published on January 15, 2013.