A major advantage of Linux is that you can implement the same thing in hundreds of ways. For example, you can generate a random password in dozens of ways. This article describes ten methods to generate a random password.
These methods are collected in Command-Line Fu and tested on our own Linux PC. Some of these 10 methods can also be run in Windows with Cygwin installed, especially the last method.
Generate a random password
For any of the following methods, you can generate a password of a specific length through simple modification, or use only the first N digits of the output result. I hope you are using a password manager similar to LastPass, so that you do not have to remember these randomly generated passwords.
1. This method uses the SHA algorithm to encrypt the date and output the first 32 characters of the result:
date +%s | sha256sum | base64 | head -c 32 ; echo
2. This method uses the embedded/dev/urandom and filters out characters that are not commonly used. Here, only the first 32 characters of the output result are returned:
< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32};echo;
3. This method uses the random function of openssl. If your system may not have openssl installed, you can try nine other methods or install openssl by yourself.
openssl rand -base64 32
4. This method is similar to the previous urandom, but it works in reverse order. Bash is very powerful!
tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1
5. This method uses the string command, which outputs printable strings from a file:
strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d 'n'; echo
6. This is a simpler version of urandom:
< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6
7. This method uses a very useful dd command:
dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev
8. You can even generate a password that can be entered with only the left hand:
9. If you use the above method every time, it is better to save it as a function. If this is done, you can use randpw to generate a random password at any time after the command is run for the first time. Maybe you can save it to your ~ /. Bashrc file.
randpw(){ < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;}
10. The last method to generate a random password is the simplest. It can also run under Windows with Cygwin installed. It may also run in Mac OS X. I am sure someone will complain that the password generated in this method is not random by other methods. But in fact, if you use all the strings it generates as the password, the password is random enough.
date | md5sum
Yes, this method is extremely memorable.
Original address: http://www.howtogeek.com/howto/30184/10-ways-to-generate-a-random-password-from-the-command-line/