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
LWj}E0kMgl3f
[email protected] [~]#
 [email protected] [~]# mkpasswd -s 3 -l 20
"PcziLb9EvFrm>4tb)pQ
[email protected] [~]#

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

or

[email protected] [~]# openssl rand 12 -base64
MDn28PGbwU0zmDxG
[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=http://fedoraproject.org/wiki/EPEL]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
PfCdv

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
MmpIo4KpORFckt7tS2AXpOnmU V1p1SVhaTyOLLtvWZPDOfeABZ

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
Y8BAm>Pv1}\9O(kk|SI?

That’s it!

Bookmark the permalink.

Leave a Reply