- /etc/shadow – HowTo: Generate Password Hash in Linux
- Generate Password Hash for /etc/shadow
- 8 Ways to Generate a Random Password on Linux Shell
- Generation of a password with OpenSSL
- Generation of a password using urandom
- Generation of a password using pwgen
- Generation of a password using gpg
- Generation of a password using SHA
- Generation of a password via an easy way
- Generation of a password using apg
- Generation of a password using makepasswd
- Conclusion
/etc/shadow – HowTo: Generate Password Hash in Linux
Linux stores users’ encrypted passwords, as well as other security information, such as account or password expiration values, in the /etc/shadow file.
Someday you may need to edit the /etc/shadow file manually to set or change ones password.
Unlike the /etc/passwd that is readable for everyone, the /etc/shadow file MUST be readable by the ROOT user only.
For this you would have to generate password hash in the format compatible with /etc/shadow .
Cool Tip: Want to create a USER with ROOT privileges? This can be very dangerous! But if you insist… Read more →
There is no need to install any additional tools as it can be easily done from the Linux command line using Python.
Generate Password Hash for /etc/shadow
The $ID indicates the type of encryption, the $SALT is a random (up to 16 characters) string and $ENCRYPTED is a password’s hash.
Hash Type | ID | Hash Length |
---|---|---|
MD5 | $1 | 22 characters |
SHA-256 | $5 | 43 characters |
SHA-512 | $6 | 86 characters |
Cool Tip: Got a hash but don’t know what type is it? Find out how to easily identify different hash types! Read more →
Use the below commands from the Linux shell to generate hashed password for /etc/shadow with the random salt.
Generate MD5 password hash:
python -c "import random,string,crypt; randomsalt = ''.join(random.sample(string.ascii_letters,8)); print crypt.crypt('MySecretPassword', '\$1\$%s\$' % randomsalt)" --- $1$YjOzcqrf$Zqx4sx5CQRuEIFCdOLAJV0
Generate SHA-256 password hash:
python -c "import random,string,crypt; randomsalt = ''.join(random.sample(string.ascii_letters,8)); print crypt.crypt('MySecretPassword', '\$5\$%s\$' % randomsalt)" --- $5$LgsPuaeR$OCtm.3tpbS/wyOZAIy6dsVNP4x0GyohyGebkIz15e88
Generate SHA-512 password hash:
python -c "import random,string,crypt; randomsalt = ''.join(random.sample(string.ascii_letters,8)); print crypt.crypt('MySecretPassword', '\$6\$%s\$' % randomsalt)" --- $6$HMpFTkgb$WqzuqMqYbjWsXFrOtvZPo.1gIkH6HiXJGr4QPv.k26jE.3mE.sdf3dds[. ]
Hope these commands will be helpful.
Just don’t forget to replace MySecretPassword with YourSecretPassword.
As you can see, it is really very easy to generate hashes for the /etc/shadow from the Linux command line using Python.
Particularly for the reason that the Python is installed by default on the most Linux distributions.
8 Ways to Generate a Random Password on Linux Shell
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.
Step 2: Now click on the Terminal and wait for the terminal to open.
Step 3: Once the terminal is opened, you will have a screen like this:
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:
The outcome will be a strong password of 14 characters as shown below.
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.
The output of the above command is as follows:
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.
Enter the credentials as asked
The installation will eventually begin. The command line will look just like the screen below.
Once the installation has been done, we will generate a random string having 14 characters in it.
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.
The outcome of this command is
And in the end, we have a password generated which is.
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.
Here is the outcome containing the 32 characters yet a strong password to keep the system safe.
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:
The outcome of the above command is a string as shown below.
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.
The output has many random 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.
Enter the required credentials.
On writing the credentials, the installation will begin.
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.
The outcome is shown below. We have a total of 4 passwords with 8 characters in each of them.
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.
Conclusion
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.