How to generate secure passwords using terminal (Mac/Linux) ?

Published: at 09:30 AM

Passwords protect you from unauthorized access to your computer 💻, application, and other personal information (credit card info, ssn no, etc). The stronger your passwords, the more secured you are.

In today’s date, with everything online, it has become important to secure yourself with stronger passwords to protect yourself from hackers with wrong intent, and malicious software.

It is always advised to create a password with letters, numbers, and special characters as they are pretty difficult to crack. A wide variety of sites provide you with an online tool to generate secure password.

pw-image The biggest concern with online password generator is you are never sure whether the company of the password generator will know your new password or not. How will you know if the password generator’s site has been compromised or not? The password you may have generated from a site might have been compromised and decoded in another place.

There are different password manager applications like LastPass, Dashlane, etc that generates secure password that are safe to use. If you are a MacOS or a Linux, it is easy very easy to generate safe and secure passwords.

PWGEN pwgen is a small package available for both MacOS and Linux that helps you generate secure and strong passwords in no time. The installation and usage is very easy.

Installation For MacOS users, you can simply install using the Homebrew package manager.

brew install pwgen

For Debian/Ubuntu,

apt-get install -y pwgen

For CentOS,

yum install -y pwgen

Usage pwgen is easy to use. Simply, type in pwgen on your terminal and it will generate a list of passwords. generate-pw

> pwgen -s -1 12

The above command will generate single secure password with 12 characters. If you want to add a special character to your password:

> pwgen -s 10 4 -1 -y

The above command will generate 4 secured passwords with 10 characters with at least 1 special character. sp-pw

> pwgen -s 20 -1 -v -c -0

This above command will generate a single password with 20 characters with 0 vowels and numbers with at least 1 single letter in caps.

pwgen is easy to use and is efficient to generate random secure password. You will be easily able to define the password length, characters and symbols. Install this handy utility and make yourself secured 😈🥷🏻

Always pass -s

This one flag matters more than anything else on the page. By default, pwgen generates pronounceable passwords — easier to remember, and meaningfully easier to guess, because they’re drawn from a much smaller set of letter patterns. The -s flag switches to completely random generation:

> pwgen 12 1      # pronounceable, weaker
> pwgen -s 12 1   # fully random

If you’re pasting the result into a password manager and never typing it, there’s no reason to use anything but -s.

No install needed: openssl and /dev/urandom

pwgen is convenient, but every Mac and Linux box can already do this with tools that are installed. If you’re on a server you don’t control, or you’d rather not add a package:

> openssl rand -base64 24

That’s 24 random bytes rendered as base64 — roughly 192 bits of entropy, far beyond what anything needs. For a specific character set, read from the kernel’s random device directly:

> LC_ALL=C tr -dc 'A-Za-z0-9!@#$%^&*' < /dev/urandom | head -c 20; echo

The LC_ALL=C prefix isn’t optional — without it, tr can choke on byte sequences that aren’t valid in your locale.

Both draw from the same cryptographically secure source the operating system uses for TLS keys.

Passphrases, when you have to type it

Some passwords you actually have to type: a disk encryption password, a login you enter before the password manager is available. Random strings are miserable for that. A multi-word passphrase gets you comparable strength with something you can hold in your head:

> LC_ALL=C tr -dc 'a-z\n' < /dev/urandom | grep -Ex '[a-z]{4,8}' | head -5 | paste -sd- -

Five random dictionary-length words is roughly 60+ bits of entropy — stronger than most 10-character passwords people actually choose, and far easier to remember.

How long is long enough?

The honest answer is that it depends on entropy, not character count. A rough guide:

Length (random, mixed case + digits + symbols)Approx. entropyVerdict
8 characters~52 bitsNot sufficient today
12 characters~79 bitsFine for most accounts
16 characters~105 bitsComfortable margin
20+ characters~131 bitsOverkill, and free

Since you’re not memorising these, pick 20 and stop thinking about it. The cost of a longer generated password is zero.

The habits that matter more than the generator

A perfectly generated password still fails if it’s reused. A few things worth being strict about:

The reason to generate locally in the first place is trust — no browser tab, no third-party server, no question about what happened to the value after it appeared on screen. Keeping that property means being just as careful about where the password goes next.

ko-fi