Password Generation in UNIX

Below I’ll describe a couple of nice methods to generate passwords using Python and Bash. Actually there are a lot of ways you can accomplish this especially with bash, but using the /dev/urandom file seems to be the most clever one. The /dev/urandom device doesn’t only generate read-friendly characters, so it’s best to filter out the ones we’d like. The best tool for that would be tr. $ cat /dev/urandom | tr -dc [:alnum:] | head -c 10 This will generate a password from 10 alphanumeric characters. It will not include some characters though, such as . ! – _ which are useful for passwords. So this line would be a little more “secure”. $ cat /dev/urandom | tr -cd “[:alnum:]\.\-_\!” | head -c 10 To generate a password in Python, using the string and random module would be a clever touch. Let’s try something like this,
>>> import string, random
 >>> def passgen(length) :
 ... keys = list(string.ascii_letters + string.digits)
 ... return "".join(random.choice(keys) for i in range(length)

With this definition of the passgen function, we can generate alphanumeric passwords with whatever length we want. If you’d like to include all characters available, try the one below:
>>> import string, random
 >>> def passgen(length) :
 ... keys = list(string.ascii_letters + string.digits + ".,;:-_()@\"\\[]?!'^+*$%&/=~`<>|")
 ... return "".join(random.choice(keys) for i in range(length)

A sample output :
>>> passgen(16)
 'pP!3p"(-uxdIqpAK'

You can find some methods of password generation using MD5 algorithms. For example for password generation in MySQL some people prefer this method; >SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 5) But this will generate very very weak passwords, no uppercase characters and a lot of characters missing, not even to mention the non-alpha numeric characters. Also you’ll have a limit for maximum character number since the MD5 algorithm has a limit for it. So it’s best to stay away from the md5 approach for password generation. Some people also use it for bash password generation too (which is wrong! due to same reasons)
  • 90 Users Found This Useful
Was this answer helpful?

Related Articles

Setting time with NTP in LINUX

NTP (Network Time Protocol) could be used to set time synced with ntp clocks, to do this use the...

Linux version & Operating System info

LINUX Version Info To learn the architecture and kernel version info use the shell command...

Build PHP5.5 on CentOS 6.4 with MSSQL Support [cite]

Most of the yum repos doesn’t include PHP5.5.X on current releases for the time being. So...

Change Outgoing IP of Postfix Mail Server

This can get quite important when your mail server is blacklisted, or if you somehow want to...

Add New Hosting to a System Installed With Plugged.sh

If you use our LAMP installer script and want to add a new domain afterwards, we’ve created...