Generate a random password from the command line in Linux

Generate a random password from the command line in Linux

We’ll show you, How to generate a strong password from the command line in Linux. Having a strong password in Linux, is the most important thing you can do to protect your account or server and to keep your data secure. Common thinking is that a strong password should be comprised of at least 14 characters, including lowercase and uppercase alphabetic characters, numbers and symbols and should never be based on a dictionary word. Using a long password is much more secure that using a short one, the longer the password the harder it is to guess. In this post, we will take a look at a several different ways to generate a strong password using the Linux command line.

1. Generate a strong password with openssl

This method uses the openssl rand function and it will generate 14 characters random string:

openssl rand -base64 14

2. Generate a strong password with urandom

In this method we will filter the /dev/urandom output with tr to delete unwanted characters and print the first 14 characters:

< /dev/urandom tr -dc A-Za-z0-9 | head -c14; echo

3. Generate a strong password with pwgen

pwgenis a tool that generates random, meaningless but pronounceable passwords.
To install pwgen run:

sudo apt-get install pwgen

Once the installation is complete, use the following command to generate a random string of 14 characters:

pwgen 14 1

You can also use some of the following flags:

  -c or --capitalize
        Include at least one capital letter in the password
  -A or --no-capitalize
        Don't include capital letters in the password
  -n or --numerals
        Include at least one number in the password
  -0 or --no-numerals
        Don't include numbers in the password
  -y or --symbols
        Include at least one special symbol in the password
  -s or --secure
        Generate completely random passwords
  -B or --ambiguous
        Don't include ambiguous characters in the password
  -h or --help
        Print a help message
  -H or --sha1=path/to/file[#seed]
        Use sha1 hash of given file as a (not so) random generator
  -C
        Print the generated passwords in columns
  -1
        Don't print the generated passwords in columns
  -v or --no-vowels
        Do not use any vowels so as to avoid accidental nasty words

4. Generate a strong password with gpg

We can also use the gpg tool to generate a strong 14 characters password:

gpg --gen-random --armor 1 14

Of course, there are many other ways to generate a strong password. For example, you can add the following bash shell function to your ~/.bashrc file:

genpasswd() { 
    strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 14 | tr -d '\n'; echo
}

and when you need to generate a strong random password just type genpasswd


If you have any questions regarding the process of generating a strong password from the command line, feel free to comment below or sign up for our hosting services and contact our EPIC Support Team. They are available 24/7 and they will take care of your request immediately. You can always asks our support team to generate a random and strong password from the command line in Linux, for your.

PS. If you liked this post on how to generate a random password from the command line in Linux, please share it with your friends on the social networks using the sharing buttons or simply leave a reply below. Thanks.

1 thought on “Generate a random password from the command line in Linux”

  1. Hi there,

    just to share the information, i noticed that the solution 4 based on “gpg –gen-random –armor 1 14” often gives passwords ending by “=”.

    So i would’nt say it’s “fully random”. But anyway, it’s better than nothing or a fully manual process.

    Best Regards

    Reply

Leave a Comment