8 Ways to Generate a Random Password on Linux Shell

Linux random password generator

A strong password is very important when you authenticate to a service with a username and password. Sometimes you need to protect your account or server and try to keep your data safe. It is often said that a strong password must have at least 14 characters, with variations such as lower and upper case letters and alphabets. Most of the time, a long password is considered much more secure than a short one because it is hard to get. In this tutorial, we will learn many ways to create a secure password using the Linux command line. We will look at many different ways you can create a stronger and more secure password using the Linux command line. If you want to create a strong password using the command line, there are several methods and utilities available to you. We will discuss many of these methods and you can choose one of the methods to create a password depending on your needs.

Generation of a password with OpenSSL

Several methods, exist in Linux to create and generate the passwords for the Linux command line. The first one we are trying is by using OpenSSL. Following are the steps that need to be followed for this purpose.

Step 1: First of all, open Terminal by clicking on the Ubuntu launcher and searching for Terminal.

Search Linux Terminal application

Step 2: Now click on the Terminal and wait for the terminal to open.

Open the Terminal

Step 3: Once the terminal is opened, you will have a screen like this:

Linux Terminal ready for use

Step 4:

The command that is used to generate a stronger password includes the OpenSSL rand function. This will help us generate 14 random characters in a string. The command is:

openssl rand -base64 14

Create a random password with OpenSSL

The outcome will be a strong password of 14 characters as shown below.

Password created with OpenSSL

Generation of a password using urandom

The second command which we are using to generate a password has filtered /dev/urandom output with tr. This will allow us to delete all of the unwanted characters and then help us to print the first 14 characters only.

Create a password with urandom device

The output of the above command is as follows:

urandom password

The output has allowed us to print the first 14 characters only by deleting all of the unwanted ones.

Generation of a password using pwgen

For using pwgen we need to install this tool first. This tool will help us generate some random yet meaningless passwords. Although the passwords generated by it are random still are pronounceable. So, we will now install the tool using the following command.

install pwgen

Enter the credentials as asked

Enter password

The installation will eventually begin. The command line will look just like the screen below.

pwgen gets installed

Once the installation has been done, we will generate a random string having 14 characters in it.

Use pwgen to create a password

The string we got randomly is this one.


Generation of a password using gpg

Gpg can also be used to generate a strong password of 14 characters. The gpg tool uses the following command to generate the 14 characters.

Using gpg to create a random password

The outcome of this command is

gpg result

And in the end, we have a password generated which is.

The password

Generation of a password using SHA

We will use SHA in this method, we can also hash the date. It runs through the base64. As a result, we get an output as the top of the 32 characters.

Use sha to create a password

Here is the outcome containing the 32 characters yet a strong password to keep the system safe.

The resulting password

Generation of a password via an easy way

The generation of a password is quite easy using the command line. Although it is not random still it is useful if we utilize the whole password. The easiest command for this purpose is as follows:

password string created with md5sum

The outcome of the above command is a string as shown below.

The md5sum password

And this command is very easy, and simple enough to remember.

Generation of a password using apg

APG is an Automatic Password Generator, it is already installed on all Ubuntu systems. It is also an easily used utility. It helps us generate various passwords that are random input.

In order to generate entire random passwords, we can run apg -a 1, which will give us the passwords with 8-10 random characters. The command we used for this purpose is apg –a 1. We will run and see various passwords generated in the output.

random password from apg

The output has many random passwords.

List of created passwords

Generation of a password using makepasswd

makepasswd” can also be used to generate passwords. For using this utility, we install it at first. We will use the command sudo apt-get install makepasswd.

Install makepasswd

Enter the required credentials.

Enter admin password

On writing the credentials, the installation will begin.

Installation of makepasswd

Press Y to continue

Once the utility has been installed, we will write the command which is makepasswd –count NumberOfPasswords –minchars MinLengthOfPassword.

At first, we tell how many passwords we are aiming to generate and what must be the length of each of the password. Here we have asked to generate 4 passwords with length as eight for each of it.

Using makepasswd

The outcome is shown below. We have a total of 4 passwords with 8 characters in each of them.

List of created passwords

The command is responsible for generating a list of random passwords based on the number of passwords we want to generate and the length is also defined by us.


In this tutorial, we have discussed many ways to generate passwords from the Linux command line. There are many methods that already exist. Some of the utilities are already present in the Linux command line, while the others are easy to install with the available commands. In this tutorial, we have discussed the utilities openssh, urandom, pwegn, gpg, sha, date, apg and makepasswd. All of these commands guarantee the generation of a stronger password from the Linux command line and are useful for users to ensure the security of their systems.