Generate Random Passwords with Perl
Luke Hebert
- 2 minutes read - 228 wordsNeed a strong password in a hurry? A single Perl command can generate one for you right from the terminal, no extra packages required.
The Script
The attached script, genpass.pl, uses Perl’s built-in rand and character-set selection to produce a random password of a configurable length. By default it generates a 16-character string drawn from uppercase letters, lowercase letters, digits, and a small set of special characters.
Usage
Run the script directly from the command line:
perl genpass.plTo change the password length, pass the desired number of characters as the first argument:
perl genpass.pl 24Why Perl?
Perl ships with most Unix and Linux systems, so you do not need to install anything extra. The one-liner approach keeps the solution lightweight: no dependencies, no configuration files, and a result in under a second.
The script relies on Perl’s rand function, which is suitable for general-purpose password generation. For security-critical contexts (for example, generating keys for production systems), prefer a tool that draws from the OS entropy pool, such as openssl rand or yarn random.
Tips
- Pipe the output to
xcliporpbcopyto copy the password straight to your clipboard. - Combine with
tr -dif you want to strip a specific character set from the result. - Store generated passwords in a password manager immediately; do not leave them in shell history.