How to Generate Random Passwords in Bash using `/dev/urandom`
Tags: bash, script, password, random
Published: 2024-05-29 17:45:28

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.

```bash
head -c 16 /dev/urandom | base64
```

**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.

```bash
head -c 16 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9'
```

**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.

```bash
#!/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"
```

**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.

```bash
chmod +x generate_password.sh
./generate_password.sh
```

**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:

```bash
#!/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"
```

### 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](https://karandeepsingh.ca/post/advanced-string-operations-in-bash-building-custom-functions/)