Files

97 lines
5.2 KiB
Markdown
Raw Permalink Normal View History

2025-12-29 13:38:39 +08:00
---
page-title: "10 Ways to Generate a Random Password from the Linux Command Line"
url: https://www.howtogeek.com/30184/10-ways-to-generate-a-random-password-from-the-command-line/
date: "2023-04-21 10:02:01"
---
![](https://www.howtogeek.com/wp-content/uploads/2014/12/img_5490eefa98262.png?width=1198&trim=1,1&bg-color=000&pad=1,1)
One of the great things about Linux is that you can do the same thing hundreds of different ways—even something as simple as generating a random password can be accomplished with dozens of different commands. Heres 10 ways you can do it.
We gathered all of these commands from [Command-Line Fu](https://www.commandlinefu.com/commands/matching/random-password/cmFuZG9tIHBhc3N3b3Jk/sort-by-votes) and tested them out on our own Linux PC to make sure they work. You should be able to use at least some of these on Windows with [Cygwin](https://www.cygwin.com/) installed, though we didnt test all of them—the last one definitely works though.
How to Speed Up a Slow PC
0 seconds of 1 minute, 13 secondsVolume 0%
For any of these random password commands, you can either modify them to output a different password length, or you can just use the first x characters of the generated password if you dont want such a long password. Hopefully youre using a password manager like [LastPass](https://lastpass.wo8g.net/vrA3j?subid3=xid:fr1682042521aaa) anyway so you dont need to memorize them.
This method uses SHA to hash the date, runs through base64, and then outputs the top 32 characters.
> ```
> date +%s | sha256sum | base64 | head -c 32 ; echo
> ```
This method used the built-in /dev/urandom feature, and filters out only characters that you would normally use in a password. Then it outputs the top 32.
> ```
> < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32};echo;
> ```
This one uses openssls rand function, which may not be installed on your system. Good thing theres lots of other examples, right?
> ```
> openssl rand -base64 32
> ```
This one works a lot like the other urandom one, but just does the work in reverse. Bash is very powerful!
> ```
> tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1
> ```
Heres another example that filters using the strings command, which outputs printable strings from a file, which in this case is the urandom feature.
> ```
> strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo
> ```
Heres an even simpler version of the urandom one.
> ```
> < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6
> ```
This one manages to use the very useful dd command.
> ```
> dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev
> ```
You can even create a random left-hand password, which would let you type your password with one hand.
> ```
> </dev/urandom tr -dc '12345!@#$%qwertQWERTasdfgASDFGzxcvbZXCVB' | head -c8; echo ""
> ```
If youre going to be using this all the time, its probably a better idea to put it into a function. In this case, once you run the command once, youll be able to use *randpw* anytime you want to generate a random password. Youd probably want to put this into your ~/.bashrc file.
> ```
> randpw(){ < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;}
> ```
You can use this same syntax to make any of these into a function—just replace everything inside the { }
And heres the easiest way to make a password from the command line, which works in Linux, Windows with Cygwin, and probably Mac OS X. Im sure that some people will complain that its not as random as some of the other options, but honestly, its random enough if youre going to be using the whole thing.
> ```
> date | md5sum
> ```
Yeah, thats even easy enough to remember.
---
Theres loads of other ways that you can create a random password from the command line in Linux—for instance, the mkpasswd command, which can actually assign the password to a Linux user account. So whats your favorite way?
READ NEXT
- [The Best Password Tips to Keep Your Accounts Secure](https://www.howtogeek.com/103560/the-best-password-tips-to-keep-your-accounts-secure/)
- [The Top 10 Tips for Securing Your Data](https://www.howtogeek.com/108033/the-top-10-tips-for-securing-your-data/)
- [How to SSH Into Your Raspberry Pi](https://www.howtogeek.com/768053/how-to-ssh-into-your-raspberry-pi/)
- [The 20 Best How-To Geek Linux Articles of 2010](https://www.howtogeek.com/39595/the-20-best-how-to-geek-linux-articles-of-2010/)
- [How to Add Users on Linux](https://www.howtogeek.com/806104/add-a-user-to-linux/)
- [Acers New Ultrawide Monitors Have USB-C, Up to 175 Hz](https://www.howtogeek.com/887057/acers-new-ultrawide-monitors-have-usb-c-up-to-175-hz/)
- [Acer Couldnt Make This RTX 4090 Gaming PC Any Smaller](https://www.howtogeek.com/886923/acer-couldnt-make-this-rtx-4090-gaming-pc-any-smaller/)
- [Netflix Is Upgrading Its Ad-Supported Plan](https://www.howtogeek.com/887035/netflix-is-upgrading-its-ad-supported-plan/)
How-To Geek is where you turn when you want experts to explain technology. Since we launched in 2006, our articles have been read billions of times. [Want to know more?](https://www.howtogeek.com/about/)