Password Generator in Linux – Generate Random Passwords in Command Line

There are many times when we face the problem of creating a strong password. Unfortunately, humans are not good at fortuitousness, and creating a random password is one of the most important factors in keeping in mind the security and privacy of data nowadays.

In this article, we’ll cover how to create a script that generates a truly random password. The character length can be customized to meet all expectations.

How to achieve (pseudo)randomness

In every Unix device, there are a number of files that are used as Pseudorandom Number Generators. One of the files in Linux is /dev/urandom. You can go ahead and run the cat command on the file and see some binary output created on the terminal.

cat /dev/urandom
Binary output from the cat command on the /dev/urandom file
Binary output from the cat command on the /dev/urandom file

Creating random passwords with only alphanumeric characters

You might be seeing some random characters in your terminal window. We have to first filter this noise and allow only alphanumeric characters. We do that by using the strings command. We will also use the grep command in conjunction so that we can allow only certain characters in the password. Here we should mention one important distinction, we cannot use the standard command, we have to use the -a flag so that it grabs the text input.

cat /dev/urandom | strings |grep -Eoa "[a-zA-Z0-9]*"
Filtering the output so that it returns only specified characters
Filtering the output so that it returns only specified characters

If you run the above command you will see that only alphanumeric characters make it through to the terminal screen. So our filtering is working as expected.

If you also want special characters such as @, $, %, & etc to appear in your password, then simply add in those characters in the regex like so:

cat /dev/urandom | strings | grep -Eoa "[a-zA-Z0-9@$%&_]*"

Setting the length of the password

Now we will set the length of the password, using the head command, which will stop the output once the required number has been reached. So the command now looks like this:

cat /dev/urandom | strings | grep -Eoa "[a-zA-Z0-9@$%&_]*" | head -n 10 

This only cuts the text to 10 lines rather than 10 characters, however, it is still of use, as we know with certainty that one line will contain at least one character(most often they contain more than one), so by running this command we are assuring that no matter what the password will be at least 10 characters long.

Deleting the new-line characters

Now we need to delete the new-line characters so that our password is in one line, we do that using the tr command:

cat /dev/urandom | strings | grep -Eoa "[a-zA-Z0-9@$%&_]*" | head -n 10 | tr -d '\n'

At this point we will also run the cut command to cut the password to the final length of 10 characters:

cat /dev/urandom | strings | grep -Eoa "[a-zA-Z0-9@$%&_]*" | head -n 10 | tr -d '\n'| cut -c1-10
Our bash one-liner to generate (pseudo)random passwords
Our bash one-liner to generate (pseudo)random passwords

Conclusion

Congratulations you have your own one-liner for a password generator that is completely customizable. You can use whatever set of characters you want in your password and do not have to depend upon human guesswork to create a safe and secure password. Hope you guys liked this article and thanks for reading it!