Linux password generator

Who doesn’t know this: you need a password. It must have a certain length, contain numbers (large and small) and possibly even one or two special characters. In addition, it must also be hack-proof. Today people like to use password generators for this. In this article, you will see how to quickly and easily create your own password generator under Linux.

1. Pwgen

We will start with Pwgen. In order to install it, open up the terminal and type in the command :

sudo apt install pwgen

It will generate a bunch of passwords by simply executing pwgen command.

Pwgen                                   [password generator Linux]

Custom password generator

For the everyday creation of secure passwords, the following shell script can provide passwords at the push of a button. The script is called via a function name and the length of the password as parameters in the shell.. How do you do that?

Read: How to recover deleted files on linux , an Exhaustive list of tools

Create bash functions

The command line in Linux is implemented by the shell. There are many different shell programs, for example on Ubuntu this is the bash by default. As always under Linux, you can configure the system as you wish, so you can expand the bash with additional functions. Functions and variables can be added to your own home folder in the .bashrc file. Here’s how:

nano .bashrc

We can add the additional functionality at the end of the file.

Password script

With the following function you can add the password function of the bash:

genpasswd() {

local l=$1

[ “$l” == “” ] && l=8

tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs


generate passwords linux

What exactly does the function do?

  • Line 1: the function is defined here. The function name genpasswd is the name of the function that can be called in the shell. You can define this function name yourself.
  • Line 2: in the variable with the name “l” we save the value of the first parameter (this will be the desired password length as a number).
  • Line 3: if no parameter is passed, we set the password length to 8 characters.
  • Line 4: The password is created in this line
    • tr: with the translate characters command, you can only filter certain characters out of a random set of characters. In this case, we get random characters from / dev / urandom, of this set we only keep capital letters (AZ), lowercase letters (az), numbers (0-9) and the character “_”. Depending on the desired password type, additional characters can be specified. We send the result of the tr command to the head command, which only forwards the first few bytes. The number of bytes depends on the parameter (length of the password). With the command xargs we output the created password on the command line.

After restarting the bash, you can use the function and create your own passwords.


It is very easy to add new functions and variables to the bash. A suitable application example is a password generator. This Linux password generator is very useful for daily operations as a developer or user.

If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.


amin nahdy

Amin Nahdy, an aspiring software engineer and a computer geek by nature as well as an avid Ubuntu and open source user. He is interested in information technology especially Linux based ecosystem as well as Windows and MacOS. He loves to share and disseminate knowledge to others in a transparent and responsible way.

Leave a Reply

Close Menu