Often system administrators need to generate random strong passwords for their users’ authentication credentials, or for other purposes. In this article, we will learn about a few commands you can use to generate random password in Linux. We will also learn how to create encrypted salt-based passwords in Linux.
How to Generate Random Password in Linux
There are plenty of random string generator commands in Linux. We will look at some of them.
1. Pwgen
Pwgen is one of the simplest tools to generate random password in Linux. You can easily install it with the following command.
# Ubuntu/Debian $ sudo apt-get install pwgen # RHEL/CentOS/SUSE/Fedora $ yum install pwgen
Once it is installed, you can generate a random password of specific length , using the following command.
$ pwgen password_length number_of_passwords
In the above command, the first argument is the password length while the second argument is the number of passwords.
Here is the command to generate a random password of length 10 characters using pwgen.
$ pwgen 10 1 hmehierw48 $ pwgen 10 1 8cyr847c48
As you can see above, pwgen generates a new random password every time you run it.
If you omit the last argument, pwgen will generate a set of random passwords. Here is a command to generate multiple random passwords of length 10.
$ pwgen 10 erheuhieh9 94h849hcrc cr49ch94hc xr4ucm40c0 rj40c4r0mr ...
2. Using makepasswd
Similarly, you can also use makepasswd command to generate random passwords of specific length. You can install it with the following command.
# Ubuntu/Debian $ sudo apt-get install makepasswd # RHEL/CentOS/Fedora/SUSE $ yum install makepasswd
Once it is installed, you can generate random passwords using the makepasswd command
$ makepasswd erh9r8404r
By default, makepasswd generates a random password of length 10.
If you want to generate password of another specific length, you can use the –char option. Here is the command to generate password of length 20.
$ makepasswd --char 20
If you need to generate more than 1 password at a time, you need to specify the number of password to be generated using –count option. Here is the command to generate 5 random passwords of length 10 each.
$ makepassword --char 10 --count 5
3. Using mkpasswd
Mkpasswd is a handy command to encrypt existing passwords using salt strings. Salt is a basically an additional string used by program to generate encrypted password, to prevent outsiders from simply brute forcing their way to get your password, using the same tool that you use to generate passwords.
If you want to encrypt passwords based on specific string, you can use mkpasswd tool for that purpose.
# Ubuntu/Debian $ sudo apt-get install mkpasswd # RHEL/CentOS/SUSE/Fedora $ yum install mkpasswd
Here is a simple command to encrypt password ‘linux’ using mkpasswd command.
$ mkpasswd linux 94rh498h45 $ mkpasswd linux e9c48h849c
In the above examples, mkpasswd uses random salt string every time. If you want to specify salt string you can do so with -s option. Here is an example to use tt as salt string
$ mkpasswd linux -s tt tt09yrnc498n $ mkpasswd linux -s tt tt09yrnc498n
As you can see above, in this case, mkpasswd generates the same encrypted password every time, for the same input, because we have specified the salt string.
In this article, we have learnt several different tools to generate random passwords in Linux. You can run these commands on terminal, or even add them to shell script to automate password generation.
Also read:
How to Use Wget to Download File Via Proxy
How to Get Hostname/Domain Name from IP Address
How to Check if String Matches Regular Expression in Python
How to Modify XML File in Python
Plot Graph from CSV Data in Python
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.