How to Generate Random Passwords and Keys in the Terminal?

2

Random password generation for Linux.

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.

Screenshot of random password generated in terminal by Automatic password generator in Ubuntu.

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.

Use gpg --gen-random --armor 1 18.

Screenshot of random keys generated in terminal by using GPG in Ubuntu.

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 md5sum or sha256. Or, just hash the date.

Screenshot of random hash generation in Linux command line, using MD5 and SHA256.

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 https://www.random.org/strings/?num=10&len=8&digits=on&upperalpha=on&loweralpha=on&unique=on&format=plain&rnd=new

Screenshot of Linux terminal with random passwords generated from random.org using cURL.

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.

Avatar

Karthikeyan KC

Aeronautical engineer, dev, science fiction author, gamer, and an explorer. I am the creator of Geekswipe. I love writing about physics, aerospace, astronomy, and python. I created Swyde. Currently working on Arclind Mindspace.

Leave a Reply

Your email address will not be published. Required fields are marked *

2 Responses

  1. Very nice list. Thank you. In case you did not know WordPress has an API endpoint for wp-config secret keys. You can get them with curl. curl "https://api.wordpress.org/secret-key/1.1/salt/"

Related