Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a password in Bash

Tags:

bash

passwords

I currently have a password that is being generated, that is a hash of the date. I'd like to increase this generated password's security, by adding in some uppercase, lower case, numeric, and special characters, all to a minimum length of 10 characters.

What I currently have is below. As you can see, it assigns the output to my PASSWORD variable.

PASSWORD=$(date +%s | sha256sum | base64 | head -c 15)

I'm unsure if I can do this inline, or, if I need to create a function within my bash script to suit? Is that even possible?

like image 427
Graeme Leighfield Avatar asked Sep 13 '25 09:09

Graeme Leighfield


2 Answers

With tr and head:

password=$(tr -dc 'A-Za-z0-9!?%=' < /dev/urandom | head -c 10)

echo "$password"

Output (example):

k?lmyif6aE

tr reads bytes via stdin from Linux‘ special random device /dev/urandom and removes all bytes/characters but A to Z, a to z, 0 to 9 and !?%=. tr sends its output via stdout to head‘s stdin. head cuts output after 10 bytes.

like image 192
Cyrus Avatar answered Sep 16 '25 02:09

Cyrus


Or you can just use openSSL rand to generate the passwords:

PASSWORD=$(openssl rand -base64 10)

The above command for example will generate a string of 10 characters in base64.

like image 21
Melchia Avatar answered Sep 16 '25 02:09

Melchia