DEV Community

Karandeep Singh
Karandeep Singh

Posted on

2

How to Generate Random Passwords in Bash using `/dev/urandom`

Generating random data is a common task in many applications, especially when it comes to creating secure passwords. In this guide, we'll learn how to generate random passwords using Bash and the /dev/urandom file. This method ensures your passwords are both random and secure. We'll build the script step-by-step, explaining each part so you can easily follow along. By the end, you'll have a complete Bash script to generate random passwords.

Step 1: Generate Random Bytes

To start, we'll generate random bytes using the head command to read from /dev/urandom. Then, we'll use base64 to encode these bytes into a readable format.

head -c 16 /dev/urandom | base64
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • head -c 16 /dev/urandom: This reads 16 bytes from /dev/urandom, a special file that provides random bytes.
  • | base64: This encodes the bytes into a base64 string, making it easy to read.

When you run this command in your terminal, you'll see a random string output, which looks something like this: r8BgD2h+P/QA5FyN.

Step 2: Remove Unwanted Characters

Next, we'll refine the output to include only alphanumeric characters, making the password more user-friendly. We'll use the tr command for this.

head -c 16 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9'
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • tr -dc 'a-zA-Z0-9': This removes any characters that are not in the ranges a-z, A-Z, or 0-9, leaving us with a clean alphanumeric string.

Run this command, and you'll get a cleaner output like r8BgD2hPQA5FyN.

Step 3: Putting It All Together

Let's combine everything into a simple script that you can run anytime you need a new random password.

#!/bin/bash

# Generate a random password
PASSWORD=$(head -c 16 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9')

# Display the password
echo "Your random password is: $PASSWORD"
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • #!/bin/bash: This line specifies that the script should be run in the Bash shell.
  • PASSWORD=$(...): This runs our command and stores the result in the PASSWORD variable.
  • echo "Your random password is: $PASSWORD": This prints the generated password to the screen.

Step 4: Running the Script

To run the script, save it to a file (e.g., generate_password.sh), give it execute permissions, and then run it.

chmod +x generate_password.sh
./generate_password.sh
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • chmod +x generate_password.sh: This makes the script executable.
  • ./generate_password.sh: This runs the script.

When you run the script, you'll see an output like: Your random password is: r8BgD2hPQA5FyN.

Full Script

Here is the complete script for easy reference:

#!/bin/bash

# Generate a random password
PASSWORD=$(head -c 16 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9')

# Display the password
echo "Your random password is: $PASSWORD"
Enter fullscreen mode Exit fullscreen mode

Conclusion

Using /dev/urandom in Bash is a simple and effective way to generate random passwords. This method ensures your passwords are secure and random, which is essential for protecting your data. Now you have a handy script to generate strong passwords anytime you need them!

Feel free to customize the script to suit your needs, and happy coding!

More Reading: Advanced Bash Functions

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (1)

Collapse
 
obambam profile image
Seye •

This is a cool hack nerd type shit

đź‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay