How to generate random passwords in Linux

Following are the methods to generate random passwords on command line interface in Linux.

[email protected] [~]# mkpasswd -l 15 -d 3 -D 3

usage: mkpasswd [args] [user]

where arguments are:

-l # (length of password, default = 15)i want = 12

-d # (min # of digits, default = 3) i want = 4

-c # (min # of lowercase chars, default = 2) i want = 4

-C # (min # of uppercase chars, default = 2) i want = 4

-s # (min # of special chars, default = 1) i don’t want

-v (verbose, show passwd interaction)

-p prog (program to set password, default = /bin/passwd)

[email protected] [~]# mkpasswd -l 12
[email protected] [~]#
 [email protected] [~]# mkpasswd -s 3 -l 20
[email protected] [~]#

mkpasswd -s 3 -l 20 that means the password will contain 3 special characters with 20 chars inside .


r[email protected] [~]# openssl rand 12 -base64
[email protected] [~]#

this generates a random alphanumeric string.

Using pwgen

[email protected] [~]# yum install pwgen

Official CentOS repository does not provide pwgen RPM. But it is available on EPEL repository. So you need to enable EPEL repo as per the instructions found on [url=]EPEL – FedoraProject[/url]. Then install pwgen using yum:

By default, pwgen generates easy to remember passwords with sufficient randomness. See example, below:

[email protected] [~]# pwgen 5 1

In the above command, pwgen creates one password of 5 characters length. The first argument to pwgen is length of password and second argument is number of passwords.

To generate complete random password, use the -s switch. Following command generates two random passwords of 25 characters length:

[email protected] [~]# pwgen -s 25 2

As you have noticed, by default, pwgen creates passwords containing letters and decimal digits. To include special characters in the passwords, use -y switch. Below command creates one password of 20 characters length and password includes special characters:

[email protected] [~]# pwgen -s -y 20 1

That’s it!

Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.