Create Your Own Password Generator

This project is for anyone who operates at the Linux command line (that’s you, right?).

The Raspberry Pi Computer

If you don’t already know about the power of the Linux command line, you can buy a Linux desktop computer for as little as $35 (or thereabouts).  Check out the Raspberry Pi if you want a cheap, but incredibly powerful entry into the world of Linux.  It’s a full-feature desktop computer for those who like that flashy desktop stuff, but with all the command and control of the Linux terminal for those who prefer operating at the black screen.

We use headless Raspberry Pi’s for various tasks:

  • Discreet wifi or bluetooth operated wildlife camera.
  • Web server used for staging this website before moving it to our cloud-based production server.
  • File server for all our networking needs to run our business.
  • Development box with a full scientific development stack based on Anaconda.
  • Server for managing Sonos and our music library.

But enough about the Raspberry Pi.  This project is for those that have already made the move to Linux. (Congratulations).

Project:  Create Your Own Linux Password Generator

This one is simple.  It uses built-in Linux tools to produce a password of any length. We’ve set the default length to 20 characters, but you can change that in the code below. Let’s start at the terminal prompt:

Step 1:  From the Linux terminal create a text file named password.sh that we will add our code into: We use the built-in text editor: nano.

nano password.sh

Step 2:  Now that you are inside the nano text editor, enter the following code:

#! /usr/bin/bash
if [ "$1" != "" ]
then
LENGTH1=$1
else
LENGTH1="20"
fi
# text below is all one line. It may have wrapped for this display, but type it in all on one continuous line. Enter it on the next line of your program:

LC_ALL=C tr -dc ‘A-Za-z0-9!#$~%&()*+,-./:;<=>?@[]^_{|}~’ </dev/urandom | head -c $LENGTH1 ; echo

# End of File.

Step 3:  Make the file executable with the chmod command:
chmod +x password.sh

Now, whenever you need a password, type  ./password.sh at the command prompt like below:
./password.sh

That will create a 20-digit password for you.  If you want a 40 digit password, add the number 40 to you command like this:
./password.sh 40

You can enter any number you like for your password length, and the code will generate a password for you that long.  If you don’t enter a number after entering “./password.sh” the generated password will default to 20 digits long.  This default length variable is set on the line of code in your file:

LENGTH1="20"

and you can change it inside the program if you don’t want to use 20 as your default password length.  I’ve excluded a few symbols such as \ ~ ‘ from the potential choices used in password generation. If you want to add them back in, do so within the list.  If you want to remove additional symbols, go right ahead. You can do what you want.  This code is yours to modify as you like.

Run it over and over until you get a password that suits you, and then use your mouse to highlight the entire password.  Copy it and then immediately paste it into the file where you store passwords.

I personally store every password within quotation marks.  That way I always know the boundaries of the password, even if the first or last character of the password is a “.

Once you’ve recorded your new password somewhere, paste it into whatever field you are confronting that is asking you to enter a password.

If you’d like to make your own Password Vault, check out another project here.