comp.org.uk

Networking | Programming | Security | Linux | Computer Science | About

Generate a Random Password in Linux

One of the most important safeguards you have to protect your Linux machine is a strong password. This encrypted entry mechanism allows you to keep your data secure. It is generally thought that a strong password should be made up of fourteen characters, including uppercase, lowercase, alphabetic, numeric and symbols. The reason behind the fourteen characters is the need for a longer password rather than a short one - a longer password is harder to guess. The Linux password should not be based on a dictionary word and shouldn't be personalised either.

The following two commands allow for the generation of a strong random Linux password from the command line:

$ openssl rand -base64 48 | cut -c1-PASSWORD_LENGTH

$ gpw () { openssl rand -base64 48 | cut -c1-${1}; }

You can use the openssl command to generate a random password. If you find yourself doing this often, you can create a function. Simply pass in how long you want the password to be.

$ echo 'gpw () { openssl rand -base64 48 | cut -c1-${1}; }' >> ~/.bash_profile 
$ . ~/.bash_profile 
$ gpw 
t3eyxkXBHAzb3VdR7G8NV3fMvZpXLOvT+AQwgQnw9pLm/UaRNHcP $ gpw 6 uu1ZMb 
$
$ gpw 6 
uu1ZMb 
$

Published on Mon 12 March 2012 by Dave Wilson in Linux with tag(s): linux password