How to generate random password on Linux

  • Blog
  • 2023.08.03
  • 0 views

Being able to generate a 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.

generate random password

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

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