Generate Secure Passwords From The Cli

Generate Secure Passwords from the CLI with pwgen

Securing your online presence with strong, unique passwords is essential. If you spend a lot of time in the terminal, pwgen is a great tool for generating secure passwords directly from the command line. Here’s a quick guide for macOS and Linux users, along with a handy CLI shortcut.

Installing pwgen

macOS

Install pwgen using Homebrew:

brew install pwgen

Linux

Install pwgen using your package manager:

Debian/Ubuntu:

sudo apt-get install pwgen

Fedora:

sudo dnf install pwgen

Basic Usage

Generate a secure password:

pwgen -s 16 1

Enhanced Security

Include special characters and avoid ambiguous ones:

pwgen -s -y -B 16 1

Creating a CLI Shortcut

To make password generation even easier, create a function that generates a password, copies it to the clipboard, and displays a message.

macOS

Add this to your shell configuration file (e.g., .bashrc, .zshrc):

genpasswd() {
  pwgen -s -y -B ${1:-16} 1 | tee >(pbcopy) | sed 's/.*/Mission success. Password locked and loaded!/'
}

Linux

Install xclip:

sudo apt-get install xclip

Add this to your shell configuration file:

genpasswd() {
  pwgen -s -y -B ${1:-16} 1 | tee >(xclip -selection clipboard) | sed 's/.*/Mission success. Password locked and loaded!/'
}

Usage

Reload your shell configuration:

source ~/.bashrc  # or source ~/.zshrc

Generate and copy a password:

genpasswd 20  # Generates a 20-character password
genpasswd     # Generates a default 16-character password

Output:

Mission success. Password locked and loaded!

Conclusion

Using pwgen and a simple CLI shortcut, you can quickly generate and copy secure passwords. This setup ensures strong passwords are always just a command away. Stay secure!

Happy password generating! 💻🔐