How to generate random password on Linux

Try it in our public cloud & Get $50 Credit
CLAIM NOW

Being able to generate a random password is an easy to do process on all Linux systems that allow users to enhance their security in a very simple way. Stronger than the average family member name, pet name, or hobby password, randomly generated passwords also prove themselves handy when needing to create multiple new passwords and are extremely easily customizable. Such passwords can include special characters and be any length you need, and multiple passwords can even be generated at once for system administration convenience.

This guide will show how you can generate a random password for yourself on any Linux/Unix system.

Getting Started

In order to complete this tutorial, you need the following:
• 1 Server (Cloud Server or Dedicated Server) running Linux.

Tutorial

We start off simple with a method that can be used on any Linux/Unix system in order to generate a random string. The below command uses /dev/urandom, which is a special file that uses the internal random number generator to produce pseudo-random bits. Note that this is different than the popular /dev/random that you likely know already, which is a true random number generator unlike the pseudo-randomness of /dev/urandom. We can use /dev/urandom to generate a password that is eight characters long and is made up of random uppercase and lowercase letters and numbers (you may need to use sudo):

head /dev/urandom | tr -dc A-Za-z0-9 | head -c 8

The above command, broken down, uses the head to get the first few lines of the dev/urandom file, then pipes the output to the command tr. This command translates the input string into a new one based on the options given to it. We used the options -dc to delete and use the complement of the given set with A-Za-z0-9 to get uppercase, lowercase, and numerical characters. The final pipe again uses head to cut the output into eight characters. To get a password that is longer than eight characters, simply change the value of -c 8 in the above command to your desired length, for example the following for a ten-character long password:

head /dev/urandom | tr -dc A-Za-z0-9 | head -c 10

Using pwgen

While /dev/urandom can be used anywhere and comes built-in, you can also use pwgen to generate a random password. pwgen is more than a random password generator however, it is a professional password generating tool that can create classical cryptographically-secure passwords, pattern-based passwords, list-based passwords, and more, all using the random pool technique to generate random data. It is open-source, free to use, and works on many systems. To install pwgen for yourself, execute one of the following commands for your respective system (you may need root privileges, use sudo if needed):

CentOS

yum install pwgen -y

Debian/Ubuntu

apt-get install pwgen -y

When using pwgen, the basic structure of the command is as following:

pwgen [ OPTIONS ] [ pw_length ] [ num_pw ]

To create a single random password that is ten characters (uppercase/lowercase letters and numbers), execute this command. The -s flag ensures that the password created is truly random, the value 10 states how many characters long the password should be, and the value 1 represents the number of passwords we want to output:

pwgen 10 -s 1

This command results in a password that looks like this:

RUFv7fxA3o

To make your passwords more secure, you can add special characters using the flag -y as part of the above command such as this:

pwgen 10 -sy 1

Adding the special character flag creates passwords like this instead of the above. Note the inclusion of special characters suchas # or +:

N+`I#ZDK4V

In the case that you want a password that is a different amount of characters, such as rather 20 (or any other number), you can modify the amount of characters output such as in this example to output a 20-character long random password:

pwgen 20 -s 1

The result will now be a single password that has 20 characters:

E502JHiybGi7dvsQGbWS

In the case that you want to output more than one password, simple change the value 1 to any other number, for example 3, to receive three generated passwords:

pwgen 10 -sy 3

The output respectively will change to show the three passwords:

LG]8*;9L.z BT3|NAEM5z 4i*GQ}CL`V

More information about the command-line options for pwgen can be found in its man page either online or accessible via:

man pwgen

Conclusion

Having completed this guide, you are now able to generate random passwords using both the tool pwgen as well as the built-in Linux/Unix file /dev/urandom. Enjoy playing around with the different command-line options to generate passwords that suit your needs, and share with your friends!