Saturday, February 7, 2015

Generate Random Passwords Easily

I have added a short function in my ~/.bashrc file that allows me to quickly generate a password of any length containing letters, numbers and symbols. This comes in handy when registering for a new website or similar.

Randomness

 

The command uses /dev/urandom to generate the random string. Evaluating the true randomness and strength of this password is beyond this guide. However, this should be sufficient for any general use.

Step by Step

  1. Edit the .bashrc file to add a new function that can then be called by an alias command.
    nano ~/.bashrc
  2. Add the following lines to the end of the file [1],
    passwdgen()
    {
    tr -dc "a-zA-Z0-9-_!@#$%^&*()_+{}|:<>?=" < /dev/urandom | fold -w $1 | head -n 4
    }
  3. Then add an alias to easily call this function by,
    nano ~/.bash_aliases
  4. Add an alias of your liking.
    alias genpasswd='passwdgen' 
  5. Apply changes by either logging out and back in or,
    sudo source ~/.bashrc

Originally I wanted to call the alias command "passwdgen", however when using TAB to quickly substitute commands this was conflicting with "passwd". Hence I decided to use "genpasswd".

Done. The command can be used as,
genpasswd <password_length>
For example I want a password with 30 characters I would do,

:~$ genpasswd 30
0kA|=yff2RT(LE#|%QVMZVsFq2Tm&|
OPw1x=R9QMt}KhR*Xp?tluvu70+6=M
1-s)%-G19Gucz!fd@!+WXQZQ<U0ZVx
vJg:-Bvx)>>-?1qe_-TvEG8SHnUWXz

Note: You get 4 passwords as an output from the function (head -n 4). This is simply to give a choice for the user to decide which password to use. Also, this adds another increment of randomness.

The list of used symbols can be altered by editing the function contents within the quotes "a-zA-Z0-9-_!@#$%^&*()_+{}|:<>?=". Adding extra symbols will increase the strength of the password, however some website (rarely) have restrictions on what symbols can be used.

Sources

No comments:

Post a Comment